Problem/Motivation

User ID from logged in user default argument for views contextual filter, right now is getting from \Drupal::curentUser(); global class instead of dependency injection to get AccountProxy object of current user.

Proposed resolution

Add dependency injection to the Drupal\user\Plugin\views\argument_default\CurrentUser class
Also, fix phpDoc.

Issue fork drupal-3039354

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

lamp5 created an issue. See original summary.

lamp5’s picture

Assigned: lamp5 » Unassigned
Status: Active » Needs review
StatusFileSize
new2.75 KB
lamp5’s picture

Version: 8.6.x-dev » 8.7.x-dev
Issue tags: -current user, -views, -views contextual filter, -views default argument +VDC

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.9 was released on November 6 and is the final full bugfix release for the Drupal 8.7.x series. Drupal 8.7.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.8.0 on December 4, 2019. (Drupal 8.8.0-beta1 is available for testing.)

Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev
ranjith_kumar_k_u’s picture

StatusFileSize
new1.97 KB

Re-rolled #2 for 9.4.

removed following code from the patch because the change is already there.

--- a/core/modules/user/src/Plugin/views/argument_default/CurrentUser.php
+++ b/core/modules/user/src/Plugin/views/argument_default/CurrentUser.php
@@ -4,10 +4,12 @@
 
 
 /**
- * Default argument plugin to extract the current user
+ * Default argument plugin to extract the current user.
  *
  * This plugin actually has no options so it does not need to do a great deal.
  *
--- a/core/modules/user/src/Plugin/views/argument_default/User.php
+++ b/core/modules/user/src/Plugin/views/argument_default/User.php
@@ -37,7 +37,6 @@ class User extends ArgumentDefaultPluginBase implements CacheableDependencyInter
    *   The plugin_id for the plugin instance.
    * @param mixed $plugin_definition
    *   The plugin implementation definition.
-   *
    * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
    *   The route match.
    */

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +Needs Review Queue Initiative

This issue is being reviewed by the kind folks in Slack, #needs-review-queue-initiative. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge request as a guide.

Couldn't find the meta but this would fall under replacing service calls with dependency injections one.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/modules/user/src/Plugin/views/argument_default/CurrentUser.php
@@ -18,11 +20,42 @@
+  /**
+   * 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\AccountProxyInterface $currentUser
+   *   The current user.
+   */
+  public function __construct(array $configuration, $plugin_id, $plugin_definition, AccountProxyInterface $currentUser) {
+    parent::__construct($configuration, $plugin_id, $plugin_definition);
+    $this->currentUser = $currentUser;
+  }

This should use property promotion and also needs to do a deprecation dance... something like this

  public function __construct(array $configuration, $plugin_id, $plugin_definition, protected ?AccountProxyInterface $currentUser = NULL) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    if ($currentUser === NULL) {
      @trigger_error('Calling ' . __CLASS__ . '::__construct() without the $currentUser argument is deprecated in drupal:10.1.0 and is removed in drupal:11.0.0. See https://www.drupal.org/node/TBD', E_USER_DEPRECATED);
      $this->currentUser = \Drupal::currentUser();
    }
  }

Note a change record will need to be put in place.

sahil.goyal’s picture

StatusFileSize
new2.25 KB
new1.08 KB

Addressed #12 as per that updating patch and attaching the inter_diff along.

ranjit1032002’s picture

Status: Needs work » Needs review
StatusFileSize
new2.24 KB
new1.07 KB

Addressed #12 as per that updating patch and attaching the inter_diff along.

ranjit1032002’s picture

StatusFileSize
new1.07 KB

attaching the inter_diff

Status: Needs review » Needs work

The last submitted patch, 14: 3039354-14.patch, failed testing. View results

krzysztof domański’s picture

Issue tags: +Needs change record

@Ranjit1032002 try

if ($currentUser === NULL) {
  @trigger_error...
  $currentUser = \Drupal::currentUser();
}
$this->currentUser = $currentUser;
Abhisheksingh27’s picture

Assigned: Unassigned » Abhisheksingh27
Abhisheksingh27’s picture

Assigned: Abhisheksingh27 » Unassigned
Status: Needs work » Needs review
StatusFileSize
new2.28 KB
new963 bytes

Adding updated #14 patch after change record.

smustgrave’s picture

Status: Needs review » Needs work

Needs the change record to review now.

krzysztof domański’s picture

Version: 9.5.x-dev » 10.1.x-dev
Issue tags: -Needs change record

Change record https://www.drupal.org/node/3347878

Still needs work. Change link https://www.drupal.org/node/TBD.
+ @trigger_error('Calling' . __CLASS__ . '::__construct() without the $currentUser argument is deprecated in drupal:10.1.0 and is removed in drupal:11.0.0. See https://www.drupal.org/node/TBD', E_USER_DEPRECATED);

krzysztof domański’s picture

1/ Naming conventions should be consistent throughout the code. Change $currentUser to $current_user.
public function __construct(array $configuration, $plugin_id, $plugin_definition, AccountProxyInterface $currentUser = NULL) {

2/ It should be more descriptive.

+  /**
+   * CurrentUser.
+   *
+   * @var \Drupal\Core\Session\AccountProxyInterface
+   */
+  protected $currentUser;
+
+  /**
+   * CurrentUser constructor.
tanuj.’s picture

Status: Needs work » Needs review
StatusFileSize
new2.31 KB
new1.86 KB

Updated the patch as per #21 & #22, please review.
*interdiff is with #19

krzysztof domański’s picture

'Calling' . __CLASS__ . '::__construct() without the $currentUser argument
One more $currentUser to change to $current_user.

krzysztof domański’s picture

Status: Needs review » Needs work
tanuj.’s picture

Status: Needs work » Needs review
StatusFileSize
new2.31 KB
new1.07 KB

Updated according to #24. please review.

smustgrave’s picture

Status: Needs review » Needs work

Will need a simple deprecation test to ensure the message is returned correctly

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

drupgirl’s picture

This patch was working great, but it no longer applies to 10.3.1 resulting in the following error that prevents the upgrade.

Fatal error: Could not check compatibility between Drupal\user\Plugin\views\argument_default\CurrentUser::create(Drupal\user\Plugin\views\argument_default\ContainerInterface $container, arr
ay $configuration, $plugin_id, $plugin_definition) and Drupal\views\Plugin\views\PluginBase::create(Symfony\Component\DependencyInjection\ContainerInterface $container, array $configuration
, $plugin_id, $plugin_definition), because class Drupal\user\Plugin\views\argument_default\ContainerInterface is not available in .../web/core/modules/user/src/Plugin/views/argument_default/CurrentUser.php on line 53

drupgirl’s picture

Oh so the only part of the patch that is needed for 10.3 is this. Once applied the update works.

diff --git a/core/modules/user/src/Plugin/views/argument_default/CurrentUser.php b/core/modules/user/src/Plugin/views/argument_default/CurrentUser.php
index 251d47198b..b0c11febe4 100644
--- a/core/modules/user/src/Plugin/views/argument_default/CurrentUser.php
+++ b/core/modules/user/src/Plugin/views/argument_default/CurrentUser.php
@@ -4,7 +4,9 @@

use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheableDependencyInterface;
+use Drupal\Core\Session\AccountProxyInterface;
use Drupal\views\Plugin\views\argument_default\ArgumentDefaultPluginBase;
+use Symfony\Component\DependencyInjection\ContainerInterface;

nikolay shapovalov’s picture

Assigned: Unassigned » nikolay shapovalov

That's great I found this issue, because I wasn't sure if DI should be used here.
I will update patch, update message, add deprecation test and move from patch to MR approach.

nikolay shapovalov’s picture

Assigned: nikolay shapovalov » Unassigned
Status: Needs work » Needs review

Mark $current_user argument as nullable.
MR is ready for review.

nikolay shapovalov’s picture

smustgrave’s picture

Status: Needs review » Needs work

Left some comments on the MR.

nikolay shapovalov’s picture

Assigned: Unassigned » nikolay shapovalov

Thanks for feedback. I will update MR.

nikolay shapovalov’s picture

Assigned: nikolay shapovalov » Unassigned
Status: Needs work » Needs review

Thanks again for your feedback. I made changes, MR is ready for review.

Introduce return type hinting for create() is better at #3050720: [Meta] Implement strict typing in existing code.

nikolay shapovalov’s picture

I update deprecation test, because after changing currentUser to property promotion unit test failed.
Should we add type hinting for property or skip for now and add only at D12?

nikolay shapovalov’s picture

I hide all patch files.
MR updated: move deprecation test to user module.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Will agree with the static return.

Rest of the feedback appers to be addressed.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

I removed the test because we do not require tests for constructor changes like this.

Committed 6728ca1 and pushed to 11.x. Thanks!

  • alexpott committed 6728ca19 on 11.x
    Issue #3039354 by nikolay shapovalov, tanuj., ranjit1032002,...
nikolay shapovalov’s picture

Great job, thanks for merging.

It looks like we forgot to change argument name at deprecation message.

without the $current_user argument 

But at construct

protected ?AccountInterface $currentUser = NULL
alexpott’s picture

@nikolay shapovalov I committed a quick follow-up to fix this.

  • alexpott committed 91de3017 on 11.x
    Issue #3039354 follow-up by nikolay shapovalov: The current user views...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.