You are here

public function ProductVariationContext::getAvailableContexts in Commerce Core 8.2

Gets all available contexts for the purposes of configuration.

When a context aware plugin is being configured, the configuration UI must know which named contexts are potentially available, but does not care about the value, since the value can be different for each request, and might not be available at all during the configuration UI's request.

For example:

// During configuration, there is no specific node to pass as context.
// However, inform the system that a context named 'node' is
// available, and provide its definition, so that context aware plugins
// can be configured to use it. When the plugin, for example a block,
// needs to evaluate the context, the value of this context will be
// supplied by getRuntimeContexts().
$context = EntityContext::fromEntityTypeId('node');
return [
  'node' => $context,
];

Return value

\Drupal\Core\Plugin\Context\ContextInterface[] All available contexts keyed by the unqualified context ID.

Overrides ContextProviderInterface::getAvailableContexts

See also

\Drupal\Core\Plugin\Context\ContextProviderInterface::getRuntimeContext()

File

modules/product/src/ContextProvider/ProductVariationContext.php, line 129

Class

ProductVariationContext
Provides a product variation context.

Namespace

Drupal\commerce_product\ContextProvider

Code

public function getAvailableContexts() {

  // @todo Remove this route name check once Layout Builder is fixed.
  if ($this->routeMatch
    ->getRouteName() !== NULL) {

    // This handles when the context is called via getAvailableContexts in
    // the entity param converter while route negotiation is being handled.
    //
    // This is a pretty big hack to workaround the fact Layout Builder does
    // not properly invoke getRuntimeContexts but assumes getAvailableContexts
    // will have populated values in the contexts returned.
    //
    // @see https://www.drupal.org/project/drupal/issues/3099968
    // @see \Drupal\Core\ParamConverter\EntityConverter::convert
    return $this
      ->getRuntimeContexts([]);
  }
  $context = EntityContext::fromEntityTypeId('commerce_product_variation', $this
    ->t('Product variation from current product.'));
  return [
    'commerce_product_variation' => $context,
  ];
}