You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
97 lines
2.7 KiB
97 lines
2.7 KiB
6 years ago
|
<?php
|
||
|
|
||
|
namespace Drupal\islandora_breadcrumbs;
|
||
|
|
||
|
use Drupal\Core\Config\ConfigFactoryInterface;
|
||
|
use Drupal\Core\Entity\EntityInterface;
|
||
|
use Drupal\Core\Breadcrumb\Breadcrumb;
|
||
|
use Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface;
|
||
|
use Drupal\Core\Routing\RouteMatchInterface;
|
||
|
|
||
|
/**
|
||
|
* Provides breadcrumbs for nodes using a configured entity reference field.
|
||
|
*/
|
||
|
class IslandoraBreadcrumbBuilder implements BreadcrumbBuilderInterface {
|
||
|
|
||
|
/**
|
||
|
* The configuration.
|
||
|
*
|
||
|
* @var \Drupal\Core\Config\ImmutableConfig
|
||
|
*/
|
||
|
protected $config;
|
||
|
|
||
|
/**
|
||
|
* Constructs a breadcrumb builder.
|
||
|
*
|
||
|
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
|
||
|
* The configuration factory.
|
||
|
*/
|
||
|
public function __construct(ConfigFactoryInterface $config_factory) {
|
||
|
$this->config = $config_factory->get('islandora.breadcrumbs');
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* {@inheritdoc}
|
||
|
*/
|
||
|
public function applies(RouteMatchInterface $attributes) {
|
||
|
$parameters = $attributes->getParameters()->all();
|
||
|
if (!empty($parameters['node'])) {
|
||
|
return ($parameters['node']->hasField($this->config->get('referenceField')) &&
|
||
|
!$parameters['node']->get($this->config->get('referenceField'))->isEmpty());
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* {@inheritdoc}
|
||
|
*/
|
||
|
public function build(RouteMatchInterface $route_match) {
|
||
|
|
||
|
$node = $route_match->getParameter('node');
|
||
|
$breadcrumb = new Breadcrumb();
|
||
|
|
||
|
$chain = [];
|
||
|
$this->walkMembership($node, $chain);
|
||
|
|
||
|
if (!$this->config->get('includeSelf')) {
|
||
|
array_pop($chain);
|
||
|
}
|
||
|
$breadcrumb->addCacheableDependency($node);
|
||
|
|
||
|
// Add membership chain to the breadcrumb.
|
||
|
foreach ($chain as $chainlink) {
|
||
|
$breadcrumb->addCacheableDependency($chainlink);
|
||
|
$breadcrumb->addLink($chainlink->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()) {
|
||
|
$this->walkMembership($entity->get($this->config->get('referenceField'))->entity, $crumbs);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|