Currently, we can filter a view based on the current viewed user or node entity's uid.

There is a lot of contribs which define entity classes extending Drupal\Core\Entity\ContentEntityBase class. So, they all have getOwnerId() method. For example, commerce module which has product and stores entities or profile module's profile entity.

Suggestion: allow those entities to use "User ID from route context" contextual filter.

Issue fork drupal-2904908

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

drugan created an issue. See original summary.

drugan’s picture

There is an urgent need on drupalcommerce users for this feature.

zenimagine’s picture

Thank you very much, it works

But I encounter another small problem.

1) I want to exclude the node or product from the current page. So I added to my view a pop-up filter to exclude the ID.

It would be interesting to replace:
"Identifier (ID) of the content from the url"
by :
"Identifier (ID) of the entity from the url"

2) I installed the "Commerce" and "Group" modules.

In contextual filters, I have:
- Identifier (ID) of the content from the URL.
- Group ID from URL.
- Store ID from the current store.

Why not group these 3 filters in:
- Identifier (ID) of the entity from the URL.

zenimagine’s picture

The patch works for me, but it does not work with the profile module page.

Every user on my site has a personal profile. I want to display a block with the nodes of the same author as the current profile.

Thank you for your help.

zenimagine’s picture

Sorry I've been searching for it, the patch is working properly ;-)

I hope it will be applied for the next update.

punamshelke’s picture

Status: Needs review » Reviewed & tested by the community

This patch is working for me.......
Thanks

alexpott’s picture

Version: 8.4.x-dev » 8.5.x-dev
Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs tests

@drugan thanks for working on this issue. This patch needs a test that proves it works for entities other than nodes.

Also...

  1. +++ b/core/modules/user/src/Plugin/views/argument_default/User.php
    @@ -92,10 +92,12 @@ public function getArgument() {
    +      $entity = reset($bag);
    

    Will the entity always be the first route parameter? That seems a big assumption.

  2. +++ b/core/modules/user/src/Plugin/views/argument_default/User.php
    @@ -92,10 +92,12 @@ public function getArgument() {
    +      if (method_exists($entity, 'getOwnerId')) {
    

    Here we're are assuming we know what the getOwnerId() method does and returns. But this is not at all enforced. We need to check whether or not the parameter implements \Drupal\user\EntityOwnerInterface - then getOwnerId() is safe to call.

drugan’s picture

Did not found a test for existing Drupal\user\Plugin\views\argument_default\User::getArgument() method. So I've decided that it will be useful to cover getting argument also for User entity (not only for changes on the patch).

drugan’s picture

Status: Needs work » Needs review
bojanz’s picture

In general this looks good.

-      '#title' => $this->t('Also look for a node and use the node author'),
+      '#title' => $this->t('Also look for an entity and use the entity author'),

Are we fine with saying "entity" in the UI? I remember that it was discouraged before, but I guess that ship sailed a long time ago.

+    $parameter_bag = $this->getMockBuilder('Symfony\Component\HttpFoundation\ParameterBag')
+      ->disableOriginalConstructor()
+      ->getMock();

Do we still allow non-Prophecy mocking to be added in new tests?

drugan’s picture

Are we fine with saying "entity" in the UI?

Yes, it may sound unclear for non-developer users but what else? Revert to "node" wording? No, it may mislead even developers because they can think that this work only for node entities. Needs help from English native speakers.

Do we still allow non-Prophecy mocking to be added in new tests?

It was my first experience with unit test, so I can imagine that it may look weird. A bit later I'll try another version of the patch using prophecy mocking.

dawehner’s picture

I like the general feature. It is always nice to get rid of hardcoded stuff for node, especially because the label "User ID from route context" totally indicate that already.

+++ b/core/modules/user/tests/src/Unit/Views/Argument/UserIdTest.php
@@ -0,0 +1,82 @@
+ * @coversDefaultClass \Drupal\user\Plugin\views\argument_default\User
+ * @group user
+ */
+class UserIdTest extends UnitTestCase {

I think I would have written a kernel test rather.

drugan’s picture

As it were suggested by @dawehner the 'Also look for a node and use the node author' option removed altogether and a new kernel test created.

boazpoolman’s picture

This works great!
Only thing is that I can't use the Product ID contextual filter to exclude a product if it is the same as the current product.
Using 'Content ID from URL'. Just like #3.

drugan’s picture

@boazpoolman

Note that current patch has nothing to do with Content ID or Product ID from url. Instead it fetches User ID from whatever entity which will be found in the route context of the current page and filters the view result based on this ID. It may be any entity: node, product, profile, store, etc.. That means all the content displayed by the view will be authored by the same user as the author of those entities. If you want to negate the result of the view and display the content which is authored by any user excerpt the author of the entity in the route context you need to scroll down in the contextual filter settings form and tick Exclude checkbox.

If you have created Products I'd recommend for testing purposes to import the 2-VIEW_author_products_and_nodes.txt view from the Drupal Commerce related issue:

#2885448: How do I display the products and nodes of the same author in the current page ?

How to:

Create three test users who are authors of a some number of product and node entities.

Go to admin/config/development/configuration/single/import Configuration type -> View

Paste the content of the file above and click Import.

Go to admin/structure/block and place the Author products and nodes block into any region (Sidebar first?).

Go to whatever content authored by the users and check that only content for the current entity author is displayed.

Go to admin/structure/views/view/author_products_and_nodes/edit/block_1 and Exclude (negate) the User ID form route context default value for the Product: Author contextual filter.

Go to whatever content authored by the users and check that content for all but not the current entity author is displayed.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Ralf Eisler’s picture

This patch works great. Use it on nodes.

Thank you very much.

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

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

zenimagine’s picture

The patch works with 8.6

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

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

zenimagine’s picture

Hi guys, it's been 2 years since I applied the patch on my site and it works for me. Do you think that one day it will be applied to the kernel ?

Thank you

drugan’s picture

Assigned: Unassigned » drugan

In the next few days I'll try re-roll the patch to the 8.8 version.

drugan’s picture

Assigned: drugan » Unassigned
StatusFileSize
new10.48 KB

Done.

zenimagine’s picture

thanks, the patch #23 works for me

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

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

zenimagine’s picture

Hi, do you know if the patch will finally be applied to drupal 8.8 ?

zenimagine’s picture

@drugan Hi, was the patch applied to the kernel? If I test a blank installation on simplytest, there is the parameter user ID from the context of the route.

Drupal 8.8.2

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

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now 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: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

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

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.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.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.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.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now 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.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now 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.

pooja saraah’s picture

StatusFileSize
new10.76 KB
new2.59 KB

Fixed failed commands on #23
Attached patch against Drupal 10.1.x

needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new2.82 KB

The Needs Review Queue Bot tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

Bhanu951 made their first commit to this issue’s fork.

bhanu951’s picture

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative, +Needs steps to reproduce

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.

This could use an issue summary update using the default template. Steps to reproduce? Was the suggestion what was used in the solution?

Taking this setting away seems like it could affect existing views. If so they'll need upgrade paths.

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.

tyler-durden’s picture

Hours of searching, and I keep on finding this post so I need to ask some questions.

I'm converting my site from Drupal 7 to 10, where on D7 I had multiple views with a contextual filter that properly got the userid from the node the block was on, so I could show other nodes from this author. I have spent hours trying to get it to work, is this post stating this is not working in Drupal 10+?

I have the option, "Also look for a node and use the node author" checked so I'd assume since that option is in there it should be working? This seems to be a feature I'd think many sites would need, so maybe I'm just not doing something right?

I'd try this patch, but it looks like it has issues now and I'm on 10.1. If this is the patch to try I can help test, as I'm not a programmer but I will help how I can.

tyler-durden’s picture

I found a work around, instead of basing my view on node/content, I based it on users and used the authour/user relationship to pull nodes and it works great.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.