You are here

public function ProductBreadcrumbBuilder::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/ProductBreadcrumbBuilder.php, line 88

Class

ProductBreadcrumbBuilder
Builds a product breadcrumb based on the "field_product_categories" field.

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>'));
  $breadcrumb
    ->addLink(Link::createFromRoute($this
    ->t('Catalog'), 'view.product_catalog.page_1'));
  $product = $route_match
    ->getParameter('commerce_product');
  assert($product instanceof ProductInterface);
  $breadcrumb
    ->addCacheableDependency($product);
  $category = $product
    ->get('field_product_categories')->entity;
  if (!$category instanceof TermInterface) {
    return $breadcrumb;
  }
  $breadcrumb
    ->addCacheableDependency($category);
  $facet = $this->facetStorage
    ->load($category
    ->bundle());
  if (!$facet instanceof FacetInterface) {
    return $breadcrumb;
  }
  $label = $this->aliasCleaner
    ->cleanString($category
    ->label());
  $view_url = Url::fromRoute('view.product_catalog.page_1');
  $facet_url = Url::fromUserInput($view_url
    ->toString() . '/' . $facet
    ->getUrlAlias() . '/' . $label . '-' . $category
    ->id());
  $breadcrumb
    ->addLink(Link::fromTextAndUrl($category
    ->label(), $facet_url));
  return $breadcrumb;
}