You are here

class HierarchyBreadcrumbBuilder in Entity Reference Hierarchy 8

Provides a breadcrumb builder for nodes in a book.

Hierarchy

Expanded class hierarchy of HierarchyBreadcrumbBuilder

1 string reference to 'HierarchyBreadcrumbBuilder'
entity_hierarchy.services.yml in ./entity_hierarchy.services.yml
entity_hierarchy.services.yml
1 service uses HierarchyBreadcrumbBuilder
entity_hierarchy.breadcrumb in ./entity_hierarchy.services.yml
Drupal\entity_hierarchy\HierarchyBreadcrumbBuilder

File

src/HierarchyBreadcrumbBuilder.php, line 23
Contains \Drupal\entity_hierarchy\HierarchyBreadcrumbBuilder.

Namespace

Drupal\entity_hierarchy
View 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

Namesort descending Modifiers Type Description Overrides
HierarchyBreadcrumbBuilder::applies public function Whether this breadcrumb builder should be used to build the breadcrumb. Overrides BreadcrumbBuilderInterface::applies
HierarchyBreadcrumbBuilder::build public function Builds the breadcrumb. Overrides BreadcrumbBuilderInterface::build
HierarchyBreadcrumbBuilder::hierarchyGetNodeParentNids private function Get the parent nodes for the given node.
HierarchyBreadcrumbBuilder::hierarchyGetNodeParentPrimaryNid private function Get the primary parent nid for the given node.
HierarchyBreadcrumbBuilder::hierarchyGetNodeParents private function Get all the parents for the given node.
HierarchyBreadcrumbBuilder::hierarchyGetNodePrimaryAncestorNids private function Get the ancestor nodes for the given node.
HierarchyBreadcrumbBuilder::hierarchyGetNodePrimaryAncestorNodes private function Get the parent nodes for the given node.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.