Unverified Commit 8df2a7d8 authored by Alex Pott's avatar Alex Pott
Browse files

fix: #3560357 Taxonomy term ID from URL ViewsArgumentDefault plugin does not...

fix: #3560357 Taxonomy term ID from URL ViewsArgumentDefault plugin does not apply node cache tags when "Load filter from node page" is selected

By: ericgsmith
By: smustgrave
(cherry picked from commit 8ebac7b2)
parent f78790cb
Loading
Loading
Loading
Loading
Loading
+4 −13
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@

use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
@@ -169,6 +170,9 @@ public function getArgument() {
    if (!empty($this->options['node'])) {
      // Just check, if a node could be detected.
      if (($node = $this->routeMatch->getParameter('node')) && $node instanceof NodeInterface) {
        CacheableMetadata::createFromRenderArray($this->view->element)
          ->merge(CacheableMetadata::createFromObject($node))
          ->applyTo($this->view->element);
        $taxonomy = [];
        foreach ($node->getFieldDefinitions() as $field) {
          if ($field->getType() == 'entity_reference' && $field->getSetting('target_type') == 'taxonomy_term') {
@@ -197,19 +201,6 @@ public function getArgument() {
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getCacheTags() {
    $tags = parent::getCacheTags();
    if (!empty($this->options['node'])) {
      if (($node = $this->routeMatch->getParameter('node')) && $node instanceof NodeInterface) {
        $tags = Cache::mergeTags($tags, $node->getCacheTags());
      }
    }
    return $tags;
  }

  /**
   * {@inheritdoc}
   */
+174 −0
Original line number Diff line number Diff line
langcode: en
status: true
dependencies:
  module:
    - taxonomy
    - user
id: test_argument_tid_from_node
label: test_argument_tid_from_node
module: views
description: ''
tag: ''
base_table: taxonomy_term_field_data
base_field: tid
display:
  default:
    id: default
    display_title: Default
    display_plugin: default
    position: 0
    display_options:
      title: test_argument_tid_from_node_block_1
      fields:
        name:
          id: name
          table: taxonomy_term_field_data
          field: name
          relationship: none
          group_type: group
          admin_label: ''
          entity_type: taxonomy_term
          entity_field: name
          plugin_id: term_name
          label: ''
          exclude: false
          alter:
            alter_text: false
            make_link: false
            absolute: false
            word_boundary: false
            ellipsis: false
            strip_tags: false
            trim: false
            html: false
          element_type: ''
          element_class: ''
          element_label_type: ''
          element_label_class: ''
          element_label_colon: false
          element_wrapper_type: ''
          element_wrapper_class: ''
          element_default_classes: true
          empty: ''
          hide_empty: false
          empty_zero: false
          hide_alter_empty: true
          click_sort_column: value
          type: string
          settings:
            link_to_entity: false
          group_column: value
          group_columns: {  }
          group_rows: true
          delta_limit: 0
          delta_offset: 0
          delta_reversed: false
          delta_first_last: false
          multi_type: separator
          separator: ', '
          field_api_classes: false
          convert_spaces: false
      pager:
        type: none
        options:
          offset: 0
      exposed_form:
        type: basic
        options:
          submit_button: Apply
          reset_button: false
          reset_button_label: Reset
          exposed_sorts_label: 'Sort by'
          expose_sort_order: true
          sort_asc_label: Asc
          sort_desc_label: Desc
      access:
        type: perm
        options:
          perm: 'access content'
      cache:
        type: tag
        options: {  }
      empty: {  }
      sorts: {  }
      arguments:
        tid:
          id: tid
          table: taxonomy_term_field_data
          field: tid
          relationship: none
          group_type: group
          admin_label: ''
          entity_type: taxonomy_term
          entity_field: tid
          plugin_id: taxonomy
          default_action: default
          exception:
            value: all
            title_enable: false
            title: All
          title_enable: false
          title: ''
          default_argument_type: taxonomy_tid
          default_argument_options:
            term_page: '0'
            node: true
            limit: false
            vids: {  }
            anyall: +
          summary_options:
            base_path: ''
            count: true
            override: false
            items_per_page: 25
          summary:
            sort_order: asc
            number_of_records: 0
            format: default_summary
          specify_validation: false
          validate:
            type: none
            fail: 'not found'
          validate_options: {  }
          break_phrase: false
          not: false
      filters: {  }
      style:
        type: default
      row:
        type: fields
      query:
        type: views_query
        options:
          query_comment: ''
          disable_sql_rewrite: false
          distinct: false
          replica: false
          query_tags: {  }
      relationships: {  }
      header: {  }
      footer: {  }
      display_extenders: {  }
    cache_metadata:
      max-age: -1
      contexts:
        - 'languages:language_content'
        - 'languages:language_interface'
        - url
        - user.permissions
      tags: {  }
  block_1:
    id: block_1
    display_title: Block
    display_plugin: block
    position: 1
    display_options:
      display_extenders: {  }
    cache_metadata:
      max-age: -1
      contexts:
        - 'languages:language_content'
        - 'languages:language_interface'
        - url
        - user.permissions
      tags: {  }
+67 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\Tests\taxonomy\Functional\Views;

use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;

/**
 * Tests the taxonomy term with argument from a node page.
 */
#[Group('taxonomy')]
#[RunTestsInSeparateProcesses]
class TaxonomyTermArgumentTidFromNodeTest extends TaxonomyTestBase {

  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'block',
    'node',
    'taxonomy',
    'taxonomy_test_views',
    'views',
  ];

  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';

  /**
   * {@inheritdoc}
   */
  public static $testViews = ['test_argument_tid_from_node'];

  /**
   * {@inheritdoc}
   */
  protected function setUp($import_test_views = TRUE, $modules = []): void {
    parent::setUp($import_test_views, $modules);

    // Remove the term field from the display, replace with the view.
    /** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
    $display_repository = \Drupal::service('entity_display.repository');
    $display_repository->getViewDisplay('node', 'article')
      ->removeComponent('field_views_testing_tags')
      ->save();

    $this->drupalPlaceBlock('views_block:test_argument_tid_from_node-block_1');
  }

  /**
   * Tests cache invalidation.
   */
  public function testCacheInvalidation(): void {
    $this->drupalGet($this->nodes[0]->toUrl());
    $this->assertSession()->pageTextContains($this->term1->label());

    $this->nodes[0]->set('field_views_testing_tags', [])->save();

    $this->drupalGet($this->nodes[0]->toUrl());
    $this->assertSession()->pageTextNotContains($this->term1->label());
  }

}
+0 −3
Original line number Diff line number Diff line
@@ -63,7 +63,6 @@ public function testNodePath(): void {

    $expected = implode(',', [$this->term1->id(), $this->term2->id()]);
    $this->assertEquals($expected, $view->argument['tid']->getDefaultArgument());
    $this->assertEquals($this->nodes[0]->getCacheTags(), $view->argument['tid']->getPlugin('argument_default')->getCacheTags());
    $view->destroy();
  }

@@ -89,7 +88,6 @@ public function testNodePathWithViewSelection(): void {

    $expected = implode(',', [$this->term1->id(), $this->term2->id()]);
    $this->assertEquals($expected, $view->argument['tid']->getDefaultArgument());
    $this->assertEquals($this->nodes[0]->getCacheTags(), $view->argument['tid']->getPlugin('argument_default')->getCacheTags());
  }

  /**
@@ -100,7 +98,6 @@ public function testTermPath(): void {

    $expected = $this->term1->id();
    $this->assertEquals($expected, $view->argument['tid']->getDefaultArgument());
    $this->assertEmpty($view->argument['tid']->getPlugin('argument_default')->getCacheTags());
  }

}