summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Pott2018-12-12 13:11:27 (GMT)
committerAlex Pott2018-12-12 13:11:36 (GMT)
commitc490e4029c0400b51c7a337c82a1085a8143dfc1 (patch)
tree8b581f3b9687ae64e673aed00a14eaed900a1cd3
parent4047cb0e27e07f4eb92b3b7533a62a5ef48ceb4c (diff)
Issue #2809305 by Upchuk, Pavan B S, Jo Fitzgerald, tim.plunkett, Berdir: Block Context assignment form element shows even if no options are availableHEAD8.6.x
(cherry picked from commit 55f13aacb5a348c5db49042d2abca04bc126eda4)
-rw-r--r--core/lib/Drupal/Core/Plugin/ContextAwarePluginAssignmentTrait.php5
-rw-r--r--core/modules/block/tests/modules/block_test/src/Plugin/Block/TestContextAwareNoValidContextOptionsBlock.php29
-rw-r--r--core/modules/block/tests/src/Functional/BlockUiTest.php7
3 files changed, 40 insertions, 1 deletions
diff --git a/core/lib/Drupal/Core/Plugin/ContextAwarePluginAssignmentTrait.php b/core/lib/Drupal/Core/Plugin/ContextAwarePluginAssignmentTrait.php
index 31ff649..75cbc68 100644
--- a/core/lib/Drupal/Core/Plugin/ContextAwarePluginAssignmentTrait.php
+++ b/core/lib/Drupal/Core/Plugin/ContextAwarePluginAssignmentTrait.php
@@ -48,7 +48,10 @@ trait ContextAwarePluginAssignmentTrait {
];
}
- if (count($options) > 1 || !$definition->isRequired()) {
+ // Show the context selector only if there is more than 1 option to choose
+ // from. Also, show if there is a single option but the plugin does not
+ // require a context.
+ if (count($options) > 1 || (count($options) == 1 && !$definition->isRequired())) {
$assignments = $plugin->getContextMapping();
$element[$context_slot] = [
'#title' => $definition->getLabel() ?: $this->t('Select a @context value:', ['@context' => $context_slot]),
diff --git a/core/modules/block/tests/modules/block_test/src/Plugin/Block/TestContextAwareNoValidContextOptionsBlock.php b/core/modules/block/tests/modules/block_test/src/Plugin/Block/TestContextAwareNoValidContextOptionsBlock.php
new file mode 100644
index 0000000..234bf9a
--- /dev/null
+++ b/core/modules/block/tests/modules/block_test/src/Plugin/Block/TestContextAwareNoValidContextOptionsBlock.php
@@ -0,0 +1,29 @@
+<?php
+
+namespace Drupal\block_test\Plugin\Block;
+
+use Drupal\Core\Block\BlockBase;
+
+/**
+ * Provides a context-aware block that uses a not-passed, non-required context.
+ *
+ * @Block(
+ * id = "test_context_aware_no_valid_context_options",
+ * admin_label = @Translation("Test context-aware block - no valid context options"),
+ * context_definitions = {
+ * "email" = @ContextDefinition("email", required = FALSE)
+ * }
+ * )
+ */
+class TestContextAwareNoValidContextOptionsBlock extends BlockBase {
+
+ /**
+ * {@inheritdoc}
+ */
+ public function build() {
+ return [
+ '#markup' => 'Rendered block with no valid context options',
+ ];
+ }
+
+}
diff --git a/core/modules/block/tests/src/Functional/BlockUiTest.php b/core/modules/block/tests/src/Functional/BlockUiTest.php
index e6c1875..2b79260 100644
--- a/core/modules/block/tests/src/Functional/BlockUiTest.php
+++ b/core/modules/block/tests/src/Functional/BlockUiTest.php
@@ -239,6 +239,13 @@ class BlockUiTest extends BrowserTestBase {
$this->assertText('User context found.');
$this->assertRaw($expected_text);
+ // Test context mapping form element is not visible if there are no valid
+ // context options for the block (the test_context_aware_no_valid_context_options
+ // block has one context defined which is not available for it on the
+ // Block Layout interface).
+ $this->drupalGet('admin/structure/block/add/test_context_aware_no_valid_context_options/classy');
+ $this->assertSession()->fieldNotExists('edit-settings-context-mapping-email');
+
// Test context mapping allows empty selection for optional contexts.
$this->drupalGet('admin/structure/block/manage/testcontextawareblock');
$edit = [