Commit fc828d8a authored by Brooke Mahoney's avatar Brooke Mahoney
Browse files

fix: #3624580 Adds cache tags for taxonomy terms.

parent eecf3745
Loading
Loading
Loading
Loading
Loading
+35 −1
Changes for easy_breadcrumb.module: 35 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -2,9 +2,13 @@

/**
 * @file
 * The Easy Breadcrumb module improves the core system breadcrumbs.
 * The Easy Breadcrumb module improves the core system breadcrumb.
 */

use Drupal\Core\Cache\Cache;
use Drupal\easy_breadcrumb\EasyBreadcrumbConstants;
use Drupal\taxonomy\Entity\Term;

/**
 * Implements hook_help().
 */
@@ -63,3 +67,33 @@ function easy_breadcrumb_preprocess_block(&$variables) {
    }
  }
}

/**
 * Implements hook_ENTITY_TYPE_update().
 *
 * If the TERM_HIERARCHY configuration option is set, then expire the breadcrumb
 * cache for all child terms.
 */
function easy_breadcrumb_taxonomy_term_update(Term $term) {
  // Returns early if the TERM_HIERARCHY configuration option is not set.
  $config = \Drupal::config('easy_breadcrumb.settings');
  if (!$config->get(EasyBreadcrumbConstants::TERM_HIERARCHY)) {
    return;
  }

  /**
   * @var \Drupal\taxonomy\TermStorageInterface $storage
   */
  $storage = \Drupal::entityTypeManager()->getStorage('taxonomy_term');
  $vid = $term->bundle();
  $tree = $storage->loadTree($vid, $term->id());
  $tags = [];

  foreach ($tree as $tree_term) {
    $tags[] = 'easy_breadcrumb:taxonomy_term:' . $tree_term->tid;
  }

  if ($tags) {
    Cache::invalidateTags($tags);
  }
}
+3 −0
Changes for src/EasyBreadcrumbBuilder.php: 3 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -665,15 +665,18 @@ class EasyBreadcrumbBuilder implements BreadcrumbBuilderInterface {
          if ($i == 0
              && $this->config->get(EasyBreadcrumbConstants::TERM_HIERARCHY)
              && $term = $route_match->getParameter('taxonomy_term')) {
            $breadcrumb->addCacheTags(['easy_breadcrumb:taxonomy_term:' . $term->id()]);
            $parents = $this->entityTypeManager->getStorage('taxonomy_term')->loadAllParents($term->id());

            // Unset current term.
            array_shift($parents);
            foreach ($parents as $parent) {
              if ($parent->access('view')) {
                $parent = $this->entityRepository->getTranslationFromContext($parent);
                $links[] = $parent->toLink();
              }
            }
          }
          unset($title);
          $i++;
        }
+38 −9
Changes for tests/src/Functional/EasyBreadcrumbTermHierarchyTest.php: 38 added lines, 9 removed lines.
Original line number Diff line number Diff line
@@ -24,6 +24,21 @@ class EasyBreadcrumbTermHierarchyTest extends EasyBreadcrumbBrowserTestBase {
    'taxonomy',
  ];

  /**
   * The Parent Term.
   */
  protected Term $parent;

  /**
   * The Child Term.
   */
  protected Term $child;

  /**
   * The Grandchild Term.
   */
  protected Term $grandchild;

  /**
   * {@inheritdoc}
   */
@@ -32,28 +47,28 @@ class EasyBreadcrumbTermHierarchyTest extends EasyBreadcrumbBrowserTestBase {
    $vocabularyId = 'tags';
    Vocabulary::create(['vid' => $vocabularyId])->save();

    $parent = Term::create([
    $this->parent = Term::create([
      'name' => 'Parent',
      'vid' => $vocabularyId,
    ]);
    $parent->save();
    $this->parent->save();

    $child = Term::create([
    $this->child = Term::create([
      'name' => 'Child',
      'vid' => $vocabularyId,
      'parent' => [$parent->id()],
      'parent' => [$this->parent->id()],
    ]);
    $child->save();
    $this->child->save();

    $grandchild = Term::create([
    $this->grandchild = Term::create([
      'name' => 'Grandchild',
      'vid' => $vocabularyId,
      'parent' => [$child->id()],
      'parent' => [$this->child->id()],
    ]);
    $grandchild->save();
    $this->grandchild->save();

    $this->easyBreadcrumbSetConfig(EasyBreadcrumbConstants::TERM_HIERARCHY, TRUE);
    $this->drupalGet($grandchild->toUrl());
    $this->drupalGet($this->grandchild->toUrl());
  }

  /**
@@ -65,4 +80,18 @@ class EasyBreadcrumbTermHierarchyTest extends EasyBreadcrumbBrowserTestBase {
    $this->easyBreadcrumbAssertSegmentTextEquals(4, 'Grandchild');
  }

  /**
   * Tests access to crumbs of unpublished terms.
   */
  public function testEasyBreadcrumbTermHierarchyAccess() {
    $this->parent->setUnpublished()->save();
    $this->drupalGet($this->grandchild->toUrl());
    $this->easyBreadcrumbAssertSegmentTextEquals(2, 'Child');
    $this->easyBreadcrumbAssertSegmentTextEquals(3, 'Grandchild');

    $this->child->setUnpublished()->save();
    $this->drupalGet($this->grandchild->toUrl());
    $this->easyBreadcrumbAssertSegmentTextEquals(2, 'Grandchild');
  }

}