You are here

public function CatalogBreadcrumbBuilder::build in Ubercart 8.4

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

uc_catalog/src/CatalogBreadcrumbBuilder.php, line 47

Class

CatalogBreadcrumbBuilder
Provides a custom breadcrumb builder for catalog node and listing pages.

Namespace

Drupal\uc_catalog

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.uc_catalog.page_1'));
  if ($route_match
    ->getRouteName() == 'entity.node.canonical') {

    // Extract term ID for node view.
    $tid = $route_match
      ->getParameter('node')->taxonomy_catalog->target_id;
    $skip_last = FALSE;
  }
  else {

    // Get term ID argument for catalog view, and skip the last term.
    $tid = $route_match
      ->getParameter('arg_0');
    $skip_last = TRUE;
  }
  if ($parents = $this->termStorage
    ->loadAllParents($tid)) {
    if ($skip_last) {
      array_shift($parents);
    }
    $parents = array_reverse($parents);
    foreach ($parents as $parent) {
      $breadcrumb
        ->addCacheableDependency($parent);
      $breadcrumb
        ->addLink(Link::createFromRoute($parent
        ->label(), 'view.uc_catalog.page_1', [
        'arg_0' => $parent
          ->id(),
      ]));
    }
  }
  return $breadcrumb;
}