How do I see which contexts are active on the page?

I can't find any ContextManager object in hook_preprocess_page().

2 Answers 2


I figured it out: The Context module defines a service for getting the ContextManager object, which has getActiveContexts(). You can obtain it using the following code.

$activeContexts = \Drupal::service('context.manager')->getActiveContexts();

I need to read Service Container.


You can use this snippet in template_preprocess_page():

  // Pass vars to page.html.twig depending on active contexts
  $active_contexts = \Drupal::service('context.manager')->getActiveContexts();

  $vars['is_my_context_active'] = FALSE;
  foreach ($active_contexts as $context) {
    if ($context->getName() == 'my_context') {
      $vars['is_my_context_active'] = TRUE;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.