class ProductBreadcrumbBuilder in Commerce Demo 8
Builds a product breadcrumb based on the "field_product_categories" field.
Hierarchy
- class \Drupal\commerce_demo\ProductBreadcrumbBuilder implements BreadcrumbBuilderInterface uses StringTranslationTrait
Expanded class hierarchy of ProductBreadcrumbBuilder
1 string reference to 'ProductBreadcrumbBuilder'
1 service uses ProductBreadcrumbBuilder
File
- src/
ProductBreadcrumbBuilder.php, line 22
Namespace
Drupal\commerce_demoView source
class ProductBreadcrumbBuilder implements BreadcrumbBuilderInterface {
use StringTranslationTrait;
/**
* The alias cleaner.
*
* @var \Drupal\pathauto\AliasCleanerInterface
*/
protected $aliasCleaner;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $facetStorage;
/**
* The route provider.
*
* @var \Drupal\Core\Routing\RouteProviderInterface
*/
protected $routeProvider;
/**
* Constructs a new ProductBreadcrumbBuilder object.
*
* @param \Drupal\pathauto\AliasCleanerInterface $alias_cleaner
* The alias cleaner.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Core\Routing\RouteProviderInterface $route_provider
* The route provider.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
public function __construct(AliasCleanerInterface $alias_cleaner, EntityTypeManagerInterface $entity_type_manager, RouteProviderInterface $route_provider) {
$this->aliasCleaner = $alias_cleaner;
$this->facetStorage = $entity_type_manager
->getStorage('facets_facet');
$this->routeProvider = $route_provider;
}
/**
* {@inheritdoc}
*/
public function applies(RouteMatchInterface $route_match) {
if ($route_match
->getRouteName() !== 'entity.commerce_product.canonical') {
return FALSE;
}
try {
$this->routeProvider
->getRouteByName('view.product_catalog.page_1');
} catch (RouteNotFoundException $e) {
// The catalog View may have been disabled or deleted.
return FALSE;
}
$product = $route_match
->getParameter('commerce_product');
return $product && $product
->hasField('field_product_categories');
}
/**
* {@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.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;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ProductBreadcrumbBuilder:: |
protected | property | The alias cleaner. | |
ProductBreadcrumbBuilder:: |
protected | property | The entity type manager. | |
ProductBreadcrumbBuilder:: |
protected | property | The route provider. | |
ProductBreadcrumbBuilder:: |
public | function |
Whether this breadcrumb builder should be used to build the breadcrumb. Overrides BreadcrumbBuilderInterface:: |
|
ProductBreadcrumbBuilder:: |
public | function |
Builds the breadcrumb. Overrides BreadcrumbBuilderInterface:: |
|
ProductBreadcrumbBuilder:: |
public | function | Constructs a new ProductBreadcrumbBuilder 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. |