function LazyContextRepository::getRuntimeContexts

Same name in other branches
  1. 9 core/lib/Drupal/Core/Plugin/Context/LazyContextRepository.php \Drupal\Core\Plugin\Context\LazyContextRepository::getRuntimeContexts()
  2. 8.9.x core/lib/Drupal/Core/Plugin/Context/LazyContextRepository.php \Drupal\Core\Plugin\Context\LazyContextRepository::getRuntimeContexts()
  3. 10 core/lib/Drupal/Core/Plugin/Context/LazyContextRepository.php \Drupal\Core\Plugin\Context\LazyContextRepository::getRuntimeContexts()

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\Context

Code

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;
        }
        assert($id[0] === '@' && str_contains($id, ':'), 'You must provide the context IDs in the @{service_id}:{unqualified_context_id} format.');
        list($service_id, $unqualified_context_id) = explode(':', $id, 2);
        // Remove the leading '@'.
        $service_id = substr($service_id, 1);
        $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;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.