class HierarchyBreadcrumbBuilder in Entity Reference Hierarchy 8
Provides a breadcrumb builder for nodes in a book.
Hierarchy
- class \Drupal\entity_hierarchy\HierarchyBreadcrumbBuilder implements BreadcrumbBuilderInterface uses StringTranslationTrait
Expanded class hierarchy of HierarchyBreadcrumbBuilder
1 string reference to 'HierarchyBreadcrumbBuilder'
1 service uses HierarchyBreadcrumbBuilder
File
- src/
HierarchyBreadcrumbBuilder.php, line 23 - Contains \Drupal\entity_hierarchy\HierarchyBreadcrumbBuilder.
Namespace
Drupal\entity_hierarchyView source
class HierarchyBreadcrumbBuilder implements BreadcrumbBuilderInterface {
use StringTranslationTrait;
/**
* {@inheritdoc}
*/
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;
}
/**
* {@inheritdoc}
*/
public function build(RouteMatchInterface $route_match) {
// Get all the possible breadcrumbs for the node.
$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;
}
}
/**
* Get the parent nodes for the given node.
*/
private function hierarchyGetNodePrimaryAncestorNodes($nid) {
return \Drupal\node\Entity\Node::loadMultiple($this
->hierarchyGetNodePrimaryAncestorNids($nid));
}
/**
* Get the ancestor nodes for the given node.
*
* @TODO: make this more efficient by implementing a materialized path or similar.
*/
private function hierarchyGetNodePrimaryAncestorNids($nid) {
$out = array();
if ($parent_nid = $this
->hierarchyGetNodeParentPrimaryNid($nid)) {
$out = $this
->hierarchyGetNodePrimaryAncestorNids($parent_nid);
$out[] = $parent_nid;
}
return $out;
}
/**
* Get the primary parent nid for the given node.
*/
private function hierarchyGetNodeParentPrimaryNid($nid) {
$nids = $this
->hierarchyGetNodeParentNids($nid, 1);
return current($nids);
}
/**
* Get the parent nodes for the given node.
*/
private function hierarchyGetNodeParentNids($node, $limit = NULL) {
$out = array();
foreach ($this
->hierarchyGetNodeParents($node, $limit) as $parent) {
// This may be an array after a node has been submitted.
$parent = (object) $parent;
$out[] = $parent->pnid;
}
return $out;
}
/**
* Get all the parents for the given node.
*/
private function hierarchyGetNodeParents($node, $limit = NULL) {
$cnid = $node;
// If a node object was passed, then the parents may already have been loaded.
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;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
HierarchyBreadcrumbBuilder:: |
public | function |
Whether this breadcrumb builder should be used to build the breadcrumb. Overrides BreadcrumbBuilderInterface:: |
|
HierarchyBreadcrumbBuilder:: |
public | function |
Builds the breadcrumb. Overrides BreadcrumbBuilderInterface:: |
|
HierarchyBreadcrumbBuilder:: |
private | function | Get the parent nodes for the given node. | |
HierarchyBreadcrumbBuilder:: |
private | function | Get the primary parent nid for the given node. | |
HierarchyBreadcrumbBuilder:: |
private | function | Get all the parents for the given node. | |
HierarchyBreadcrumbBuilder:: |
private | function | Get the ancestor nodes for the given node. | |
HierarchyBreadcrumbBuilder:: |
private | function | Get the parent nodes for the given node. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |