Skip to content
Snippets Groups Projects
Unverified Commit 8c90cbfd authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3029625 by tim.plunkett: Do not throw an exception when a context is...

Issue #3029625 by tim.plunkett: Do not throw an exception when a context is missing while applying context mapping to a plugin if that context was previously set
parent 687cf58d
No related branches found
No related tags found
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
......@@ -111,6 +111,10 @@ public function applyContextMapping(ContextAwarePluginInterface $plugin, $contex
$missing_value[] = $plugin_context_id;
}
}
elseif (($context = $plugin->getContext($context_id)) && $context->hasContextValue()) {
// Ignore mappings if the plugin has a value for a missing context.
unset($mappings[$plugin_context_id]);
}
elseif ($plugin_context_definition->isRequired()) {
// Collect required contexts that are missing.
$missing_value[] = $plugin_context_id;
......
......@@ -42,6 +42,25 @@ public function testApplyContextMapping() {
$this->assertSame($context, $result);
}
/**
* @covers ::applyContextMapping
*/
public function testApplyContextMappingAlreadyApplied() {
$entity = EntityTest::create([]);
$context_definition = EntityContextDefinition::fromEntity($entity);
$context = EntityContext::fromEntity($entity);
$definition = ['context_definitions' => ['a_context_id' => $context_definition]];
$plugin = new TestContextAwarePlugin([], 'test_plugin_id', $definition);
$plugin->setContext('a_context_id', $context);
(new ContextHandler())->applyContextMapping($plugin, []);
$result = $plugin->getContext('a_context_id');
$this->assertInstanceOf(EntityContext::class, $result);
$this->assertSame($context, $result);
}
}
/**
......
......@@ -18,6 +18,7 @@
use Drupal\Core\DependencyInjection\ClassResolverInterface;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\Context\Context;
use Drupal\Core\Plugin\Context\ContextDefinition;
use Drupal\Core\Plugin\Context\ContextHandler;
use Drupal\Core\Plugin\ContextAwarePluginInterface;
......@@ -378,8 +379,9 @@ public function testApplyContextMappingMissingRequired() {
->method('setContext');
// No context, so no cacheability metadata can be passed along.
$plugin->expects($this->never())
->method('getContext');
$plugin->expects($this->once())
->method('getContext')
->willReturn(new Context($context_definition));
$this->setExpectedException(MissingValueContextException::class, 'Required contexts without a value: hit');
$this->contextHandler->applyContextMapping($plugin, $contexts);
......@@ -413,8 +415,9 @@ public function testApplyContextMappingMissingNotRequired() {
->method('setContext');
// No context, so no cacheability metadata can be passed along.
$plugin->expects($this->never())
->method('getContext');
$plugin->expects($this->once())
->method('getContext')
->willReturn(new Context($context_definition));
$this->contextHandler->applyContextMapping($plugin, $contexts);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment