Unverified Commit 55f13aac authored by alexpott's avatar alexpott

Issue #2809305 by Upchuk, Pavan B S, Jo Fitzgerald, tim.plunkett, Berdir:...

Issue #2809305 by Upchuk, Pavan B S, Jo Fitzgerald, tim.plunkett, Berdir: Block Context assignment form element shows even if no options are available
parent 50d98d74
......@@ -48,7 +48,10 @@ protected function addContextAssignmentElement(ContextAwarePluginInterface $plug
];
}
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]),
......
<?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',
];
}
}
......@@ -239,6 +239,13 @@ public function testContextAwareBlocks() {
$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 = [
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment