HierarchyBasedBreadcrumbBuilder.php in Entity Reference Hierarchy 8.2
File
modules/entity_hierarchy_breadcrumb/src/HierarchyBasedBreadcrumbBuilder.php
View source
<?php
namespace Drupal\entity_hierarchy_breadcrumb;
use Drupal\Core\Breadcrumb\Breadcrumb;
use Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Link;
use Drupal\Core\Routing\AdminContext;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\entity_hierarchy\Storage\EntityTreeNodeMapperInterface;
use Drupal\entity_hierarchy\Storage\NestedSetNodeKeyFactory;
use Drupal\entity_hierarchy\Storage\NestedSetStorageFactory;
use Symfony\Component\Routing\Route;
class HierarchyBasedBreadcrumbBuilder implements BreadcrumbBuilderInterface {
protected $storageFactory;
protected $nodeKeyFactory;
protected $mapper;
protected $entityFieldManager;
protected $adminContext;
public function __construct(NestedSetStorageFactory $storage_factory, NestedSetNodeKeyFactory $node_key_factory, EntityTreeNodeMapperInterface $mapper, EntityFieldManagerInterface $entity_field_manager, AdminContext $admin_context) {
$this->storageFactory = $storage_factory;
$this->nodeKeyFactory = $node_key_factory;
$this->mapper = $mapper;
$this->entityFieldManager = $entity_field_manager;
$this->adminContext = $admin_context;
}
public function applies(RouteMatchInterface $route_match) {
if ($this->adminContext
->isAdminRoute($route_match
->getRouteObject())) {
return FALSE;
}
$route_entity = $this
->getEntityFromRouteMatch($route_match);
if (!$route_entity || !$route_entity instanceof ContentEntityInterface || !$this
->getHierarchyFieldFromEntity($route_entity)) {
return FALSE;
}
return TRUE;
}
public function build(RouteMatchInterface $route_match) {
$breadcrumb = new Breadcrumb();
$breadcrumb
->addCacheContexts([
'route',
]);
$route_entity = $this
->getEntityFromRouteMatch($route_match);
$entity_type = $route_entity
->getEntityTypeId();
$storage = $this->storageFactory
->get($this
->getHierarchyFieldFromEntity($route_entity), $entity_type);
$ancestors = $storage
->findAncestors($this->nodeKeyFactory
->fromEntity($route_entity));
$ancestor_entities = $this->mapper
->loadAndAccessCheckEntitysForTreeNodes($entity_type, $ancestors, $breadcrumb);
$links = [];
foreach ($ancestor_entities as $ancestor_entity) {
if (!$ancestor_entities
->contains($ancestor_entity)) {
continue;
}
$entity = $ancestor_entities
->offsetGet($ancestor_entity);
if ($entity
->id() == $route_entity
->id()) {
$links[] = Link::createFromRoute($entity
->label(), '<none>');
}
else {
$links[] = $entity
->toLink();
}
}
array_unshift($links, Link::createFromRoute(new TranslatableMarkup('Home'), '<front>'));
$breadcrumb
->setLinks($links);
return $breadcrumb;
}
protected function getEntityTypeFromRoute(Route $route) {
if (!empty($route
->getOptions()['parameters'])) {
foreach ($route
->getOptions()['parameters'] as $option) {
if (isset($option['type']) && strpos($option['type'], 'entity:') === 0) {
return substr($option['type'], strlen('entity:'));
}
}
}
return NULL;
}
protected function getEntityFromRouteMatch(RouteMatchInterface $route_match) {
$route = $route_match
->getRouteObject();
if (!$route) {
return NULL;
}
$entity_type_id = $this
->getEntityTypeFromRoute($route);
if ($entity_type_id) {
return $route_match
->getParameter($entity_type_id);
}
return NULL;
}
protected function getHierarchyFieldFromEntity(ContentEntityInterface $entity) {
$fields = $this->entityFieldManager
->getFieldMapByFieldType('entity_reference_hierarchy');
$entity_type = $entity
->getEntityTypeId();
if (isset($fields[$entity_type])) {
foreach ($fields[$entity_type] as $field_name => $detail) {
if (!empty($detail['bundles'][$entity
->bundle()])) {
return $field_name;
}
}
}
return NULL;
}
}