class ProductRouteContext in Commerce Core 8.2
Sets the current product as context on commerce_product routes.
@todo Remove once core gets a generic EntityRouteContext.
Hierarchy
- class \Drupal\commerce_product\ContextProvider\ProductRouteContext implements ContextProviderInterface uses StringTranslationTrait
Expanded class hierarchy of ProductRouteContext
1 string reference to 'ProductRouteContext'
- commerce_product.services.yml in modules/
product/ commerce_product.services.yml - modules/product/commerce_product.services.yml
1 service uses ProductRouteContext
File
- modules/
product/ src/ ContextProvider/ ProductRouteContext.php, line 19
Namespace
Drupal\commerce_product\ContextProviderView source
class ProductRouteContext implements ContextProviderInterface {
use StringTranslationTrait;
/**
* The route match.
*
* @var \Drupal\Core\Routing\RouteMatchInterface
*/
protected $routeMatch;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Constructs a new ProductRouteContext object.
*
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The route match.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
*/
public function __construct(RouteMatchInterface $route_match, EntityTypeManagerInterface $entity_type_manager) {
$this->routeMatch = $route_match;
$this->entityTypeManager = $entity_type_manager;
}
/**
* {@inheritdoc}
*/
public function getRuntimeContexts(array $unqualified_context_ids) {
$context_definition = new EntityContextDefinition('entity:commerce_product', NULL, FALSE);
$value = NULL;
if ($product = $this->routeMatch
->getParameter('commerce_product')) {
$value = $product;
}
elseif ($product_type = $this->routeMatch
->getParameter('commerce_product_type')) {
$product_storage = $this->entityTypeManager
->getStorage('commerce_product');
$product_type_id = $product_type instanceof ProductTypeInterface ? $product_type
->id() : $product_type;
$value = $product_storage
->createWithSampleValues($product_type_id);
}
$cacheability = new CacheableMetadata();
$cacheability
->setCacheContexts([
'route',
]);
$context = new Context($context_definition, $value);
$context
->addCacheableDependency($cacheability);
return [
'commerce_product' => $context,
];
}
/**
* {@inheritdoc}
*/
public function getAvailableContexts() {
$context = new Context(new EntityContextDefinition('entity:commerce_product', $this
->t('Product from URL')));
return [
'commerce_product' => $context,
];
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ProductRouteContext:: |
protected | property | The entity type manager. | |
ProductRouteContext:: |
protected | property | The route match. | |
ProductRouteContext:: |
public | function |
Gets all available contexts for the purposes of configuration. Overrides ContextProviderInterface:: |
|
ProductRouteContext:: |
public | function |
Gets runtime context values for the given context IDs. Overrides ContextProviderInterface:: |
|
ProductRouteContext:: |
public | function | Constructs a new ProductRouteContext 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. |