Verified Commit a9b40008 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3112298 by Spokje, mpdonadio, ravi.shankar, quietone, alexpott,...

Issue #3112298 by Spokje, mpdonadio, ravi.shankar, quietone, alexpott, smustgrave, immaculatexavier, ranjith_kumar_k_u, ankithashetty, mondrake, daffie, andypost, vladbo, JeroenT, pifagor, longwave, voleger: Replace REQUEST_TIME in classes with direct container access
parent 7ea369d9
Loading
Loading
Loading
Loading
Loading
+11 −4
Original line number Diff line number Diff line
@@ -74,10 +74,17 @@ public static function create(ContainerInterface $container) {
   *   The entity type bundle service.
   * @param \Drupal\Component\Datetime\TimeInterface $time
   *   The time service.
   * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
   *   The entity field manager service.
   * @param \Drupal\Core\Entity\EntityFieldManagerInterface|null $entity_field_manager
   *   (optional) The entity field manager service.
   */
  public function __construct(EntityRepositoryInterface $entity_repository, AccountInterface $current_user, RendererInterface $renderer, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, TimeInterface $time = NULL, EntityFieldManagerInterface $entity_field_manager = NULL) {
  public function __construct(
    EntityRepositoryInterface $entity_repository,
    AccountInterface $current_user,
    RendererInterface $renderer,
    EntityTypeBundleInfoInterface $entity_type_bundle_info,
    TimeInterface $time,
    EntityFieldManagerInterface $entity_field_manager = NULL,
  ) {
    parent::__construct($entity_repository, $entity_type_bundle_info, $time);
    $this->currentUser = $current_user;
    $this->renderer = $renderer;
@@ -286,7 +293,7 @@ public function buildEntity(array $form, FormStateInterface $form_state) {
      $comment->setCreatedTime($form_state->getValue('date')->getTimestamp());
    }
    else {
      $comment->setCreatedTime(REQUEST_TIME);
      $comment->setCreatedTime($this->time->getRequestTime());
    }
    // Empty author ID should revert to anonymous.
    $author_id = $form_state->getValue('uid');
+25 −6
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@

namespace Drupal\content_translation;

use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
@@ -123,8 +124,21 @@ class ContentTranslationHandler implements ContentTranslationHandlerInterface, E
   *   The installed entity definition repository service.
   * @param \Drupal\Core\Routing\RedirectDestinationInterface|null $redirectDestination
   *   The request stack.
   */
  public function __construct(EntityTypeInterface $entity_type, LanguageManagerInterface $language_manager, ContentTranslationManagerInterface $manager, EntityTypeManagerInterface $entity_type_manager, AccountInterface $current_user, MessengerInterface $messenger, DateFormatterInterface $date_formatter, EntityLastInstalledSchemaRepositoryInterface $entity_last_installed_schema_repository, protected ?RedirectDestinationInterface $redirectDestination = NULL) {
   * @param \Drupal\Component\Datetime\TimeInterface|null $time
   *   The time service.
   */
  public function __construct(
    EntityTypeInterface $entity_type,
    LanguageManagerInterface $language_manager,
    ContentTranslationManagerInterface $manager,
    EntityTypeManagerInterface $entity_type_manager,
    AccountInterface $current_user,
    MessengerInterface $messenger,
    DateFormatterInterface $date_formatter,
    EntityLastInstalledSchemaRepositoryInterface $entity_last_installed_schema_repository,
    protected ?RedirectDestinationInterface $redirectDestination = NULL,
    protected ?TimeInterface $time = NULL,
  ) {
    $this->entityTypeId = $entity_type->id();
    $this->entityType = $entity_type;
    $this->languageManager = $language_manager;
@@ -138,6 +152,10 @@ public function __construct(EntityTypeInterface $entity_type, LanguageManagerInt
      @trigger_error('Calling ContentTranslationHandler::__construct() without the $redirectDestination argument is deprecated in drupal:10.2.0 and will be required in drupal:11.0.0. See https://www.drupal.org/node/3375487', E_USER_DEPRECATED);
      $this->redirectDestination = \Drupal::service('redirect.destination');
    }
    if ($this->time === NULL) {
      @trigger_error('Calling ' . __METHOD__ . ' without the $time argument is deprecated in drupal:10.3.0 and it will be required in drupal:11.0.0. See https://www.drupal.org/node/3301971', E_USER_DEPRECATED);
      $this->time = \Drupal::service('datetime.time');
    }
  }

  /**
@@ -153,7 +171,8 @@ public static function createInstance(ContainerInterface $container, EntityTypeI
      $container->get('messenger'),
      $container->get('date.formatter'),
      $container->get('entity.last_installed_schema.repository'),
      $container->get('redirect.destination')
      $container->get('redirect.destination'),
      $container->get('datetime.time'),
    );
  }

@@ -536,7 +555,7 @@ public function entityFormAlter(array &$form, FormStateInterface $form_state, En
        '#description' => t('Leave blank for %anonymous.', ['%anonymous' => \Drupal::config('user.settings')->get('anonymous')]),
      ];

      $date = $new_translation ? REQUEST_TIME : $metadata->getCreatedTime();
      $date = $new_translation ? $this->time->getRequestTime() : $metadata->getCreatedTime();
      $form['content_translation']['created'] = [
        '#type' => 'textfield',
        '#title' => t('Authored on'),
@@ -706,7 +725,7 @@ public function entityFormEntityBuild($entity_type, EntityInterface $entity, arr
    $metadata = $this->manager->getTranslationMetadata($entity);
    $metadata->setAuthor(!empty($values['uid']) ? User::load($values['uid']) : User::load(0));
    $metadata->setPublished(!empty($values['status']));
    $metadata->setCreatedTime(!empty($values['created']) ? strtotime($values['created']) : REQUEST_TIME);
    $metadata->setCreatedTime(!empty($values['created']) ? strtotime($values['created']) : $this->time->getRequestTime());

    $metadata->setOutdated(!empty($values['outdated']));
    if (!empty($values['retranslate'])) {
@@ -753,7 +772,7 @@ public function entityFormSubmit($form, FormStateInterface $form_state) {
    // handler as well and have the same logic like in the Form API.
    if ($entity->hasField('content_translation_changed')) {
      $metadata = $this->manager->getTranslationMetadata($entity);
      $metadata->setChangedTime(REQUEST_TIME);
      $metadata->setChangedTime($this->time->getRequestTime());
    }
  }

+11 −3
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@

namespace Drupal\content_translation\Controller;

use Drupal\Component\Datetime\TimeInterface;
use Drupal\content_translation\ContentTranslationManager;
use Drupal\content_translation\ContentTranslationManagerInterface;
use Drupal\Core\Cache\CacheableMetadata;
@@ -40,10 +41,16 @@ class ContentTranslationController extends ControllerBase {
   *   A content translation manager instance.
   * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
   *   The entity field manager service.
   * @param \Drupal\Component\Datetime\TimeInterface|null $time
   *   The time service.
   */
  public function __construct(ContentTranslationManagerInterface $manager, EntityFieldManagerInterface $entity_field_manager) {
  public function __construct(ContentTranslationManagerInterface $manager, EntityFieldManagerInterface $entity_field_manager, protected ?TimeInterface $time = NULL) {
    $this->manager = $manager;
    $this->entityFieldManager = $entity_field_manager;
    if ($this->time === NULL) {
      @trigger_error('Calling ' . __METHOD__ . ' without the $time argument is deprecated in drupal:10.3.0 and it will be required in drupal:11.0.0. See https://www.drupal.org/node/3301971', E_USER_DEPRECATED);
      $this->time = \Drupal::service('datetime.time');
    }
  }

  /**
@@ -52,7 +59,8 @@ public function __construct(ContentTranslationManagerInterface $manager, EntityF
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('content_translation.manager'),
      $container->get('entity_field.manager')
      $container->get('entity_field.manager'),
      $container->get('datetime.time'),
    );
  }

@@ -111,7 +119,7 @@ public function prepareTranslation(ContentEntityInterface $entity, LanguageInter
    // Update the translation author to current user, as well the translation
    // creation time.
    $metadata->setAuthor($user);
    $metadata->setCreatedTime(REQUEST_TIME);
    $metadata->setCreatedTime($this->time->getRequestTime());
    $metadata->setSource($source_langcode);
  }

+11 −3
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@

namespace Drupal\locale\Form;

use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
@@ -36,7 +37,8 @@ class TranslationStatusForm extends FormBase {
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('module_handler'),
      $container->get('state')
      $container->get('state'),
      $container->get('datetime.time'),
    );
  }

@@ -47,10 +49,16 @@ public static function create(ContainerInterface $container) {
   *   A module handler.
   * @param \Drupal\Core\State\StateInterface $state
   *   The state service.
   * @param \Drupal\Component\Datetime\TimeInterface|null $time
   *   The time service.
   */
  public function __construct(ModuleHandlerInterface $module_handler, StateInterface $state) {
  public function __construct(ModuleHandlerInterface $module_handler, StateInterface $state, protected ?TimeInterface $time = NULL) {
    $this->moduleHandler = $module_handler;
    $this->state = $state;
    if ($this->time === NULL) {
      @trigger_error('Calling ' . __METHOD__ . ' without the $time argument is deprecated in drupal:10.3.0 and it will be required in drupal:11.0.0. See https://www.drupal.org/node/3301971', E_USER_DEPRECATED);
      $this->time = \Drupal::service('datetime.time');
    }
  }

  /**
@@ -280,7 +288,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
    // translation updates. If the status is expired we clear it and run a batch
    // to update the status and then fetch the translation updates.
    $last_checked = $this->state->get('locale.translation_last_checked');
    if ($last_checked < REQUEST_TIME - LOCALE_TRANSLATION_STATUS_TTL) {
    if ($last_checked < $this->time->getRequestTime() - LOCALE_TRANSLATION_STATUS_TTL) {
      locale_translation_clear_status();
      $batch = locale_translation_batch_update_build([], $langcodes, $options);
      batch_set($batch);
+19 −3
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@

namespace Drupal\migrate_drupal_ui\Form;

use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Batch\BatchBuilder;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Extension\Exception\UnknownExtensionException;
@@ -76,11 +77,25 @@ class ReviewForm extends MigrateUpgradeFormBase {
   *   The config factory service.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler service.
   * @param \Drupal\Component\Datetime\TimeInterface|null $time
   *   The time service.
   */
  public function __construct(StateInterface $state, MigrationPluginManagerInterface $migration_plugin_manager, PrivateTempStoreFactory $tempstore_private, MigrationState $migrationState, ConfigFactoryInterface $config_factory, ModuleHandlerInterface $module_handler) {
  public function __construct(
    StateInterface $state,
    MigrationPluginManagerInterface $migration_plugin_manager,
    PrivateTempStoreFactory $tempstore_private,
    MigrationState $migrationState,
    ConfigFactoryInterface $config_factory,
    ModuleHandlerInterface $module_handler,
    protected ?TimeInterface $time = NULL,
  ) {
    parent::__construct($config_factory, $migration_plugin_manager, $state, $tempstore_private);
    $this->migrationState = $migrationState;
    $this->moduleHandler = $module_handler;
    if ($this->time === NULL) {
      @trigger_error('Calling ' . __METHOD__ . ' without the $time argument is deprecated in drupal:10.3.0 and it will be required in drupal:11.0.0. See https://www.drupal.org/node/3301971', E_USER_DEPRECATED);
      $this->time = \Drupal::service('datetime.time');
    }
  }

  /**
@@ -93,7 +108,8 @@ public static function create(ContainerInterface $container) {
      $container->get('tempstore.private'),
      $container->get('migrate_drupal.migration_state'),
      $container->get('config.factory'),
      $container->get('module_handler')
      $container->get('module_handler'),
      $container->get('datetime.time'),
    );
  }

@@ -249,7 +265,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
    batch_set($batch_builder->toArray());
    $form_state->setRedirect('<front>');
    $this->store->set('step', 'overview');
    $this->state->set('migrate_drupal_ui.performed', REQUEST_TIME);
    $this->state->set('migrate_drupal_ui.performed', $this->time->getRequestTime());
  }

  /**
Loading