public function ProductVariationContext::getRuntimeContexts in Commerce Core 8.2
Gets runtime context values for the given context IDs.
For context-aware plugins to function correctly, all of the contexts that they require must be populated with values. So this method should set a value for each context that it adds. For example:
// Determine a specific node to pass as context to a block.
$node = ...
// Set that specific node as the value of the 'node' context.
$context = EntityContext::fromEntity($node);
return ['node' => $context];
On the other hand, there are cases, on which providers no longer are possible to provide context objects, even without the value, so the caller should not expect it.
Parameters
string[] $unqualified_context_ids: The requested context IDs. The context provider must only return contexts for those IDs.
Return value
\Drupal\Core\Plugin\Context\ContextInterface[] The determined available contexts, keyed by the unqualified context_id.
Overrides ContextProviderInterface::getRuntimeContexts
See also
\Drupal\Core\Plugin\Context\ContextProviderInterface:getAvailableContexts()
1 call to ProductVariationContext::getRuntimeContexts()
- ProductVariationContext::getAvailableContexts in modules/
product/ src/ ContextProvider/ ProductVariationContext.php - Gets all available contexts for the purposes of configuration.
File
- modules/
product/ src/ ContextProvider/ ProductVariationContext.php, line 76
Class
- ProductVariationContext
- Provides a product variation context.
Namespace
Drupal\commerce_product\ContextProviderCode
public function getRuntimeContexts(array $unqualified_context_ids) {
$context_definition = new EntityContextDefinition('entity:commerce_product_variation', new TranslatableMarkup('Product variation'));
$value = $this->routeMatch
->getParameter('commerce_product_variation');
if ($value === NULL) {
$product = $this->routeMatch
->getParameter('commerce_product');
if ($product instanceof ProductInterface) {
$product_variation_storage = $this->entityTypeManager
->getStorage('commerce_product_variation');
assert($product_variation_storage instanceof ProductVariationStorageInterface);
$value = $product_variation_storage
->loadFromContext($product);
if ($value === NULL) {
$product_type = ProductType::load($product
->bundle());
$value = $product_variation_storage
->create([
'type' => $product_type
->getVariationTypeId(),
]);
}
}
elseif (strpos($this->routeMatch
->getRouteName(), 'layout_builder') !== FALSE) {
/** @var \Drupal\layout_builder\SectionStorageInterface $section_storage */
$section_storage = $this->routeMatch
->getParameter('section_storage');
if ($section_storage instanceof DefaultsSectionStorageInterface) {
$context = $section_storage
->getContextValue('display');
assert($context instanceof EntityDisplayInterface);
if ($context
->getTargetEntityTypeId() === 'commerce_product') {
$product_type = ProductType::load($context
->getTargetBundle());
$value = $this->sampleEntityGenerator
->get('commerce_product_variation', $product_type
->getVariationTypeId());
}
}
elseif ($section_storage instanceof OverridesSectionStorageInterface) {
$context = $section_storage
->getContextValue('entity');
if ($context instanceof ProductInterface) {
$value = $context
->getDefaultVariation();
if ($value === NULL) {
$product_type = ProductType::load($context
->bundle());
$value = $this->sampleEntityGenerator
->get('commerce_product_variation', $product_type
->getVariationTypeId());
}
}
}
}
}
$cacheability = new CacheableMetadata();
$cacheability
->setCacheContexts([
'route',
]);
$context = new Context($context_definition, $value);
$context
->addCacheableDependency($cacheability);
return [
'commerce_product_variation' => $context,
];
}