HierarchyBreadcrumbBuilder.php in Entity Reference Hierarchy 8
File
src/HierarchyBreadcrumbBuilder.php
View source
<?php
namespace Drupal\entity_hierarchy;
use Drupal\Core\Access\AccessManagerInterface;
use Drupal\Core\Breadcrumb\Breadcrumb;
use Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Link;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\node\NodeInterface;
class HierarchyBreadcrumbBuilder implements BreadcrumbBuilderInterface {
use StringTranslationTrait;
public function applies(RouteMatchInterface $route_match) {
$node = $route_match
->getParameter('node');
$parent = null;
if ($node instanceof NodeInterface) {
$current_nid = $node
->id();
$hierarchy_storage = \Drupal::service('entity_hierarchy.outline_storage');
$parent = $hierarchy_storage
->hierarchyGetParent($current_nid);
}
return !empty($parent) ? true : false;
}
public function build(RouteMatchInterface $route_match) {
$node = $route_match
->getParameter('node');
$nid = $node->nid->value;
$trail = $this
->hierarchyGetNodePrimaryAncestorNodes($nid);
if ($trail) {
$breadcrumb = new Breadcrumb();
$links = [
Link::createFromRoute($this
->t('Home'), '<front>'),
];
foreach ($trail as $node) {
$options = array();
$links[] = $node
->toLink($node
->getTitle(), 'canonical', $options);
}
$breadcrumb
->addCacheableDependency($node);
$breadcrumb
->setLinks($links);
$breadcrumb
->addCacheContexts([
'route',
]);
return $breadcrumb;
}
else {
return;
}
}
private function hierarchyGetNodePrimaryAncestorNodes($nid) {
return \Drupal\node\Entity\Node::loadMultiple($this
->hierarchyGetNodePrimaryAncestorNids($nid));
}
private function hierarchyGetNodePrimaryAncestorNids($nid) {
$out = array();
if ($parent_nid = $this
->hierarchyGetNodeParentPrimaryNid($nid)) {
$out = $this
->hierarchyGetNodePrimaryAncestorNids($parent_nid);
$out[] = $parent_nid;
}
return $out;
}
private function hierarchyGetNodeParentPrimaryNid($nid) {
$nids = $this
->hierarchyGetNodeParentNids($nid, 1);
return current($nids);
}
private function hierarchyGetNodeParentNids($node, $limit = NULL) {
$out = array();
foreach ($this
->hierarchyGetNodeParents($node, $limit) as $parent) {
$parent = (object) $parent;
$out[] = $parent->pnid;
}
return $out;
}
private function hierarchyGetNodeParents($node, $limit = NULL) {
$cnid = $node;
if (is_object($node)) {
if (isset($node->entity_hierarchy_parents)) {
return $node->entity_hierarchy_parents;
}
$cnid = $node->nid;
}
$out = array();
$db = \Drupal::database();
$query = $db
->select('entity_hierarchy', 'nh')
->fields('nh')
->where('cnid = :cnid', array(
':cnid' => $cnid,
))
->orderBy('pweight', 'ASC');
if ($limit) {
$query
->range(0, $limit);
}
$result = $query
->execute()
->fetchAll();
foreach ($result as $item) {
$out[] = $item;
}
return $out;
}
}