nodeStorage = $entity_manager->getStorage('node'); $this->config = $config_factory->get('islandora_breadcrumbs.breadcrumbs'); $this->utils = $utils; } /** * {@inheritdoc} */ public function applies(RouteMatchInterface $attributes) { // Using getRawParameters for consistency (always gives a // node ID string) because getParameters sometimes returns // a node ID string and sometimes returns a node object. $nid = $attributes->getRawParameters()->get('node'); if (!empty($nid)) { $node = $this->nodeStorage->load($nid); return (!empty($node) && $node->hasField($this->config->get('referenceField'))); } } /** * {@inheritdoc} */ public function build(RouteMatchInterface $route_match) { $nid = $route_match->getRawParameters()->get('node'); $node = $this->nodeStorage->load($nid); $breadcrumb = new Breadcrumb(); $breadcrumb->addLink(Link::createFromRoute($this->t('Home'), '')); $chain = array_reverse($this->utils->findAncestors($node, [$this->config->get('referenceField')], $this->config->get('maxDepth'))); // XXX: Handle a looping breadcrumb scenario by filtering the present // node out and then optionally re-adding it after if set to do so. $chain = array_filter($chain, function ($link) use ($nid) { return $link !== $nid; }); if ($this->config->get('includeSelf')) { array_push($chain, $node); } $breadcrumb->addCacheableDependency($node); // Add membership chain to the breadcrumb. foreach ($chain as $chainlink) { $node = $this->nodeStorage->load($chainlink); $breadcrumb->addCacheableDependency($node); $breadcrumb->addLink($node->toLink()); } $breadcrumb->addCacheContexts(['route']); return $breadcrumb; } }