summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNathaniel Catchpole2015-10-23 19:48:58 (GMT)
committerNathaniel Catchpole2015-10-23 19:48:58 (GMT)
commit76c77b600b40569667a3dbf593873bf6735e95f8 (patch)
tree17e5f6a4f6ff6b171c01b9690968f6d06f0d954b
parentd47dab1e30f1461708e4f4612f2619a1682b1bac (diff)
Issue #2484645 by giancarlosotelo, Arla: Assigning context mapping: allow empty selection for optional contexts
-rw-r--r--core/lib/Drupal/Core/Plugin/ContextAwarePluginAssignmentTrait.php5
-rw-r--r--core/lib/Drupal/Core/Plugin/ContextAwarePluginBase.php2
-rw-r--r--core/modules/block/src/Tests/BlockUiTest.php10
-rw-r--r--core/modules/block/tests/modules/block_test/src/Plugin/Block/TestContextAwareBlock.php4
4 files changed, 16 insertions, 5 deletions
diff --git a/core/lib/Drupal/Core/Plugin/ContextAwarePluginAssignmentTrait.php b/core/lib/Drupal/Core/Plugin/ContextAwarePluginAssignmentTrait.php
index f9e349e..012bd30 100644
--- a/core/lib/Drupal/Core/Plugin/ContextAwarePluginAssignmentTrait.php
+++ b/core/lib/Drupal/Core/Plugin/ContextAwarePluginAssignmentTrait.php
@@ -53,7 +53,7 @@ trait ContextAwarePluginAssignmentTrait {
];
}
- if (count($options) > 1) {
+ if (count($options) > 1 || !$definition->isRequired()) {
$assignments = $plugin->getContextMapping();
$element[$context_slot] = [
'#title' => $definition->getLabel() ?: $this->t('Select a @context value:', ['@context' => $context_slot]),
@@ -63,6 +63,9 @@ trait ContextAwarePluginAssignmentTrait {
'#default_value' => !empty($assignments[$context_slot]) ? $assignments[$context_slot] : '',
'#description' => $definition->getDescription(),
];
+ if (!$definition->isRequired()) {
+ $element[$context_slot]['#empty_value'] = '';
+ }
}
}
return $element;
diff --git a/core/lib/Drupal/Core/Plugin/ContextAwarePluginBase.php b/core/lib/Drupal/Core/Plugin/ContextAwarePluginBase.php
index 21ee311..219dd37 100644
--- a/core/lib/Drupal/Core/Plugin/ContextAwarePluginBase.php
+++ b/core/lib/Drupal/Core/Plugin/ContextAwarePluginBase.php
@@ -93,7 +93,7 @@ abstract class ContextAwarePluginBase extends ComponentContextAwarePluginBase im
public function setContextMapping(array $context_mapping) {
if ($this instanceof ConfigurablePluginInterface) {
$configuration = $this->getConfiguration();
- $configuration['context_mapping'] = $context_mapping;
+ $configuration['context_mapping'] = array_filter($context_mapping);
$this->setConfiguration($configuration);
}
else {
diff --git a/core/modules/block/src/Tests/BlockUiTest.php b/core/modules/block/src/Tests/BlockUiTest.php
index 5320942..25b7942 100644
--- a/core/modules/block/src/Tests/BlockUiTest.php
+++ b/core/modules/block/src/Tests/BlockUiTest.php
@@ -207,7 +207,6 @@ class BlockUiTest extends WebTestBase {
$this->assertTrue(!empty($elements), 'The context-aware test block appears.');
$definition = \Drupal::service('plugin.manager.block')->getDefinition('test_context_aware');
$this->assertTrue(!empty($definition), 'The context-aware test block exists.');
-
$edit = [
'region' => 'content',
'settings[context_mapping][user]' => '@block_test.multiple_static_context:user2',
@@ -217,6 +216,15 @@ class BlockUiTest extends WebTestBase {
$this->drupalGet('');
$this->assertText('Test context-aware block');
$this->assertRaw($expected_text);
+
+ // Test context mapping allows empty selection for optional contexts.
+ $this->drupalGet('admin/structure/block/manage/testcontextawareblock');
+ $edit = [
+ 'settings[context_mapping][user]' => '',
+ ];
+ $this->drupalPostForm(NULL, $edit, 'Save block');
+ $this->drupalGet('');
+ $this->assertText('No context mapping selected.');
}
/**
diff --git a/core/modules/block/tests/modules/block_test/src/Plugin/Block/TestContextAwareBlock.php b/core/modules/block/tests/modules/block_test/src/Plugin/Block/TestContextAwareBlock.php
index c4223d0..013dbae 100644
--- a/core/modules/block/tests/modules/block_test/src/Plugin/Block/TestContextAwareBlock.php
+++ b/core/modules/block/tests/modules/block_test/src/Plugin/Block/TestContextAwareBlock.php
@@ -16,7 +16,7 @@ use Drupal\Core\Block\BlockBase;
* id = "test_context_aware",
* admin_label = @Translation("Test context-aware block"),
* context = {
- * "user" = @ContextDefinition("entity:user")
+ * "user" = @ContextDefinition("entity:user", required = FALSE)
* }
* )
*/
@@ -31,7 +31,7 @@ class TestContextAwareBlock extends BlockBase {
return array(
'#prefix' => '<div id="' . $this->getPluginId() . '--username">',
'#suffix' => '</div>',
- '#markup' => $user->getUsername(),
+ '#markup' => $user ? $user->getUsername() : 'No context mapping selected.' ,
);
}