|
|
|
@ -4,12 +4,12 @@ namespace Drupal\islandora_breadcrumbs;
|
|
|
|
|
|
|
|
|
|
use Drupal\Core\Config\ConfigFactoryInterface; |
|
|
|
|
use Drupal\Core\Entity\EntityTypeManagerInterface; |
|
|
|
|
use Drupal\Core\Entity\EntityInterface; |
|
|
|
|
use Drupal\Core\Breadcrumb\Breadcrumb; |
|
|
|
|
use Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface; |
|
|
|
|
use Drupal\Core\Link; |
|
|
|
|
use Drupal\Core\Routing\RouteMatchInterface; |
|
|
|
|
use Drupal\Core\StringTranslation\StringTranslationTrait; |
|
|
|
|
use Drupal\islandora\IslandoraUtils; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Provides breadcrumbs for nodes using a configured entity reference field. |
|
|
|
@ -31,6 +31,13 @@ class IslandoraBreadcrumbBuilder implements BreadcrumbBuilderInterface {
|
|
|
|
|
*/ |
|
|
|
|
protected $nodeStorage; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Islandora utils. |
|
|
|
|
* |
|
|
|
|
* @var \Drupal\islandora\IslandoraUtils |
|
|
|
|
*/ |
|
|
|
|
protected $utils; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Constructs a breadcrumb builder. |
|
|
|
|
* |
|
|
|
@ -38,10 +45,13 @@ class IslandoraBreadcrumbBuilder implements BreadcrumbBuilderInterface {
|
|
|
|
|
* Storage to load nodes. |
|
|
|
|
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory |
|
|
|
|
* The configuration factory. |
|
|
|
|
* @param \Drupal\islandora\IslandoraUtils $utils |
|
|
|
|
* Islandora utils service. |
|
|
|
|
*/ |
|
|
|
|
public function __construct(EntityTypeManagerInterface $entity_manager, ConfigFactoryInterface $config_factory) { |
|
|
|
|
public function __construct(EntityTypeManagerInterface $entity_manager, ConfigFactoryInterface $config_factory, IslandoraUtils $utils) { |
|
|
|
|
$this->nodeStorage = $entity_manager->getStorage('node'); |
|
|
|
|
$this->config = $config_factory->get('islandora_breadcrumbs.breadcrumbs'); |
|
|
|
|
$this->utils = $utils; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -68,49 +78,20 @@ class IslandoraBreadcrumbBuilder implements BreadcrumbBuilderInterface {
|
|
|
|
|
$breadcrumb = new Breadcrumb(); |
|
|
|
|
$breadcrumb->addLink(Link::createFromRoute($this->t('Home'), '<front>')); |
|
|
|
|
|
|
|
|
|
$chain = []; |
|
|
|
|
$this->walkMembership($node, $chain); |
|
|
|
|
|
|
|
|
|
if (!$this->config->get('includeSelf')) { |
|
|
|
|
array_pop($chain); |
|
|
|
|
$chain = array_reverse($this->utils->findAncestors($node, [$this->config->get('referenceField')], 1)); |
|
|
|
|
if ($this->config->get('includeSelf')) { |
|
|
|
|
array_push($chain, $node); |
|
|
|
|
} |
|
|
|
|
$breadcrumb->addCacheableDependency($node); |
|
|
|
|
|
|
|
|
|
// Add membership chain to the breadcrumb. |
|
|
|
|
foreach ($chain as $chainlink) { |
|
|
|
|
$breadcrumb->addCacheableDependency($chainlink); |
|
|
|
|
$breadcrumb->addLink($chainlink->toLink()); |
|
|
|
|
$node = $this->nodeStorage->load($chainlink); |
|
|
|
|
$breadcrumb->addCacheableDependency($node); |
|
|
|
|
$breadcrumb->addLink($node->toLink()); |
|
|
|
|
} |
|
|
|
|
$breadcrumb->addCacheContexts(['route']); |
|
|
|
|
return $breadcrumb; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Follows chain of field_member_of links. |
|
|
|
|
* |
|
|
|
|
* We pass crumbs by reference to enable checking for looped chains. |
|
|
|
|
*/ |
|
|
|
|
protected function walkMembership(EntityInterface $entity, &$crumbs) { |
|
|
|
|
// Avoid infinate loops, return if we've seen this before. |
|
|
|
|
foreach ($crumbs as $crumb) { |
|
|
|
|
if ($crumb->uuid == $entity->uuid) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Add this item onto the pile. |
|
|
|
|
array_unshift($crumbs, $entity); |
|
|
|
|
|
|
|
|
|
if ($this->config->get('maxDepth') > 0 && count($crumbs) >= $this->config->get('maxDepth')) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Find the next in the chain, if there are any. |
|
|
|
|
if ($entity->hasField($this->config->get('referenceField')) && |
|
|
|
|
!$entity->get($this->config->get('referenceField'))->isEmpty() && |
|
|
|
|
$entity->get($this->config->get('referenceField'))->entity instanceof EntityInterface) { |
|
|
|
|
$this->walkMembership($entity->get($this->config->get('referenceField'))->entity, $crumbs); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|