Unverified Commit 6728ca19 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3039354 by nikolay shapovalov, tanuj., ranjit1032002, Abhisheksingh27,...

Issue #3039354 by nikolay shapovalov, tanuj., ranjit1032002, Abhisheksingh27, sahil.goyal, lamp5, ranjith_kumar_k_u, krzysztof domański, smustgrave, drupgirl, alexpott: The current user views default argument should use dependency injection
parent 89a009ac
Loading
Loading
Loading
Loading
Loading
+30 −1
Original line number Diff line number Diff line
@@ -4,9 +4,11 @@

use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\views\Attribute\ViewsArgumentDefault;
use Drupal\views\Plugin\views\argument_default\ArgumentDefaultPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Default argument plugin to extract the current user.
@@ -19,11 +21,38 @@
)]
class CurrentUser extends ArgumentDefaultPluginBase implements CacheableDependencyInterface {

  /**
   * CurrentUser constructor.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Session\AccountInterface|null $currentUser
   *   The current user.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, protected ?AccountInterface $currentUser = NULL) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    if ($this->currentUser === NULL) {
      @trigger_error('Calling ' . __CLASS__ . '::__construct() without the $current_user argument is deprecated in drupal:11.2.0 and is required in drupal:12.0.0. See https://www.drupal.org/node/3347878', E_USER_DEPRECATED);
      $this->currentUser = \Drupal::currentUser();
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container->get('current_user'));
  }

  /**
   * {@inheritdoc}
   */
  public function getArgument() {
    return \Drupal::currentUser()->id();
    return $this->currentUser->id();
  }

  /**