Skip to content
Snippets Groups Projects
Commit e2fc75b3 authored by Mikael Meulle's avatar Mikael Meulle
Browse files

SourcePluginManager: bug fix to better handle context requirements at plugin discovery (suite)

parent 77eb86b8
No related branches found
No related tags found
No related merge requests found
......@@ -8,6 +8,7 @@ use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\Context\ContextAwarePluginManagerInterface;
use Drupal\Core\Plugin\Context\ContextAwarePluginManagerTrait;
use Drupal\Core\Plugin\Context\ContextHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\ui_patterns\Annotation\Source;
use Drupal\ui_patterns\Plugin\Context\RequirementsContextDefinition;
......@@ -27,6 +28,7 @@ class SourcePluginManager extends DefaultPluginManager implements ContextAwarePl
CacheBackendInterface $cache_backend,
ModuleHandlerInterface $module_handler,
protected PropTypePluginManager $prop_type_manager,
protected ContextHandlerInterface $context_handler,
) {
parent::__construct(
'Plugin/UiPatterns/Source',
......@@ -275,12 +277,17 @@ class SourcePluginManager extends DefaultPluginManager implements ContextAwarePl
*
* In addition to getDefinitionsForContexts(), this method
* checks context_definitions of plugins according to their keys.
* When required in def, a context must be present with same key.
* When required in def, a context must be present with same key,
* and it must satisfy the context definition.
*/
public function getDefinitionsForContextsRefined(array $contexts = []) {
$definitions = $this->getDefinitionsForContexts($contexts);
$definitions = array_filter($definitions, function ($definition) use ($contexts) {
$context_definitions = $definition["context_definitions"] ?? [];
$checked_context_by_keys = [];
foreach ($contexts as $key => $context) {
$checked_context_by_keys[$key] = [];
}
$definitions = array_filter($definitions, function ($definition) use ($contexts, &$checked_context_by_keys) {
$context_definitions = isset($definition['context_definitions']) ? $definition['context_definitions'] ?? [] : [];
foreach ($context_definitions as $key => $context_definition) {
if (!$context_definition->isRequired()) {
continue;
......@@ -288,6 +295,13 @@ class SourcePluginManager extends DefaultPluginManager implements ContextAwarePl
if (!array_key_exists($key, $contexts)) {
return FALSE;
}
$context_definition_key = hash('sha256', serialize($context_definition));
if (!isset($checked_context_by_keys[$key][$context_definition_key])) {
$checked_context_by_keys[$key][$context_definition_key] = $context_definitions->isSatisfiedBy($contexts[$key]);
}
if (!$checked_context_by_keys[$key][$context_definition_key]) {
return FALSE;
}
}
return TRUE;
});
......
......@@ -29,6 +29,7 @@ services:
parent: default_plugin_manager
arguments:
- "@plugin.manager.ui_patterns_prop_type"
- '@context.handler'
plugin.manager.ui_patterns_derivable_context:
class: Drupal\ui_patterns\DerivableContextPluginManager
parent: default_plugin_manager
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment