diff --git a/core/lib/Drupal/Core/Block/BlockBase.php b/core/lib/Drupal/Core/Block/BlockBase.php
index ba1026b..0a7f949 100644
--- a/core/lib/Drupal/Core/Block/BlockBase.php
+++ b/core/lib/Drupal/Core/Block/BlockBase.php
@@ -8,8 +8,10 @@
 namespace Drupal\Core\Block;
 
 use Drupal\block\BlockInterface;
+use Drupal\Component\Plugin\ContextAwarePluginInterface;
 use Drupal\Core\Access\AccessResult;
 use Drupal\Core\Form\FormStateInterface;
+use Drupal\Core\Plugin\ContextAwarePluginAssignmentTrait;
 use Drupal\Core\Plugin\ContextAwarePluginBase;
 use Drupal\Component\Utility\Unicode;
 use Drupal\Component\Utility\NestedArray;
@@ -29,6 +31,8 @@
  */
 abstract class BlockBase extends ContextAwarePluginBase implements BlockPluginInterface {
 
+  use ContextAwarePluginAssignmentTrait;
+
   /**
    * The transliteration service.
    *
@@ -160,6 +164,9 @@ protected function blockAccess(AccountInterface $account) {
    */
   public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
     $definition = $this->getPluginDefinition();
+    $temporary = $form_state->getTemporary();
+    $contexts = isset($temporary['gathered_contexts']) ? $temporary['gathered_contexts'] : [];
+    $form['context_mapping'] = $this->addContextAssignmentElement($this, $contexts);
     $form['provider'] = array(
       '#type' => 'value',
       '#value' => $definition['provider'],
diff --git a/core/modules/block/src/BlockForm.php b/core/modules/block/src/BlockForm.php
index 768cb42..ca6ba9e 100644
--- a/core/modules/block/src/BlockForm.php
+++ b/core/modules/block/src/BlockForm.php
@@ -310,7 +310,12 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
     $settings = (new FormState())->setValues($form_state->getValue('settings'));
 
     // Call the plugin submit handler.
-    $entity->getPlugin()->submitConfigurationForm($form, $settings);
+    $block = $entity->getPlugin();
+    $block->submitConfigurationForm($form, $settings);
+    if ($block instanceof ContextAwarePluginInterface) {
+      $context_mapping = isset($settings['context_mapping']) ? $settings['context_mapping'] : [];
+      $block->setContextMapping($context_mapping);
+    }
     // Update the original form values.
     $form_state->setValue('settings', $settings->getValues());
 
diff --git a/core/modules/block/src/BlockRepository.php b/core/modules/block/src/BlockRepository.php
index ee12060..2915015 100644
--- a/core/modules/block/src/BlockRepository.php
+++ b/core/modules/block/src/BlockRepository.php
@@ -77,6 +77,11 @@ public function getVisibleBlocksPerRegion(array $contexts) {
     $full = array();
     foreach ($this->blockStorage->loadByProperties(array('theme' => $this->getTheme())) as $block_id => $block) {
       /** @var \Drupal\block\BlockInterface $block */
+      $block->setAvailableContexts($contexts);
+      $block_plugin = $block->getPlugin();
+      if ($block_plugin instanceof ContextAwarePluginInterface) {
+        $this->contextHandler->applyContextMapping($block_plugin, $contexts);
+      }
       // Set the contexts on the block before checking access.
       if ($block->setContexts($contexts)->access('view')) {
         $full[$block->get('region')][$block_id] = $block;
