You are here

class CatalogBreadcrumbBuilder in Ubercart 8.4

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

Hierarchy

Expanded class hierarchy of CatalogBreadcrumbBuilder

1 string reference to 'CatalogBreadcrumbBuilder'
uc_catalog.services.yml in uc_catalog/uc_catalog.services.yml
uc_catalog/uc_catalog.services.yml
1 service uses CatalogBreadcrumbBuilder
uc_catalog.breadcrumb in uc_catalog/uc_catalog.services.yml
Drupal\uc_catalog\CatalogBreadcrumbBuilder

File

uc_catalog/src/CatalogBreadcrumbBuilder.php, line 15

Namespace

Drupal\uc_catalog
View source
class CatalogBreadcrumbBuilder implements BreadcrumbBuilderInterface {
  use StringTranslationTrait;

  /**
   * The taxonomy storage.
   *
   * @var \Drupal\Taxonomy\TermStorageInterface
   */
  protected $termStorage;

  /**
   * Constructs a new CatalogBreadcrumbBuilder.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager) {
    $this->termStorage = $entity_type_manager
      ->getStorage('taxonomy_term');
  }

  /**
   * {@inheritdoc}
   */
  public function applies(RouteMatchInterface $route_match) {
    $route_name = $route_match
      ->getRouteName();
    return $route_name == 'entity.node.canonical' && $route_match
      ->getParameter('node') && !empty($route_match
      ->getParameter('node')->taxonomy_catalog->target_id) || substr($route_name, 0, 16) == 'view.uc_catalog.' && $route_match
      ->getParameter('arg_0');
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CatalogBreadcrumbBuilder::$termStorage protected property The taxonomy storage.
CatalogBreadcrumbBuilder::applies public function Whether this breadcrumb builder should be used to build the breadcrumb. Overrides BreadcrumbBuilderInterface::applies
CatalogBreadcrumbBuilder::build public function Builds the breadcrumb. Overrides BreadcrumbBuilderInterface::build
CatalogBreadcrumbBuilder::__construct public function Constructs a new CatalogBreadcrumbBuilder.
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.