You are here

public function ContextManager::getContext in Context 8.4

Get a single context by id.

Parameters

string $id: The context id.

Return value

array|\Drupal\context\ContextInterface The context object if found, NULL otherwise.

File

src/ContextManager.php, line 152

Class

ContextManager
This is the manager service for the context module.

Namespace

Drupal\context

Code

public function getContext($id) {
  $contexts = $this
    ->getContexts();
  $currentContext = $this->currentRouteMatch
    ->getParameter('context') ? $this->currentRouteMatch
    ->getParameter('context')
    ->id() : '';
  $ids = [];
  if ($id) {
    if (strpos($id, '*') !== FALSE) {
      $id = preg_quote($id);
      $id = str_replace('\\*', '.*', $id);
      foreach ($contexts as $context) {
        if (preg_match('/^' . $id . '$/i', $context
          ->getName())) {
          if ($currentContext !== $context
            ->getName()) {
            $ids[$context
              ->getName()] = $context;
          }
        }
      }
      return $ids;
    }
    else {
      if (array_key_exists($id, $contexts)) {
        return $contexts[$id];
      }
    }
  }
}