You are here

public function CatalogBreadcrumbBuilder::build in Commerce Demo 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/CatalogBreadcrumbBuilder.php, line 70

Class

CatalogBreadcrumbBuilder
Builds a catalog breadcrumb.

Namespace

Drupal\commerce_demo

Code

public function build(RouteMatchInterface $route_match) {
  $breadcrumb = new Breadcrumb();
  $breadcrumb
    ->addCacheContexts([
    'route',
  ]);
  $breadcrumb
    ->addLink(Link::createFromRoute($this
    ->t('Home'), '<front>'));
  $title = $this->titleResolver
    ->getTitle($this->requestStack
    ->getCurrentRequest(), $route_match
    ->getRouteObject());
  $breadcrumb
    ->addLink(Link::fromTextAndUrl($title, Url::fromRoute($route_match
    ->getRouteName())));
  $facets_query = $route_match
    ->getParameter('facets_query');
  $facets_query_parts = explode('/', $facets_query);
  if (count($facets_query_parts) % 2 !== 0) {
    array_shift($facets_query_parts);
  }
  foreach ($facets_query_parts as $k => $facets_query_part) {
    if ($facets_query_part == 'category') {
      $facet_query_alias = $facets_query_part;
      $facet_value = $facets_query_parts[$k + 1];
      $alias_value_parts = explode('-', $facet_value);
      $term_id = array_pop($alias_value_parts);
      $term = $this->entityTypeManager
        ->getStorage('taxonomy_term')
        ->load($term_id);
      assert($term instanceof TermInterface);
      $breadcrumb
        ->addLink(Link::fromTextAndUrl($term
        ->getName(), Url::fromRoute($route_match
        ->getRouteName(), [
        'facets_query' => "{$facet_query_alias}/{$facet_value}",
      ])));
      $breadcrumb
        ->addCacheableDependency($term);
      break;
    }
  }
  return $breadcrumb;
}