You are here

public function NodeRouteContext::getAvailableContexts in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/node/src/ContextProvider/NodeRouteContext.php \Drupal\node\ContextProvider\NodeRouteContext::getAvailableContexts()
  2. 9 core/modules/node/src/ContextProvider/NodeRouteContext.php \Drupal\node\ContextProvider\NodeRouteContext::getAvailableContexts()

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

core/modules/node/src/ContextProvider/NodeRouteContext.php, line 76

Class

NodeRouteContext
Sets the current node as a context on node routes.

Namespace

Drupal\node\ContextProvider

Code

public function getAvailableContexts() {
  $context = EntityContext::fromEntityTypeId('node', $this
    ->t('Node from URL'));
  return [
    'node' => $context,
  ];
}