Skip to content

Commit c222b1e

Browse files
committed
Issue #2809305 by Upchuk, Pavan B S, Jo Fitzgerald, tim.plunkett, Berdir: Block Context assignment form element shows even if no options are available
(cherry picked from commit 55f13aa)
1 parent 8dbf42a commit c222b1e

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

lib/Drupal/Core/Plugin/ContextAwarePluginAssignmentTrait.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ protected function addContextAssignmentElement(ContextAwarePluginInterface $plug
4848
];
4949
}
5050

51-
if (count($options) > 1 || !$definition->isRequired()) {
51+
// Show the context selector only if there is more than 1 option to choose
52+
// from. Also, show if there is a single option but the plugin does not
53+
// require a context.
54+
if (count($options) > 1 || (count($options) == 1 && !$definition->isRequired())) {
5255
$assignments = $plugin->getContextMapping();
5356
$element[$context_slot] = [
5457
'#title' => $definition->getLabel() ?: $this->t('Select a @context value:', ['@context' => $context_slot]),
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Drupal\block_test\Plugin\Block;
4+
5+
use Drupal\Core\Block\BlockBase;
6+
7+
/**
8+
* Provides a context-aware block that uses a not-passed, non-required context.
9+
*
10+
* @Block(
11+
* id = "test_context_aware_no_valid_context_options",
12+
* admin_label = @Translation("Test context-aware block - no valid context options"),
13+
* context_definitions = {
14+
* "email" = @ContextDefinition("email", required = FALSE)
15+
* }
16+
* )
17+
*/
18+
class TestContextAwareNoValidContextOptionsBlock extends BlockBase {
19+
20+
/**
21+
* {@inheritdoc}
22+
*/
23+
public function build() {
24+
return [
25+
'#markup' => 'Rendered block with no valid context options',
26+
];
27+
}
28+
29+
}

modules/block/tests/src/Functional/BlockUiTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,13 @@ public function testContextAwareBlocks() {
239239
$this->assertText('User context found.');
240240
$this->assertRaw($expected_text);
241241

242+
// Test context mapping form element is not visible if there are no valid
243+
// context options for the block (the test_context_aware_no_valid_context_options
244+
// block has one context defined which is not available for it on the
245+
// Block Layout interface).
246+
$this->drupalGet('admin/structure/block/add/test_context_aware_no_valid_context_options/classy');
247+
$this->assertSession()->fieldNotExists('edit-settings-context-mapping-email');
248+
242249
// Test context mapping allows empty selection for optional contexts.
243250
$this->drupalGet('admin/structure/block/manage/testcontextawareblock');
244251
$edit = [

0 commit comments

Comments
 (0)