It looks like in midst of porting this module to Drupal 8, the condition for checking the entity type in the subquery for the views argument handler was missed (see https://git.drupalcode.org/project/taxonomy_entity_index/-/commit/4d25cc...) even though the same condition was refactored and preserved in the views filter handler (see https://git.drupalcode.org/project/taxonomy_entity_index/-/commit/4d25cc...). Without checking the entity type, the contextual filter picks up seemingly unrelated entities.

Take the scenerio where media and content both have field_tags that use the Tags taxonomy. Also, where node:123 is tagged with your target term, and media:123 is not tagged with that term. If you have a view of all media using this term, media:123 will be included despite not being tagged with the term because 123 is an entity id in the subquery for the node (node:123) that is tagged with the term.

I could replicate it in a vanilla site with the following steps:

  • In simplytest.me, enable media and this module.
  • Add the tags field to the image media.
  • Configure module to index both content and media.
  • Created a node with nid = 1 with a tag (with tid = 2).
  • Created media with mid = 1 without a tag.
  • Create a view of all media.
  • Add the contextual filter "Taxonomy Entity Index: Has taxonomy term ID on Media (with depth and indexed in taxonomy_entity_index)", and set the depth to 1 or more.
  • View should be empty with the argument of 2, but media:1 is shown even though it has no tag.

Adding the below code to the argument handler (as in the case in /src/Plugin/views/filter/TaxonomyEntityIndexTidDepth.php:127) appears to resolve this issue:

    if (isset($this->baseTableInfo['table']['entity type'])) {
      $subquery->condition('entity_type', $this->baseTableInfo['table']['entity type']);
    }

Comments

cainaru created an issue. See original summary.

cainaru’s picture

Here is a patch that adds the condition to the argument handler.

godotislate’s picture

Status: Active » Needs review
larowlan’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

Thanks for this!
We have some tests in the module now, could you expand the existing test to demonstrate the bug. Thanks

cainaru’s picture

@larowlan Sure! I've attached two patches—one is a test-only patch to demonstrate the bug, and the other patch is the full patch that includes both tests and the fix.

godotislate’s picture

Status: Needs review » Reviewed & tested by the community

+1 RTBC

larowlan’s picture

This is looking great, only one minor change required and this can go in.

+++ b/tests/src/Functional/Views/TaxonomyEntityIndexTermArgumentDepthTest.php
@@ -0,0 +1,137 @@
+ * @group taxonomy

this needs to be taxonomy_entity_index

Thanks @cainaru, great work

cainaru’s picture

@larowlan Thanks! Attached is a patch updated to use @group taxonomy_entity_index, as well as an interdiff showing the changes between #5 and #8.

  • larowlan committed 8e680dd on 8.x-1.x authored by cainaru
    Issue #3133815 by cainaru: Add missing entity_type condition in subquery...
larowlan’s picture

Status: Reviewed & tested by the community » Fixed
Issue tags: -Needs tests

Cutting 1.4 tag with this in it, thanks!

Status: Fixed » Closed (fixed)

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