Commit 76c77b60 authored by catch's avatar catch

Issue #2484645 by giancarlosotelo, Arla: Assigning context mapping: allow...

Issue #2484645 by giancarlosotelo, Arla: Assigning context mapping: allow empty selection for optional contexts
parent d47dab1e
......@@ -53,7 +53,7 @@ protected function addContextAssignmentElement(ContextAwarePluginInterface $plug
];
}
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 @@ protected function addContextAssignmentElement(ContextAwarePluginInterface $plug
'#default_value' => !empty($assignments[$context_slot]) ? $assignments[$context_slot] : '',
'#description' => $definition->getDescription(),
];
if (!$definition->isRequired()) {
$element[$context_slot]['#empty_value'] = '';
}
}
}
return $element;
......
......@@ -93,7 +93,7 @@ public function getContextMapping() {
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 {
......
......@@ -207,7 +207,6 @@ public function testContextAwareBlocks() {
$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 @@ public function testContextAwareBlocks() {
$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.');
}
/**
......
......@@ -16,7 +16,7 @@
* 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 @@ public function build() {
return array(
'#prefix' => '<div id="' . $this->getPluginId() . '--username">',
'#suffix' => '</div>',
'#markup' => $user->getUsername(),
'#markup' => $user ? $user->getUsername() : 'No context mapping selected.' ,
);
}
......
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