class CatalogBreadcrumbBuilder in Commerce Demo 8
Builds a catalog breadcrumb.
Hierarchy
- class \Drupal\commerce_demo\CatalogBreadcrumbBuilder implements BreadcrumbBuilderInterface uses StringTranslationTrait
Expanded class hierarchy of CatalogBreadcrumbBuilder
1 string reference to 'CatalogBreadcrumbBuilder'
1 service uses CatalogBreadcrumbBuilder
File
- src/
CatalogBreadcrumbBuilder.php, line 19
Namespace
Drupal\commerce_demoView source
class CatalogBreadcrumbBuilder implements BreadcrumbBuilderInterface {
use StringTranslationTrait;
/**
* The title resolver.
*
* @var \Drupal\Core\Controller\TitleResolverInterface
*/
protected $titleResolver;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManager
*/
protected $entityTypeManager;
/**
* The request stack.
*
* @var \Symfony\Component\HttpFoundation\RequestStack
*/
protected $requestStack;
/**
* Constructs a new CatalogBreadcrumbBuilder object.
*
* @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
* The request stack.
* @param \Drupal\Core\Controller\TitleResolverInterface $title_resolver
* The title resolver.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity storage.
*/
public function __construct(RequestStack $request_stack, TitleResolverInterface $title_resolver, EntityTypeManagerInterface $entity_type_manager) {
$this->requestStack = $request_stack;
$this->titleResolver = $title_resolver;
$this->entityTypeManager = $entity_type_manager;
}
/**
* {@inheritdoc}
*/
public function applies(RouteMatchInterface $route_match) {
return $route_match
->getRouteName() == 'view.product_catalog.page_1' && $route_match
->getParameter('facets_query');
}
/**
* {@inheritdoc}
*/
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;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CatalogBreadcrumbBuilder:: |
protected | property | The entity type manager. | |
CatalogBreadcrumbBuilder:: |
protected | property | The request stack. | |
CatalogBreadcrumbBuilder:: |
protected | property | The title resolver. | |
CatalogBreadcrumbBuilder:: |
public | function |
Whether this breadcrumb builder should be used to build the breadcrumb. Overrides BreadcrumbBuilderInterface:: |
|
CatalogBreadcrumbBuilder:: |
public | function |
Builds the breadcrumb. Overrides BreadcrumbBuilderInterface:: |
|
CatalogBreadcrumbBuilder:: |
public | function | Constructs a new CatalogBreadcrumbBuilder object. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |