diff --git a/core/modules/block/block.install b/core/modules/block/block.install index 4a8bdec..24e0af2 100644 --- a/core/modules/block/block.install +++ b/core/modules/block/block.install @@ -34,13 +34,17 @@ function block_update_8001() { // saved in key value storage; see change record // https://www.drupal.org/node/2527840 for more explanation. - // These 3 are all the contexts that Drupal core provides. + // These are all the contexts that Drupal core provides. $context_service_id_map = [ 'language' => 'language.current_language_context', - 'node' => 'node.node_route_context', - 'user.current_user' => 'user.current_user_context', + 'node.node' => '@node.node_route_context:node', + 'user.current_user' => '@user.current_user_context:current_user', ]; + foreach (array_keys(\Drupal::languageManager()->getDefinedLanguageTypesInfo()) as $language_type_id) { + $context_service_id_map['language.' . $language_type_id] = '@language.current_language_context:' . $language_type_id; + } + // Using the entity API is never safe, given later update functions might, for // example, change an existing key. This means this update function might run // with code which already assumes the new key, but this update function does @@ -55,7 +59,7 @@ function block_update_8001() { if ($visibility = $block->get('visibility')) { foreach ($visibility as $condition_plugin_id => &$condition) { foreach ($condition['context_mapping'] as $key => $context) { - if (!isset($context_service_id_map[$key])) { + if (!isset($context_service_id_map[$context])) { // Remove the visibility condition for unknown context mapping // entries, so the update process itself runs through and users can // fix their block placements manually OR alternatively contributed @@ -67,8 +71,7 @@ function block_update_8001() { } // We replace the previous format of "{$context_id}" // with "@{$service_id}:{$unqualified_context_id}". - $new_context_id = explode('.', $context, 2); - $condition['context_mapping'][$key] = '@' . $context_service_id_map[$key] . ':' . $new_context_id[1]; + $condition['context_mapping'][$key] = $context_service_id_map[$context]; } } $block->set('visibility', $visibility); @@ -84,8 +87,8 @@ function block_update_8001() { } } - // Use the trusted data feature during hook_update_N() to mitigate issues - // with schema changes. + // Mark the resulting configuration as trusted data. This avoids issues with + // future schema changes. $block->save(TRUE); } diff --git a/core/modules/block/src/Tests/Update/BlockContextMappingUpdateTest.php b/core/modules/block/src/Tests/Update/BlockContextMappingUpdateTest.php index 55feb3d..da03fa0 100644 --- a/core/modules/block/src/Tests/Update/BlockContextMappingUpdateTest.php +++ b/core/modules/block/src/Tests/Update/BlockContextMappingUpdateTest.php @@ -23,7 +23,7 @@ class BlockContextMappingUpdateTest extends UpdatePathTestBase { /** * {@inheritdoc} */ - protected static $modules = ['block_test']; + protected static $modules = ['block_test', 'language']; /** * {@inheritdoc} @@ -83,6 +83,13 @@ public function testUpdateHookN() { $this->drupalGet('node/' . $article->id()); $this->assertNoRaw($block_title, 'Test block is not visible on a Article node as an anonymous user.'); + // Ensure that all the context mappings got updated properly. + $block = Block::load('testfor2354889'); + $visibility = $block->get('visibility'); + $this->assertEqual('@node.node_route_context:node', $visibility['node_type']['context_mapping']['node']); + $this->assertEqual('@user.current_user_context:current_user', $visibility['user_role']['context_mapping']['user']); + $this->assertEqual('@language.current_language_context:language_interface', $visibility['language']['context_mapping']['language']); + // Check that a block with invalid context is being disabled and that it can // still be edited afterward. $disabled_block = Block::load('thirdtestfor2354889'); diff --git a/core/modules/system/tests/fixtures/update/block.block.testfor2354889.yml b/core/modules/system/tests/fixtures/update/block.block.testfor2354889.yml index 376b04e..d2ac964 100644 --- a/core/modules/system/tests/fixtures/update/block.block.testfor2354889.yml +++ b/core/modules/system/tests/fixtures/update/block.block.testfor2354889.yml @@ -41,3 +41,11 @@ visibility: negate: false context_mapping: user: user.current_user + language: + id: language + langcodes: + en: en + de: de + negate: false + context_mapping: + language: language.language_interface