diff --git a/core/core.services.yml b/core/core.services.yml
index 0788f37..dc38193 100644
--- a/core/core.services.yml
+++ b/core/core.services.yml
@@ -266,6 +266,9 @@ services:
   context.handler:
     class: Drupal\Core\Plugin\Context\ContextHandler
     arguments: ['@typed_data_manager']
+  context.manager:
+    class: Drupal\Core\Plugin\Context\ContextManager
+    arguments: ['@service_container']
   cron:
     class: Drupal\Core\Cron
     arguments: ['@module_handler', '@lock', '@queue', '@state', '@account_switcher', '@logger.channel.cron', '@plugin.manager.queue_worker']
@@ -1399,7 +1402,6 @@ services:
       - { name: event_subscriber }
   email.validator:
     class: Egulias\EmailValidator\EmailValidator
-
   response_filter.active_link:
     class: Drupal\Core\EventSubscriber\ActiveLinkResponseFilter
     arguments: ['@current_user', '@path.current', '@path.matcher', '@language_manager']
diff --git a/core/lib/Drupal/Core/CoreServiceProvider.php b/core/lib/Drupal/Core/CoreServiceProvider.php
index ae0d031..8244320 100644
--- a/core/lib/Drupal/Core/CoreServiceProvider.php
+++ b/core/lib/Drupal/Core/CoreServiceProvider.php
@@ -10,6 +10,7 @@
 use Drupal\Core\Cache\Context\CacheContextsPass;
 use Drupal\Core\Cache\ListCacheBinsPass;
 use Drupal\Core\DependencyInjection\Compiler\BackendCompilerPass;
+use Drupal\Core\DependencyInjection\Compiler\ContextProvidersPass;
 use Drupal\Core\DependencyInjection\Compiler\ProxyServicesPass;
 use Drupal\Core\DependencyInjection\Compiler\RegisterLazyRouteEnhancers;
 use Drupal\Core\DependencyInjection\Compiler\RegisterLazyRouteFilters;
@@ -88,6 +89,7 @@ public function register(ContainerBuilder $container) {
     // Add the compiler pass that will process the tagged services.
     $container->addCompilerPass(new ListCacheBinsPass());
     $container->addCompilerPass(new CacheContextsPass());
+    $container->addCompilerPass(new ContextProvidersPass());
 
     // Register plugin managers.
     $container->addCompilerPass(new PluginManagerPass());
diff --git a/core/lib/Drupal/Core/DependencyInjection/Compiler/ContextProvidersPass.php b/core/lib/Drupal/Core/DependencyInjection/Compiler/ContextProvidersPass.php
new file mode 100644
index 0000000..a2d0189
--- /dev/null
+++ b/core/lib/Drupal/Core/DependencyInjection/Compiler/ContextProvidersPass.php
@@ -0,0 +1,34 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\Core\DependencyInjection\Compiler\ContextProvidersPass.
+ */
+
+namespace Drupal\Core\DependencyInjection\Compiler;
+
+use Symfony\Component\DependencyInjection\ContainerBuilder;
+use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
+
+/**
+ * Adds the context provider service IDs to the context manager.
+ */
+class ContextProvidersPass implements CompilerPassInterface {
+
+  /**
+   * {@inheritdoc}
+   *
+   * Adds the IDs of services tagged as context providers service IDs to the
+   * context manager.
+   */
+  public function process(ContainerBuilder $container) {
+    $context_providers = [];
+    foreach (array_keys($container->findTaggedServiceIds('context_provider')) as $id) {
+      $context_providers[] = $id;
+    }
+
+    $definition = $container->getDefinition('context.manager');
+    $definition->addArgument($context_providers);
+  }
+
+}
diff --git a/core/lib/Drupal/Core/Plugin/Context/ContextManager.php b/core/lib/Drupal/Core/Plugin/Context/ContextManager.php
new file mode 100644
index 0000000..da67f03
--- /dev/null
+++ b/core/lib/Drupal/Core/Plugin/Context/ContextManager.php
@@ -0,0 +1,98 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\Core\Plugin\Context\ContextManager.
+ */
+
+namespace Drupal\Core\Plugin\Context;
+
+use Symfony\Component\DependencyInjection\ContainerInterface;
+
+/**
+ * Manages context results.
+ *
+ * Provides methods to fetch all available contexts at configuration-time or a
+ * subset at run-time.
+ */
+class ContextManager implements ContextManagerInterface {
+
+  /**
+   * The set of available context providers service IDs.
+   *
+   * @var string[]
+   *   Context provider service IDs.
+   */
+  protected $contextProviderServiceIDs = [];
+
+  /**
+   * The service container.
+   *
+   * @var \Symfony\Component\DependencyInjection\ContainerInterface
+   */
+  protected $container;
+
+  /**
+   * Constructs a ContextManager object.
+   *
+   * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
+   *   The current service container.
+   * @param string[] $context_provider_service_ids
+   *   The set of the available context provider service IDs.
+   */
+  public function __construct(ContainerInterface $container, array $context_provider_service_ids) {
+    $this->container = $container;
+    $this->contextProviderServiceIDs = $context_provider_service_ids;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function getRunTimeContexts(array $context_ids) {
+    $contexts = [];
+
+    // Create a map of context providers (service IDs) to context slot names.
+    $context_slot_names_by_service = [];
+    foreach ($context_ids as $id) {
+      // The IDs have been passed in {context_slot_name}@{service_id} format.
+      if (strpos($id, '@') !== FALSE) {
+        list($context_slot_name, $id) = explode('@', $id, 2);
+      }
+      else {
+        throw new \InvalidArgumentException('You must provide the context IDs in the {context_slot_name}@{service_id} format.');
+      }
+      $context_slot_names_by_service[$id][] = $context_slot_name;
+    }
+
+    // Iterate over all context providers (services), gather the run-time
+    // contexts and assign them to the slots as requested.
+    foreach ($context_slot_names_by_service as $service_id => $context_slot_names) {
+      $context_results = $this->container->get($service_id)->getRunTimeContexts($context_slot_names);
+
+      $wanted_contexts = array_intersect_key($context_results, array_flip($context_slot_names));
+      foreach ($wanted_contexts as $context_slot_name => $context) {
+        $context_id = $context_slot_name . '@' . $service_id;
+        $contexts[$context_id] = $context;
+      }
+    }
+
+    return $contexts;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function getConfigurationTimeContexts() {
+    $contexts = [];
+    foreach ($this->contextProviderServiceIDs as $service_id) {
+      $context_results = $this->container->get($service_id)->getConfigurationTimeContexts();
+      foreach ($context_results as $context_slot_name => $context) {
+        $context_id = $context_slot_name . '@' . $service_id;
+        $contexts[$context_id] = $context;
+      }
+    }
+
+    return $contexts;
+  }
+
+}
diff --git a/core/lib/Drupal/Core/Plugin/Context/ContextManagerInterface.php b/core/lib/Drupal/Core/Plugin/Context/ContextManagerInterface.php
new file mode 100644
index 0000000..1421ab8
--- /dev/null
+++ b/core/lib/Drupal/Core/Plugin/Context/ContextManagerInterface.php
@@ -0,0 +1,35 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\Core\Plugin\Context\ContextManagerInterface.
+ */
+
+namespace Drupal\Core\Plugin\Context;
+
+/**
+ * Manages available contexts and collects context values at run-time.
+ */
+interface ContextManagerInterface {
+
+  /**
+   * Gets run-time context values for the given context IDs.
+   *
+   * @param string[] $context_ids
+   *   Context provider IDs to get contexts for. These must be in the
+   *   {context_slot_name}@{service_id} format.
+   *
+   * @return \Drupal\Core\Plugin\Context\ContextCollectionInterface
+   *   The run-time contexts.
+   */
+  public function getRunTimeContexts(array $context_ids);
+
+  /**
+   * Gets all context values for the purposes of configuration.
+   *
+   * @return \Drupal\Core\Plugin\Context\ContextCollectionInterface
+   *   The configuration-time contexts.
+   */
+  public function getConfigurationTimeContexts();
+
+}
diff --git a/core/lib/Drupal/Core/Plugin/Context/ContextProviderInterface.php b/core/lib/Drupal/Core/Plugin/Context/ContextProviderInterface.php
new file mode 100644
index 0000000..3a6fb3e
--- /dev/null
+++ b/core/lib/Drupal/Core/Plugin/Context/ContextProviderInterface.php
@@ -0,0 +1,66 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\Core\Plugin\Context\ContextProviderInterface.
+ */
+
+namespace Drupal\Core\Plugin\Context;
+
+/**
+ * Defines an interface for providing plugin contexts.
+ */
+interface ContextProviderInterface {
+
+  /**
+   * Determines the available run-time contexts.
+   *
+   * For context-aware plugins to function correctly, all of the contexts that
+   * they require must be populated with values. So this method must set a value
+   * for each context that it adds. For example:
+   * @code
+   *   // Determine a specific node to pass as context to a block.
+   *   $node = ...
+   *
+   *   // Set that specific node as the value of the 'node' context.
+   *   $context = new Context(new ContextDefinition('entity:node'));
+   *   $context->setContextValue($node);
+   *   return ['node.node' => $context];
+   * @endcode
+   *
+   * @param array $context_slot_names
+   *   The needed context IDs. The context provider can decide to optimize it.
+   *
+   * @return \Drupal\Core\Plugin\Context\ContextInterface[]
+   *   The determined contexts.
+   */
+  public function getRunTimeContexts(array $context_slot_names);
+
+  /**
+   * Determines the available configuration-time contexts.
+   *
+   * 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:
+   * @code
+   *   // During configuration, there is no specific node to pass as context.
+   *   // However, inform the system that a context named 'node.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 = new Context(new ContextDefinition('entity:node'));
+   *   return ['node.node' => $context];
+   * @endcode
+   *
+   * @return \Drupal\Core\Plugin\Context\ContextInterface[]
+   *   All available contexts.
+   *
+   * @see static::getActiveContext()
+   */
+  public function getConfigurationTimeContexts();
+
+}
diff --git a/core/modules/block/block.services.yml b/core/modules/block/block.services.yml
index df4d0d7..0030fc8 100644
--- a/core/modules/block/block.services.yml
+++ b/core/modules/block/block.services.yml
@@ -8,20 +8,20 @@ services:
     tags:
       - { name: event_subscriber }
   block.current_user_context:
-    class: Drupal\block\EventSubscriber\CurrentUserContext
+    class: Drupal\block\ContextProvider\CurrentUserContext
     arguments: ['@current_user', '@entity.manager']
     tags:
-      - { name: 'event_subscriber' }
+      - { name: 'context_provider' }
   block.current_language_context:
-    class: Drupal\block\EventSubscriber\CurrentLanguageContext
+    class: Drupal\block\ContextProvider\CurrentLanguageContext
     arguments: ['@language_manager']
     tags:
-      - { name: 'event_subscriber' }
+      - { name: 'context_provider' }
   block.node_route_context:
-    class: Drupal\block\EventSubscriber\NodeRouteContext
+    class: Drupal\block\ContextProvider\NodeRouteContext
     arguments: ['@current_route_match']
     tags:
-      - { name: 'event_subscriber' }
+      - { name: 'context_provider' }
   block.repository:
     class: Drupal\block\BlockRepository
     arguments: ['@entity.manager', '@theme.manager', '@context.handler']
diff --git a/core/modules/block/src/BlockForm.php b/core/modules/block/src/BlockForm.php
index ac9406a..6c8e09c 100644
--- a/core/modules/block/src/BlockForm.php
+++ b/core/modules/block/src/BlockForm.php
@@ -7,7 +7,6 @@
 
 namespace Drupal\block;
 
-use Drupal\block\Event\BlockContextEvent;
 use Drupal\block\Event\BlockEvents;
 use Drupal\Component\Utility\Html;
 use Drupal\Core\Entity\EntityForm;
@@ -18,6 +17,7 @@
 use Drupal\Core\Form\FormStateInterface;
 use Drupal\Core\Language\LanguageManagerInterface;
 use Drupal\Core\Plugin\ContextAwarePluginInterface;
+use Drupal\Core\Plugin\Context\ContextManagerInterface;
 use Symfony\Component\DependencyInjection\ContainerInterface;
 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
 
@@ -69,23 +69,30 @@ class BlockForm extends EntityForm {
   protected $themeHandler;
 
   /**
+   * The context manager service.
+   *
+   * @var \Drupal\Core\Plugin\Context\ContextManagerInterface
+   */
+  protected $contextManager;
+
+  /**
    * Constructs a BlockForm object.
    *
    * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
    *   The entity manager.
    * @param \Drupal\Core\Executable\ExecutableManagerInterface $manager
    *   The ConditionManager for building the visibility UI.
-   * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $dispatcher
-   *   The EventDispatcher for gathering administrative contexts.
+   * @param \Drupal\Core\Plugin\Context\ContextManagerInterface $context_manager
+   *   The context manager service.
    * @param \Drupal\Core\Language\LanguageManagerInterface $language
    *   The language manager.
    * @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler
    * The theme handler.
    */
-  public function __construct(EntityManagerInterface $entity_manager, ExecutableManagerInterface $manager, EventDispatcherInterface $dispatcher, LanguageManagerInterface $language, ThemeHandlerInterface $theme_handler) {
+  public function __construct(EntityManagerInterface $entity_manager, ExecutableManagerInterface $manager, ContextManagerInterface $context_manager, LanguageManagerInterface $language, ThemeHandlerInterface $theme_handler) {
     $this->storage = $entity_manager->getStorage('block');
     $this->manager = $manager;
-    $this->dispatcher = $dispatcher;
+    $this->contextManager = $context_manager;
     $this->language = $language;
     $this->themeHandler = $theme_handler;
   }
@@ -97,7 +104,7 @@ public static function create(ContainerInterface $container) {
     return new static(
       $container->get('entity.manager'),
       $container->get('plugin.manager.condition'),
-      $container->get('event_dispatcher'),
+      $container->get('context.manager'),
       $container->get('language_manager'),
       $container->get('theme_handler')
     );
@@ -117,7 +124,7 @@ public function form(array $form, FormStateInterface $form_state) {
 
     // Store the gathered contexts in the form state for other objects to use
     // during form building.
-    $form_state->setTemporaryValue('gathered_contexts', $this->dispatcher->dispatch(BlockEvents::ADMINISTRATIVE_CONTEXT, new BlockContextEvent())->getContexts());
+    $form_state->setTemporaryValue('gathered_contexts', $this->contextManager->getConfigurationTimeContexts());
 
     $form['#tree'] = TRUE;
     $form['settings'] = $entity->getPlugin()->buildConfigurationForm(array(), $form_state);
diff --git a/core/modules/block/src/BlockRepository.php b/core/modules/block/src/BlockRepository.php
index bc5eb8c..a5db410 100644
--- a/core/modules/block/src/BlockRepository.php
+++ b/core/modules/block/src/BlockRepository.php
@@ -32,6 +32,13 @@ class BlockRepository implements BlockRepositoryInterface {
   protected $themeManager;
 
   /**
+   * The set of active blocks for each theme.
+   *
+   * @var array
+   */
+  protected $blocksForTheme = [];
+
+  /**
    * Constructs a new BlockRepository.
    *
    * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
@@ -52,13 +59,18 @@ public function __construct(EntityManagerInterface $entity_manager, ThemeManager
    */
   public function getVisibleBlocksPerRegion(array $contexts, array &$cacheable_metadata = []) {
     $active_theme = $this->themeManager->getActiveTheme();
+    if (!isset($this->blocksForTheme[$active_theme->getName()])) {
+      $this->blocksForTheme[$active_theme->getName()] = $this->getBlocksForTheme();
+    }
+    $blocks = $this->blocksForTheme[$active_theme->getName()];
     // Build an array of the region names in the right order.
     $empty = array_fill_keys($active_theme->getRegions(), array());
 
-    $full = array();
-    foreach ($this->blockStorage->loadByProperties(array('theme' => $active_theme->getName())) as $block_id => $block) {
+    $full = [];
+    /* @var \Drupal\block\BlockInterface $block */
+    foreach ($blocks as $block_id => $block) {
       /** @var \Drupal\block\BlockInterface $block */
-      $block->setContexts($contexts);
+      $block->setContexts($contexts + $block->getContexts());
       $access = $block->access('view', NULL, TRUE);
       $region = $block->getRegion();
       if (!isset($cacheable_metadata[$region])) {
@@ -84,4 +96,13 @@ public function getVisibleBlocksPerRegion(array $contexts, array &$cacheable_met
     return $assignments;
   }
 
+  /**
+   * {@inheritdoc}
+   */
+  public function getBlocksForTheme() {
+    $active_theme = $this->themeManager->getActiveTheme();
+    $this->blocksForTheme[$active_theme->getName()] = $this->blockStorage->loadByProperties(['theme' => $active_theme->getName()]);
+    return $this->blocksForTheme[$active_theme->getName()];
+  }
+
 }
diff --git a/core/modules/block/src/BlockRepositoryInterface.php b/core/modules/block/src/BlockRepositoryInterface.php
index 00eb5e4..1fa1ace 100644
--- a/core/modules/block/src/BlockRepositoryInterface.php
+++ b/core/modules/block/src/BlockRepositoryInterface.php
@@ -24,4 +24,12 @@
    */
   public function getVisibleBlocksPerRegion(array $contexts, array &$cacheable_metadata = []);
 
+  /**
+   * Gets the active blocks as block entities for the current theme.
+   *
+   * @return \Drupal\block\BlockInterface[]
+   *   Array of block entities keyed by block ID.
+   */
+  public function getBlocksForTheme();
+
 }
diff --git a/core/modules/block/src/EventSubscriber/CurrentLanguageContext.php b/core/modules/block/src/ContextProvider/CurrentLanguageContext.php
similarity index 64%
rename from core/modules/block/src/EventSubscriber/CurrentLanguageContext.php
rename to core/modules/block/src/ContextProvider/CurrentLanguageContext.php
index 7b1a206..163194d 100644
--- a/core/modules/block/src/EventSubscriber/CurrentLanguageContext.php
+++ b/core/modules/block/src/ContextProvider/CurrentLanguageContext.php
@@ -2,22 +2,23 @@
 
 /**
  * @file
- * Contains \Drupal\block\EventSubscriber\CurrentLanguageContext.
+ * Contains \Drupal\block\ContextProvider\CurrentLanguageContext.
  */
 
-namespace Drupal\block\EventSubscriber;
+namespace Drupal\block\ContextProvider;
 
-use Drupal\block\Event\BlockContextEvent;
 use Drupal\Core\Cache\CacheableMetadata;
 use Drupal\Core\Language\LanguageManagerInterface;
 use Drupal\Core\Plugin\Context\Context;
+use Drupal\Core\Plugin\Context\ContextResult;
 use Drupal\Core\Plugin\Context\ContextDefinition;
+use Drupal\Core\Plugin\Context\ContextProviderInterface;
 use Drupal\Core\StringTranslation\StringTranslationTrait;
 
 /**
  * Sets the current language as a context.
  */
-class CurrentLanguageContext extends BlockContextSubscriberBase {
+class CurrentLanguageContext implements ContextProviderInterface {
 
   use StringTranslationTrait;
 
@@ -41,10 +42,20 @@ public function __construct(LanguageManagerInterface $language_manager) {
   /**
    * {@inheritdoc}
    */
-  public function onBlockActiveContext(BlockContextEvent $event) {
+  public function getRunTimeContexts(array $context_slot_names) {
     // Add a context for each language type.
     $language_types = $this->languageManager->getLanguageTypes();
     $info = $this->languageManager->getDefinedLanguageTypesInfo();
+
+    if ($context_slot_names) {
+      foreach ($context_slot_names as $context_slot_name) {
+        if (array_search(str_replace('language.', '', $context_slot_name), $language_types) === FALSE) {
+          unset($language_types[str_replace('language.', '', $context_slot_name)]);
+        }
+      }
+    }
+
+    $result = [];
     foreach ($language_types as $type_key) {
       if (isset($info[$type_key]['name'])) {
         $context = new Context(new ContextDefinition('language', $info[$type_key]['name']));
@@ -54,16 +65,18 @@ public function onBlockActiveContext(BlockContextEvent $event) {
         $cacheability->setCacheContexts(['languages:' . $type_key]);
         $context->addCacheableDependency($cacheability);
 
-        $event->setContext('language.' . $type_key, $context);
+        $result['language.' . $type_key] = $context;
       }
     }
+
+    return $result;
   }
 
   /**
    * {@inheritdoc}
    */
-  public function onBlockAdministrativeContext(BlockContextEvent $event) {
-    $this->onBlockActiveContext($event);
+  public function getConfigurationTimeContexts() {
+    return $this->getRunTimeContexts([]);
   }
 
 }
diff --git a/core/modules/block/src/EventSubscriber/CurrentUserContext.php b/core/modules/block/src/ContextProvider/CurrentUserContext.php
similarity index 74%
rename from core/modules/block/src/EventSubscriber/CurrentUserContext.php
rename to core/modules/block/src/ContextProvider/CurrentUserContext.php
index 194a252..1f0678a 100644
--- a/core/modules/block/src/EventSubscriber/CurrentUserContext.php
+++ b/core/modules/block/src/ContextProvider/CurrentUserContext.php
@@ -2,23 +2,24 @@
 
 /**
  * @file
- * Contains \Drupal\block\EventSubscriber\CurrentUserContext.
+ * Contains \Drupal\block\ContextProvider\CurrentUserContext.
  */
 
-namespace Drupal\block\EventSubscriber;
+namespace Drupal\block\ContextProvider;
 
-use Drupal\block\Event\BlockContextEvent;
 use Drupal\Core\Cache\CacheableMetadata;
 use Drupal\Core\Entity\EntityManagerInterface;
 use Drupal\Core\Plugin\Context\Context;
+use Drupal\Core\Plugin\Context\ContextResult;
 use Drupal\Core\Plugin\Context\ContextDefinition;
+use Drupal\Core\Plugin\Context\ContextProviderInterface;
 use Drupal\Core\Session\AccountInterface;
 use Drupal\Core\StringTranslation\StringTranslationTrait;
 
 /**
  * Sets the current user as a context.
  */
-class CurrentUserContext extends BlockContextSubscriberBase {
+class CurrentUserContext implements ContextProviderInterface {
 
   use StringTranslationTrait;
 
@@ -52,7 +53,7 @@ public function __construct(AccountInterface $account, EntityManagerInterface $e
   /**
    * {@inheritdoc}
    */
-  public function onBlockActiveContext(BlockContextEvent $event) {
+  public function getRunTimeContexts(array $context_slot_names) {
     $current_user = $this->userStorage->load($this->account->id());
 
     $context = new Context(new ContextDefinition('entity:user', $this->t('Current user')));
@@ -60,14 +61,18 @@ public function onBlockActiveContext(BlockContextEvent $event) {
     $cacheability = new CacheableMetadata();
     $cacheability->setCacheContexts(['user']);
     $context->addCacheableDependency($cacheability);
-    $event->setContext('user.current_user', $context);
+
+    $result = [];
+    $result['user.current_user'] = $context;
+
+    return $result;
   }
 
   /**
    * {@inheritdoc}
    */
-  public function onBlockAdministrativeContext(BlockContextEvent $event) {
-    $this->onBlockActiveContext($event);
+  public function getConfigurationTimeContexts() {
+    return $this->getRunTimeContexts([]);
   }
 
 }
diff --git a/core/modules/block/src/EventSubscriber/NodeRouteContext.php b/core/modules/block/src/ContextProvider/NodeRouteContext.php
similarity index 75%
rename from core/modules/block/src/EventSubscriber/NodeRouteContext.php
rename to core/modules/block/src/ContextProvider/NodeRouteContext.php
index 89d24f5..a4bc9ac 100644
--- a/core/modules/block/src/EventSubscriber/NodeRouteContext.php
+++ b/core/modules/block/src/ContextProvider/NodeRouteContext.php
@@ -2,22 +2,23 @@
 
 /**
  * @file
- * Contains \Drupal\block\EventSubscriber\NodeRouteContext.
+ * Contains \Drupal\block\ContextProvider\NodeRouteContext.
  */
 
-namespace Drupal\block\EventSubscriber;
+namespace Drupal\block\ContextProvider;
 
-use Drupal\block\Event\BlockContextEvent;
 use Drupal\Core\Cache\CacheableMetadata;
 use Drupal\Core\Plugin\Context\Context;
+use Drupal\Core\Plugin\Context\ContextResult;
 use Drupal\Core\Plugin\Context\ContextDefinition;
+use Drupal\Core\Plugin\Context\ContextProviderInterface;
 use Drupal\Core\Routing\RouteMatchInterface;
 use Drupal\node\Entity\Node;
 
 /**
  * Sets the current node as a context on node routes.
  */
-class NodeRouteContext extends BlockContextSubscriberBase {
+class NodeRouteContext implements ContextProviderInterface {
 
   /**
    * The route match object.
@@ -39,13 +40,13 @@ public function __construct(RouteMatchInterface $route_match) {
   /**
    * {@inheritdoc}
    */
-  public function onBlockActiveContext(BlockContextEvent $event) {
+  public function getRunTimeContexts(array $context_slot_names) {
+    $result = [];
     $context = new Context(new ContextDefinition('entity:node', NULL, FALSE));
     if (($route_object = $this->routeMatch->getRouteObject()) && ($route_contexts = $route_object->getOption('parameters')) && isset($route_contexts['node'])) {
       if ($node = $this->routeMatch->getParameter('node')) {
         $context->setContextValue($node);
       }
-      $event->setContext('node.node', $context);
     }
     elseif ($this->routeMatch->getRouteName() == 'node.add') {
       $node_type = $this->routeMatch->getParameter('node_type');
@@ -54,15 +55,17 @@ public function onBlockActiveContext(BlockContextEvent $event) {
     $cacheability = new CacheableMetadata();
     $cacheability->setCacheContexts(['route']);
     $context->addCacheableDependency($cacheability);
-    $event->setContext('node.node', $context);
+    $result['node.node'] = $context;
+
+    return $result;
   }
 
   /**
    * {@inheritdoc}
    */
-  public function onBlockAdministrativeContext(BlockContextEvent $event) {
+  public function getConfigurationTimeContexts() {
     $context = new Context(new ContextDefinition('entity:node'));
-    $event->setContext('node.node', $context);
+    return ['node.node' => $context];
   }
 
 }
diff --git a/core/modules/block/src/Event/BlockContextEvent.php b/core/modules/block/src/Event/BlockContextEvent.php
deleted file mode 100644
index 99b0bbd..0000000
--- a/core/modules/block/src/Event/BlockContextEvent.php
+++ /dev/null
@@ -1,53 +0,0 @@
-<?php
-
-/**
- * @file
- * Contains \Drupal\block\Event\BlockContextEvent.
- */
-
-namespace Drupal\block\Event;
-
-use Drupal\Core\Plugin\Context\ContextInterface;
-use Symfony\Component\EventDispatcher\Event;
-
-/**
- * Event subscribers can add context to be used by the block and its conditions.
- *
- * @see \Drupal\block\Event\BlockEvents::ACTIVE_CONTEXT
- * @see \Drupal\block\Event\BlockEvents::ADMINISTRATIVE_CONTEXT
- */
-class BlockContextEvent extends Event {
-
-  /**
-   * The array of available contexts for blocks.
-   *
-   * @var array
-   */
-  protected $contexts = [];
-
-  /**
-   * Sets the context object for a given name.
-   *
-   * @param string $name
-   *   The name to store the context object under.
-   * @param \Drupal\Core\Plugin\Context\ContextInterface $context
-   *   The context object to set.
-   *
-   * @return $this
-   */
-  public function setContext($name, ContextInterface $context) {
-    $this->contexts[$name] = $context;
-    return $this;
-  }
-
-  /**
-   * Returns the context objects.
-   *
-   * @return \Drupal\Component\Plugin\Context\ContextInterface[]
-   *   An array of contexts that have been provided.
-   */
-  public function getContexts() {
-    return $this->contexts;
-  }
-
-}
diff --git a/core/modules/block/src/Event/BlockEvents.php b/core/modules/block/src/Event/BlockEvents.php
deleted file mode 100644
index 118f2f8..0000000
--- a/core/modules/block/src/Event/BlockEvents.php
+++ /dev/null
@@ -1,55 +0,0 @@
-<?php
-
-/**
- * @file
- * Contains \Drupal\block\Event\BlockEvents.
- */
-
-namespace Drupal\block\Event;
-
-/**
- * Defines events for the Block module.
- */
-final class BlockEvents {
-
-  /**
-   * Name of the event when gathering condition context for a block plugin.
-   *
-   * This event allows you to provide additional context that can be used by
-   * a condition plugin in order to determine the visibility of a block. The
-   * event listener method receives a \Drupal\block\Event\BlockContextEvent
-   * instance. Generally any new context is paired with a new condition plugin
-   * that interprets the provided context and allows the block system to
-   * determine whether or not the block should be displayed.
-   *
-   * @Event
-   *
-   * @see \Drupal\Core\Block\BlockBase::getConditionContexts()
-   * @see \Drupal\block\Event\BlockContextEvent
-   * @see \Drupal\block\EventSubscriber\NodeRouteContext::onBlockActiveContext()
-   * @see \Drupal\Core\Condition\ConditionInterface
-   */
-  const ACTIVE_CONTEXT = 'block.active_context';
-
-  /**
-   * Name of the event when gathering contexts for plugin configuration.
-   *
-   * This event allows you to provide information about your context to the
-   * administration UI without having to provide a value for the context. For
-   * example, during configuration there is no specific node to pass as context.
-   * However, we still need to inform the system that a context named 'node' is
-   * available and provide a definition so that blocks can be configured to use
-   * it.
-   *
-   * The event listener method receives a \Drupal\block\Event\BlockContextEvent
-   * instance.
-   *
-   * @Event
-   *
-   * @see \Drupal\block\BlockForm::form()
-   * @see \Drupal\block\Event\BlockContextEvent
-   * @see \Drupal\block\EventSubscriber\NodeRouteContext::onBlockAdministrativeContext()
-   */
-  const ADMINISTRATIVE_CONTEXT = 'block.administrative_context';
-
-}
diff --git a/core/modules/block/src/EventSubscriber/BlockContextSubscriberBase.php b/core/modules/block/src/EventSubscriber/BlockContextSubscriberBase.php
deleted file mode 100644
index 60158db..0000000
--- a/core/modules/block/src/EventSubscriber/BlockContextSubscriberBase.php
+++ /dev/null
@@ -1,75 +0,0 @@
-<?php
-
-/**
- * @file
- * Contains \Drupal\block\EventSubscriber\BlockContextSubscriberBase.
- */
-
-namespace Drupal\block\EventSubscriber;
-
-use Drupal\block\Event\BlockContextEvent;
-use Drupal\block\Event\BlockEvents;
-use Symfony\Component\EventDispatcher\EventSubscriberInterface;
-
-/**
- * Provides a base class for block context subscribers.
- */
-abstract class BlockContextSubscriberBase implements EventSubscriberInterface {
-
-  /**
-   * {@inheritdoc}
-   */
-  public static function getSubscribedEvents() {
-    $events[BlockEvents::ACTIVE_CONTEXT][] = 'onBlockActiveContext';
-    $events[BlockEvents::ADMINISTRATIVE_CONTEXT][] = 'onBlockAdministrativeContext';
-    return $events;
-  }
-
-  /**
-   * Determines the available run-time contexts.
-   *
-   * For blocks to render correctly, all of the contexts that they require
-   * must be populated with values. So this method must set a value for each
-   * context that it adds. For example:
-   * @code
-   *   // Determine a specific node to pass as context to blocks.
-   *   $node = ...
-   *
-   *   // Set that specific node as the value of the 'node' context.
-   *   $context = new Context(new ContextDefinition('entity:node'));
-   *   $context->setContextValue($node);
-   *   $event->setContext('node.node', $context);
-   * @endcode
-   *
-   * @param \Drupal\block\Event\BlockContextEvent $event
-   *   The Event to which to register available contexts.
-   */
-  abstract public function onBlockActiveContext(BlockContextEvent $event);
-
-  /**
-   * Determines the available configuration-time contexts.
-   *
-   * When a block 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:
-   * @code
-   *   // During configuration, there is no specific node to pass as context.
-   *   // However, inform the system that a context named 'node.node' is
-   *   // available, and provide its definition, so that blocks can be
-   *   // configured to use it. When the block is rendered, the value of this
-   *   // context will be supplied by onBlockActiveContext().
-   *   $context = new Context(new ContextDefinition('entity:node'));
-   *   $event->setContext('node.node', $context);
-   * @endcode
-   *
-   * @param \Drupal\block\Event\BlockContextEvent $event
-   *   The Event to which to register available contexts.
-   *
-   * @see static::onBlockActiveContext()
-   */
-  abstract public function onBlockAdministrativeContext(BlockContextEvent $event);
-
-}
diff --git a/core/modules/block/src/Plugin/DisplayVariant/BlockPageVariant.php b/core/modules/block/src/Plugin/DisplayVariant/BlockPageVariant.php
index f5caee4..505a093 100644
--- a/core/modules/block/src/Plugin/DisplayVariant/BlockPageVariant.php
+++ b/core/modules/block/src/Plugin/DisplayVariant/BlockPageVariant.php
@@ -7,19 +7,17 @@
 
 namespace Drupal\block\Plugin\DisplayVariant;
 
+use Drupal\Core\Plugin\Context\ContextManagerInterface;
 use Drupal\block\BlockRepositoryInterface;
-use Drupal\block\Event\BlockContextEvent;
-use Drupal\block\Event\BlockEvents;
 use Drupal\Core\Block\MainContentBlockPluginInterface;
 use Drupal\Core\Block\MessagesBlockPluginInterface;
 use Drupal\Core\Cache\CacheableMetadata;
 use Drupal\Core\Display\PageVariantInterface;
-use Drupal\Core\Entity\EntityTypeInterface;
 use Drupal\Core\Entity\EntityViewBuilderInterface;
 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
 use Drupal\Core\Display\VariantBase;
+use Drupal\Core\Plugin\ContextAwarePluginInterface;
 use Symfony\Component\DependencyInjection\ContainerInterface;
-use Symfony\Component\EventDispatcher\EventDispatcherInterface;
 
 /**
  * Provides a page display variant that decorates the main content with blocks.
@@ -61,6 +59,13 @@ class BlockPageVariant extends VariantBase implements PageVariantInterface, Cont
   protected $blockListCacheTags;
 
   /**
+   * The context manager service.
+   *
+   * @var \Drupal\Core\Plugin\Context\ContextManagerInterface
+   */
+  protected $contextManager;
+
+  /**
    * The render array representing the main page content.
    *
    * @var array
@@ -80,16 +85,16 @@ class BlockPageVariant extends VariantBase implements PageVariantInterface, Cont
    *   The block repository.
    * @param \Drupal\Core\Entity\EntityViewBuilderInterface $block_view_builder
    *   The block view builder.
-   * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $dispatcher
-   *   The event dispatcher.
+   * @param \Drupal\Core\Plugin\Context\ContextManagerInterface $context_manager
+   *   The block context manager service.
    * @param string[] $block_list_cache_tags
    *   The Block entity type list cache tags.
    */
-  public function __construct(array $configuration, $plugin_id, $plugin_definition, BlockRepositoryInterface $block_repository, EntityViewBuilderInterface $block_view_builder, EventDispatcherInterface $dispatcher, array $block_list_cache_tags) {
+  public function __construct(array $configuration, $plugin_id, $plugin_definition, BlockRepositoryInterface $block_repository, EntityViewBuilderInterface $block_view_builder, ContextManagerInterface $context_manager, array $block_list_cache_tags) {
     parent::__construct($configuration, $plugin_id, $plugin_definition);
     $this->blockRepository = $block_repository;
     $this->blockViewBuilder = $block_view_builder;
-    $this->dispatcher = $dispatcher;
+    $this->contextManager = $context_manager;
     $this->blockListCacheTags = $block_list_cache_tags;
   }
 
@@ -103,7 +108,7 @@ public static function create(ContainerInterface $container, array $configuratio
       $plugin_definition,
       $container->get('block.repository'),
       $container->get('entity.manager')->getViewBuilder('block'),
-      $container->get('event_dispatcher'),
+      $container->get('context.manager'),
       $container->get('entity.manager')->getDefinition('block')->getListCacheTags()
     );
   }
@@ -129,10 +134,10 @@ public function build() {
         'tags' => $this->blockListCacheTags,
       ],
     ];
-    $contexts = $this->getActiveBlockContexts();
     // Load all region content assigned via blocks.
-    $cacheable_metadata_list = [];
-    foreach ($this->blockRepository->getVisibleBlocksPerRegion($contexts, $cacheable_metadata_list) as $region => $blocks) {
+    $theme_blocks = $this->blockRepository->getBlocksForTheme();
+    $cacheable_metadata = [];
+    foreach ($this->blockRepository->getVisibleBlocksPerRegion($this->getRunTimeContexts($theme_blocks), $cacheable_metadata) as $region => $blocks) {
       /** @var $blocks \Drupal\block\BlockInterface[] */
       foreach ($blocks as $key => $block) {
         $block_plugin = $block->getPlugin();
@@ -180,7 +185,7 @@ public function build() {
     // This would need to be changed to allow caching of block regions, as each
     // region must then have the relevant cacheable metadata.
     $merged_cacheable_metadata = CacheableMetadata::createFromRenderArray($build);
-    foreach ($cacheable_metadata_list as $cacheable_metadata) {
+    foreach ($cacheable_metadata as $cacheable_metadata) {
       $merged_cacheable_metadata = $merged_cacheable_metadata->merge($cacheable_metadata);
     }
     $merged_cacheable_metadata->applyTo($build);
@@ -189,13 +194,35 @@ public function build() {
   }
 
   /**
-   * Returns an array of context objects to set on the blocks.
+   * Retrieves the required contexts for placed blocks.
+   *
+   * Whilst these blocks may be placed, we cannot determine whether they are
+   * visible until after context values have been calculated and provided to
+   * the block so that access and therefore visibility can be evaluated.
    *
-   * @return \Drupal\Component\Plugin\Context\ContextInterface[]
-   *   An array of contexts to set on the blocks.
+   * @param \Drupal\block\BlockInterface[] $theme_blocks
+   *   Blocks placed in the current theme that may or may not be visible to the
+   *   current user.
+   *
+   * @return \Drupal\Core\Plugin\Context\ContextInterface[]
+   *   Array of context objects.
    */
-  protected function getActiveBlockContexts() {
-    return $this->dispatcher->dispatch(BlockEvents::ACTIVE_CONTEXT, new BlockContextEvent())->getContexts();
+  protected function getRunTimeContexts(array $theme_blocks) {
+    $context_provider_ids = [];
+    $contexts = [];
+    foreach ($theme_blocks as $block_id => $block) {
+      /* @var \Drupal\block\BlockInterface $block */
+      foreach ($block->getVisibilityConditions() as $condition) {
+        if ($condition instanceof ContextAwarePluginInterface) {
+          /* @var \Drupal\Core\Plugin\ContextAwarePluginInterface $condition */
+          $context_provider_ids = array_unique(array_merge($context_provider_ids, $condition->getContextMapping()));
+        }
+      }
+    }
+    if ($context_provider_ids) {
+      $contexts = $this->contextManager->getRunTimeContexts($context_provider_ids);
+    }
+    return $contexts;
   }
 
 }
diff --git a/core/modules/block/src/Tests/BlockLanguageTest.php b/core/modules/block/src/Tests/BlockLanguageTest.php
index 56ee191..a034dc8 100644
--- a/core/modules/block/src/Tests/BlockLanguageTest.php
+++ b/core/modules/block/src/Tests/BlockLanguageTest.php
@@ -90,7 +90,7 @@ public function testLanguageBlockVisibilityLanguageDelete() {
           'langcodes' => array(
             'fr' => 'fr',
           ),
-          'context_mapping' => ['language' => 'language.language_interface'],
+          'context_mapping' => ['language' => 'language.language_interface@block.current_language_context'],
         ),
       ),
     );
@@ -142,7 +142,7 @@ public function testMultipleLanguageTypes() {
     // Enable a standard block and set visibility to French only.
     $block_id = strtolower($this->randomMachineName(8));
     $edit = [
-      'visibility[language][context_mapping][language]' => 'language.language_interface',
+      'visibility[language][context_mapping][language]' => 'language.language_interface@block.current_language_context',
       'visibility[language][langcodes][fr]' => TRUE,
       'id' => $block_id,
       'region' => 'sidebar_first',
@@ -168,7 +168,7 @@ public function testMultipleLanguageTypes() {
 
     // Change visibility to now depend on content language for this block.
     $edit = [
-      'visibility[language][context_mapping][language]' => 'language.language_content'
+      'visibility[language][context_mapping][language]' => 'language.language_content@block.current_language_context'
     ];
     $this->drupalPostForm('admin/structure/block/manage/' . $block_id, $edit, t('Save block'));
 
diff --git a/core/modules/block/tests/src/Unit/BlockFormTest.php b/core/modules/block/tests/src/Unit/BlockFormTest.php
index 8acff20..117cb34 100644
--- a/core/modules/block/tests/src/Unit/BlockFormTest.php
+++ b/core/modules/block/tests/src/Unit/BlockFormTest.php
@@ -31,13 +31,6 @@ class BlockFormTest extends UnitTestCase {
   protected $storage;
 
   /**
-   * The event dispatcher service.
-   *
-   * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface|\PHPUnit_Framework_MockObject_MockObject
-   */
-  protected $dispatcher;
-
-  /**
    * The language manager service.
    *
    * @var \Drupal\Core\Language\LanguageManagerInterface|\PHPUnit_Framework_MockObject_MockObject
@@ -60,6 +53,13 @@ class BlockFormTest extends UnitTestCase {
   protected $entityManager;
 
   /**
+   * The context manager service.
+   *
+   * @var \Drupal\Core\Plugin\Context\ContextManagerInterface|\PHPUnit_Framework_MockObject_MockObject
+   */
+  protected $contextManager;
+
+  /**
    * {@inheritdoc}
    */
   protected function setUp() {
@@ -67,7 +67,7 @@ protected function setUp() {
 
     $this->conditionManager = $this->getMock('Drupal\Core\Executable\ExecutableManagerInterface');
     $this->language = $this->getMock('Drupal\Core\Language\LanguageManagerInterface');
-    $this->dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
+    $this->contextManager = $this->getMock('Drupal\Core\Plugin\Context\ContextManagerInterface');
 
     $this->entityManager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
     $this->storage = $this->getMock('Drupal\Core\Config\Entity\ConfigEntityStorageInterface');
@@ -104,7 +104,7 @@ public function testGetUniqueMachineName() {
       ->method('getQuery')
       ->will($this->returnValue($query));
 
-    $block_form_controller = new BlockForm($this->entityManager, $this->conditionManager, $this->dispatcher, $this->language, $this->themeHandler);
+    $block_form_controller = new BlockForm($this->entityManager, $this->conditionManager, $this->contextManager, $this->language, $this->themeHandler);
 
     // Ensure that the block with just one other instance gets the next available
     // name suggestion.
diff --git a/core/modules/block/tests/src/Unit/BlockRepositoryTest.php b/core/modules/block/tests/src/Unit/BlockRepositoryTest.php
index d77d64e..30a98a1 100644
--- a/core/modules/block/tests/src/Unit/BlockRepositoryTest.php
+++ b/core/modules/block/tests/src/Unit/BlockRepositoryTest.php
@@ -60,7 +60,7 @@ protected function setUp() {
       ]);
 
     $theme_manager = $this->getMock('Drupal\Core\Theme\ThemeManagerInterface');
-    $theme_manager->expects($this->once())
+    $theme_manager->expects($this->atLeastOnce())
       ->method('getActiveTheme')
       ->will($this->returnValue($active_theme));
 
@@ -91,6 +91,9 @@ public function testGetVisibleBlocksPerRegion(array $blocks_config, array $expec
       $block->expects($this->once())
         ->method('access')
         ->will($this->returnValue($block_config[0]));
+      $block->expects($this->any())
+        ->method('getContexts')
+        ->willReturn([]);
       $block->expects($block_config[0] ? $this->atLeastOnce() : $this->never())
         ->method('getRegion')
         ->willReturn($block_config[1]);
@@ -102,7 +105,8 @@ public function testGetVisibleBlocksPerRegion(array $blocks_config, array $expec
       ->with(['theme' => $this->theme])
       ->willReturn($blocks);
     $result = [];
-    foreach ($this->blockRepository->getVisibleBlocksPerRegion([]) as $region => $resulting_blocks) {
+    $cacheable_metadata = [];
+    foreach ($this->blockRepository->getVisibleBlocksPerRegion([], $cacheable_metadata) as $region => $resulting_blocks) {
       $result[$region] = [];
       foreach ($resulting_blocks as $plugin_id => $block) {
         $result[$region][] = $plugin_id;
@@ -153,6 +157,9 @@ public function testGetVisibleBlocksPerRegionWithContext() {
     $block->expects($this->once())
       ->method('access')
       ->willReturn(AccessResult::allowed()->addCacheTags(['config:block.block.block_id']));
+    $block->expects($this->any())
+      ->method('getContexts')
+      ->willReturn([]);
     $block->expects($this->once())
       ->method('getRegion')
       ->willReturn('top');
diff --git a/core/modules/block/tests/src/Unit/Plugin/DisplayVariant/BlockPageVariantTest.php b/core/modules/block/tests/src/Unit/Plugin/DisplayVariant/BlockPageVariantTest.php
index 5438a99..13b7440 100644
--- a/core/modules/block/tests/src/Unit/Plugin/DisplayVariant/BlockPageVariantTest.php
+++ b/core/modules/block/tests/src/Unit/Plugin/DisplayVariant/BlockPageVariantTest.php
@@ -32,18 +32,18 @@ class BlockPageVariantTest extends UnitTestCase {
   protected $blockViewBuilder;
 
   /**
-   * The event dispatcher.
+   * The plugin context handler.
    *
-   * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface|\PHPUnit_Framework_MockObject_MockObject
+   * @var \Drupal\Core\Plugin\Context\ContextHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
    */
-  protected $dispatcher;
+  protected $contextHandler;
 
   /**
-   * The plugin context handler.
+   * The context manager.
    *
-   * @var \Drupal\Core\Plugin\Context\ContextHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
+   * @var \Drupal\Core\Plugin\Context\ContextManagerInterface|\PHPUnit_Framework_MockObject_MockObject
    */
-  protected $contextHandler;
+  protected $contextManager;
 
   /**
    * Sets up a display variant plugin for testing.
@@ -71,12 +71,9 @@ public function setUpDisplayVariant($configuration = array(), $definition = arra
 
     $this->blockRepository = $this->getMock('Drupal\block\BlockRepositoryInterface');
     $this->blockViewBuilder = $this->getMock('Drupal\Core\Entity\EntityViewBuilderInterface');
-    $this->dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
-    $this->dispatcher->expects($this->any())
-      ->method('dispatch')
-      ->willReturnArgument(1);
+    $this->contextManager = $this->getMock('Drupal\Core\Plugin\Context\ContextManagerInterface');
     return $this->getMockBuilder('Drupal\block\Plugin\DisplayVariant\BlockPageVariant')
-      ->setConstructorArgs(array($configuration, 'test', $definition, $this->blockRepository, $this->blockViewBuilder, $this->dispatcher, ['config:block_list']))
+      ->setConstructorArgs(array($configuration, 'test', $definition, $this->blockRepository, $this->blockViewBuilder, $this->contextManager, ['config:block_list']))
       ->setMethods(array('getRegionNames'))
       ->getMock();
   }
@@ -217,12 +214,14 @@ public function testBuild(array $blocks_config, $visible_block_count, array $exp
     $messages_block_plugin = $this->getMock('Drupal\Core\Block\MessagesBlockPluginInterface');
     foreach ($blocks_config as $block_id => $block_config) {
       $block = $this->getMock('Drupal\block\BlockInterface');
+      $block->expects($this->any())
+        ->method('getContexts')
+        ->willReturn([]);
       $block->expects($this->atLeastOnce())
         ->method('getPlugin')
         ->willReturn($block_config[1] ? $main_content_block_plugin : ($block_config[2] ? $messages_block_plugin : $block_plugin));
       $blocks[$block_config[0]][$block_id] = $block;
     }
-
     $this->blockViewBuilder->expects($this->exactly($visible_block_count))
       ->method('view')
       ->will($this->returnValue(array()));
@@ -232,8 +231,12 @@ public function testBuild(array $blocks_config, $visible_block_count, array $exp
         $cacheable_metadata['top'] = (new CacheableMetadata())->addCacheTags(['route']);
         return $blocks;
       });
+      $this->blockRepository->expects($this->once())
+        ->method('getBlocksForTheme')
+        ->willReturn([]);
 
-    $this->assertSame($expected_render_array, $display_variant->build());
+    $value = $display_variant->build();
+    $this->assertSame($expected_render_array, $value);
   }
 
   /**
@@ -246,6 +249,9 @@ public function testBuildWithoutMainContent() {
     $this->blockRepository->expects($this->once())
       ->method('getVisibleBlocksPerRegion')
       ->willReturn([]);
+    $this->blockRepository->expects($this->once())
+      ->method('getBlocksForTheme')
+      ->willReturn([]);
 
     $expected = [
       '#cache' => [
diff --git a/core/modules/comment/comment.module b/core/modules/comment/comment.module
index 8bcac2c..47b9cb0 100644
--- a/core/modules/comment/comment.module
+++ b/core/modules/comment/comment.module
@@ -339,7 +339,7 @@ function comment_entity_storage_load($entities, $entity_type) {
   if (!\Drupal::entityManager()->getDefinition($entity_type)->isSubclassOf('Drupal\Core\Entity\FieldableEntityInterface')) {
     return;
   }
-  if (!\Drupal::service('comment.manager')->getFields($entity_type)) {
+  if (!\Drupal::hasService('comment.manager') || !\Drupal::service('comment.manager')->getFields($entity_type)) {
     // Do not query database when entity has no comment fields.
     return;
   }
diff --git a/core/tests/Drupal/Tests/Core/Plugin/Context/ContextManagerTest.php b/core/tests/Drupal/Tests/Core/Plugin/Context/ContextManagerTest.php
new file mode 100644
index 0000000..00c3cda
--- /dev/null
+++ b/core/tests/Drupal/Tests/Core/Plugin/Context/ContextManagerTest.php
@@ -0,0 +1,112 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\Tests\Core\Plugin\Context\ContextManagerTest.
+ */
+
+namespace Drupal\Tests\Core\Plugin\Context;
+
+use Drupal\Core\Plugin\Context\Context;
+use Drupal\Core\Plugin\Context\ContextDefinition;
+use Drupal\Core\Plugin\Context\ContextManager;
+use Drupal\Tests\UnitTestCase;
+use Symfony\Component\DependencyInjection\ContainerBuilder;
+
+/**
+ * @coversDefaultClass \Drupal\Core\Plugin\Context\ContextManager
+ * @group context
+ */
+class ContextManagerTest extends UnitTestCase {
+
+  /**
+   * The container.
+   *
+   * @var \Symfony\Component\DependencyInjection\ContainerBuilder
+   */
+  protected $container;
+
+  /**
+   * {@inheritdoc}
+   */
+  protected function setUp() {
+    parent::setUp();
+
+    $this->container = new ContainerBuilder();
+  }
+
+  /**
+   * @covers ::getRunTimeContexts
+   */
+  public function testGetRunTimeContextsSingle() {
+    $contexts = $this->setupContextAndProvider('test_provider', ['test_context']);
+
+    $context_manager = new ContextManager($this->container, ['test_provider']);
+    $run_time_contexts = $context_manager->getRunTimeContexts(['test_context@test_provider']);
+    $this->assertEquals(['test_context@test_provider' => $contexts[0]], $run_time_contexts);
+  }
+
+  /**
+   * @covers ::getRunTimeContexts
+   */
+  public function testGetRunTimeMultipleContextsPerService() {
+    $contexts = $this->setupContextAndProvider('test_provider', ['test_context0', 'test_context1']);
+
+    $context_manager = new ContextManager($this->container, ['test_provider']);
+    $run_time_contexts = $context_manager->getRunTimeContexts(['test_context0@test_provider', 'test_context1@test_provider']);
+    $this->assertEquals(['test_context0@test_provider' => $contexts[0], 'test_context1@test_provider' => $contexts[1]], $run_time_contexts);
+  }
+
+  /**
+   * @covers ::getRunTimeContexts
+   */
+  public function testGetRunTimeMultipleContextProviders() {
+    $contexts0 = $this->setupContextAndProvider('test_provider', ['test_context0', 'test_context1'], ['test_context0']);
+    $contexts1 = $this->setupContextAndProvider('test_provider2', ['test1_context0', 'test1_context1'], ['test1_context0']);
+
+    $context_manager = new ContextManager($this->container, ['test_provider']);
+    $run_time_contexts = $context_manager->getRunTimeContexts(['test_context0@test_provider', 'test1_context0@test_provider2']);
+    $this->assertEquals(['test_context0@test_provider' => $contexts0[0], 'test1_context0@test_provider2' => $contexts1[1]], $run_time_contexts);
+  }
+
+  /**
+   * @covers ::getRunTimeContexts
+   * @expectedException \InvalidArgumentException
+   * @expectedExceptionMessage You must provide the context IDs in the {context_slot_name}@{service_id} format.
+   */
+  public function testInvalidContextId() {
+    $context_manager = new ContextManager($this->container, ['test_provider']);
+    $context_manager->getRunTimeContexts(['test_context', 'test_context1@test_provider']);
+  }
+
+  /**
+   * Sets up contexts and context providers.
+   *
+   * @param string $service_id
+   *   The service ID of the service provider.
+   * @param string[] $context_slot_names
+   *   An array of context slot names.
+   * @param string[] $expected_context_slot_names
+   *   The expected context slotes passed to getRunTimeContexts.
+   *
+   * @return array
+   *   An array of set up contexts.
+   */
+  protected function setupContextAndProvider($service_id, $context_slot_names, $expected_context_slot_names = []) {
+    $contexts = [];
+    foreach ($context_slot_names as $name) {
+      $contexts[] = new Context(new ContextDefinition('example'));
+    }
+
+    $expected_context_slot_names = $expected_context_slot_names ?: $context_slot_names;
+
+    $context_provider = $this->prophesize('\Drupal\Core\Plugin\Context\ContextProviderInterface');
+    $context_provider->getRunTimeContexts($expected_context_slot_names)
+      ->willReturn(array_combine($context_slot_names, $contexts));
+    $context_provider = $context_provider->reveal();
+    $this->container->set($service_id, $context_provider);
+
+    return $contexts;
+  }
+
+}
