You are here

public function TaxonomyMenuTrailsBreadcrumbBuilder::build in Taxonomy Menu Trails 8

Builds the breadcrumb.

Parameters

\Drupal\Core\Routing\RouteMatchInterface $route_match: The current route match.

Return value

\Drupal\Core\Breadcrumb\Breadcrumb A breadcrumb.

Overrides BreadcrumbBuilderInterface::build

File

src/TaxonomyMenuTrailsBreadcrumbBuilder.php, line 124

Class

TaxonomyMenuTrailsBreadcrumbBuilder

Namespace

Drupal\taxonomy_menu_trails

Code

public function build(RouteMatchInterface $route_match) {
  $breadcrumb = new Breadcrumb();
  $breadcrumb
    ->addLink(Link::createFromRoute($this
    ->t('Home'), '<front>'));
  $node_object = $route_match
    ->getParameters()
    ->get('node');
  $node_is_fieldable = $node_object instanceof FieldableEntityInterface;
  if ($node_is_fieldable) {

    // Check all taxonomy terms applying to the current page.
    foreach ($node_object
      ->getFields() as $field) {
      if ($field
        ->getSetting('target_type') == 'taxonomy_term') {
        foreach ($field
          ->referencedEntities() as $term) {
          $url = $term
            ->toUrl();
          $route_links = $this->menuLinkManager
            ->loadLinksByRoute($url
            ->getRouteName(), $url
            ->getRouteParameters());
          if (!empty($route_links)) {
            $taxonomy_term_link = reset($route_links);
            $plugin_definition = $taxonomy_term_link
              ->getPluginDefinition();
            $taxonomy_term_id = $plugin_definition['route_parameters']['taxonomy_term'];
            $parents = $this->termStorage
              ->loadAllParents($taxonomy_term_id);
            foreach (array_reverse($parents) as $term1) {
              $term1 = $this->entityManager
                ->getTranslationFromContext($term1);
              $breadcrumb
                ->addLink(Link::createFromRoute($term1
                ->getName(), 'entity.taxonomy_term.canonical', [
                'taxonomy_term' => $term1
                  ->id(),
              ]));
            }
          }
        }
      }
    }
  }
  return $breadcrumb;
}