public function LazyContextRepository::getRuntimeContexts in Drupal 8
Same name and namespace in other branches
- 9 core/lib/Drupal/Core/Plugin/Context/LazyContextRepository.php \Drupal\Core\Plugin\Context\LazyContextRepository::getRuntimeContexts()
Gets runtime context values for the given context IDs.
Given that context providers might not return contexts for the given context IDs, it is also not guaranteed that the context repository returns contexts for all specified IDs.
Parameters
string[] $context_ids: Fully qualified context IDs, which looks like service_id}:{unqualified_context_id}, so for example node.node_route_context:node.
Return value
\Drupal\Core\Plugin\Context\ContextInterface[] The determined contexts, keyed by the fully qualified context ID.
Overrides ContextRepositoryInterface::getRuntimeContexts
File
- core/
lib/ Drupal/ Core/ Plugin/ Context/ LazyContextRepository.php, line 50
Class
- LazyContextRepository
- Provides a context repository which uses context provider services.
Namespace
Drupal\Core\Plugin\ContextCode
public function getRuntimeContexts(array $context_ids) {
$contexts = [];
// Create a map of context providers (service IDs) to unqualified context
// IDs.
$context_ids_by_service = [];
foreach ($context_ids as $id) {
if (isset($this->contexts[$id])) {
$contexts[$id] = $this->contexts[$id];
continue;
}
// The IDs have been passed in @{service_id}:{unqualified_context_id}
// format.
// @todo Convert to an assert once https://www.drupal.org/node/2408013 is
// in.
if ($id[0] === '@' && strpos($id, ':') !== FALSE) {
list($service_id, $unqualified_context_id) = explode(':', $id, 2);
// Remove the leading '@'.
$service_id = substr($service_id, 1);
}
else {
throw new \InvalidArgumentException('You must provide the context IDs in the @{service_id}:{unqualified_context_id} format.');
}
$context_ids_by_service[$service_id][] = $unqualified_context_id;
}
// Iterate over all missing context providers (services), gather the
// runtime contexts and assign them as requested.
foreach ($context_ids_by_service as $service_id => $unqualified_context_ids) {
$contexts_by_service = $this->container
->get($service_id)
->getRuntimeContexts($unqualified_context_ids);
$wanted_contexts = array_intersect_key($contexts_by_service, array_flip($unqualified_context_ids));
foreach ($wanted_contexts as $unqualified_context_id => $context) {
$context_id = '@' . $service_id . ':' . $unqualified_context_id;
$this->contexts[$context_id] = $contexts[$context_id] = $context;
}
}
return $contexts;
}