You are here

public function MenuItemExtrasMenuLinkContent::getCacheTagsToInvalidate in Menu Item Extras 8.2

Returns the cache tags that should be used to invalidate caches.

This will not return additional cache tags added through addCacheTags().

Return value

string[] Set of cache tags.

Overrides EntityBase::getCacheTagsToInvalidate

See also

\Drupal\Core\Cache\RefinableCacheableDependencyInterface::addCacheTags()

\Drupal\Core\Cache\CacheableDependencyInterface::getCacheTags()

File

src/Entity/MenuItemExtrasMenuLinkContent.php, line 40

Class

MenuItemExtrasMenuLinkContent

Namespace

Drupal\menu_item_extras\Entity

Code

public function getCacheTagsToInvalidate() {
  $tags = parent::getCacheTagsToInvalidate();
  $parent_id = $this
    ->getParentId();
  if ($this
    ->isNew() && $parent_id) {

    // Need to invalidate the parent to clear the render cache generated
    // in MenuLinkTreeHandler::getMenuLinkItemContent().
    if (strpos($parent_id, self::MENU_LINK_CONTENT_PREFIX) === 0) {
      $parent_uuid = substr($parent_id, strlen(self::MENU_LINK_CONTENT_PREFIX));

      /** @var \Drupal\Core\Entity\ContentEntityStorageInterface $storage */
      $storage = $this
        ->entityTypeManager()
        ->getStorage($this
        ->getEntityTypeId());
      $loaded_entities = $storage
        ->loadByProperties([
        'uuid' => $parent_uuid,
      ]);
      $parent = reset($loaded_entities);
      if ($parent) {
        return Cache::mergeTags($tags, [
          self::MENU_LINK_CONTENT_PREFIX . $parent
            ->id(),
        ]);
      }
    }
    else {

      /** @var \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager */
      $menu_link_manager = \Drupal::service('plugin.manager.menu.link');
      if ($menu_link_manager
        ->hasDefinition($parent_id)) {

        /** @var \Drupal\Core\Menu\MenuLinkInterface $parent */
        $parent = $menu_link_manager
          ->createInstance($parent_id);
        if ($parent) {
          return Cache::mergeTags($tags, $parent
            ->getCacheTags());
        }
      }
    }
  }
  return $tags;
}