diff --git a/core/lib/Drupal/Core/Annotation/ContextDefinition.php b/core/lib/Drupal/Core/Annotation/ContextDefinition.php index 6a46c01e5ac6923b04fd2de865090da700356740..0284e9230eb515d1dcdb1f8e0db72df723c4dd54 100644 --- a/core/lib/Drupal/Core/Annotation/ContextDefinition.php +++ b/core/lib/Drupal/Core/Annotation/ContextDefinition.php @@ -119,6 +119,12 @@ public function __construct(array $values) { $class = $this->getDefinitionClass($values); $this->definition = new $class($values['value'], $values['label'], $values['required'], $values['multiple'], $values['description'], $values['default_value']); + + if (isset($values['constraints'])) { + foreach ($values['constraints'] as $constraint_name => $options) { + $this->definition->addConstraint($constraint_name, $options); + } + } } /** diff --git a/core/modules/block/tests/modules/block_test/src/Plugin/Block/TestContextAwareBlock.php b/core/modules/block/tests/modules/block_test/src/Plugin/Block/TestContextAwareBlock.php index 178643c2ec3ea10fc71a487475b2ee5fb237cf0c..33c0a71b1e288e173132e0621d710c2c0e72f48e 100644 --- a/core/modules/block/tests/modules/block_test/src/Plugin/Block/TestContextAwareBlock.php +++ b/core/modules/block/tests/modules/block_test/src/Plugin/Block/TestContextAwareBlock.php @@ -13,7 +13,9 @@ * id = "test_context_aware", * admin_label = @Translation("Test context-aware block"), * context_definitions = { - * "user" = @ContextDefinition("entity:user", required = FALSE) + * "user" = @ContextDefinition("entity:user", required = FALSE, + * constraints = { "NotNull" = {} } + * ), * } * ) */ diff --git a/core/modules/layout_builder/layout_builder.module b/core/modules/layout_builder/layout_builder.module index 726eae208aab77b8f0f34d907e51706f737cfcc6..8d19c35badc6b0d168c5651e273ab7c9038cede5 100644 --- a/core/modules/layout_builder/layout_builder.module +++ b/core/modules/layout_builder/layout_builder.module @@ -255,19 +255,6 @@ function layout_builder_plugin_filter_block__block_ui_alter(array &$definitions, } } -/** - * Implements hook_layout_builder_section_storage_alter(). - */ -function layout_builder_layout_builder_section_storage_alter(array &$definitions) { - // @todo Until https://www.drupal.org/node/3016420 is resolved, context - // definition annotations cannot specify any constraints. Alter - // \Drupal\layout_builder\Plugin\SectionStorage\OverridesSectionStorage to - // add the constraint of having the required layout field. - /** @var \Drupal\layout_builder\SectionStorage\SectionStorageDefinition[] $definitions */ - $definitions['overrides']->getContextDefinition('entity') - ->addConstraint('EntityHasField', OverridesSectionStorage::FIELD_NAME); -} - /** * Implements hook_plugin_filter_TYPE__CONSUMER_alter(). */ diff --git a/core/modules/layout_builder/src/Plugin/SectionStorage/OverridesSectionStorage.php b/core/modules/layout_builder/src/Plugin/SectionStorage/OverridesSectionStorage.php index bd9c8690e1a5ae1b4dc5a7ab9f4c431d9ec43098..fa440b300df58b70bd583876baac1702216bba1e 100644 --- a/core/modules/layout_builder/src/Plugin/SectionStorage/OverridesSectionStorage.php +++ b/core/modules/layout_builder/src/Plugin/SectionStorage/OverridesSectionStorage.php @@ -35,7 +35,9 @@ * id = "overrides", * weight = -20, * context_definitions = { - * "entity" = @ContextDefinition("entity"), + * "entity" = @ContextDefinition("entity", constraints = { + * "EntityHasField" = \Drupal\layout_builder\Plugin\SectionStorage\OverridesSectionStorage::FIELD_NAME, + * }), * "view_mode" = @ContextDefinition("string"), * } * ) diff --git a/core/tests/Drupal/KernelTests/Core/Plugin/Annotation/ContextDefinitionTest.php b/core/tests/Drupal/KernelTests/Core/Plugin/Annotation/ContextDefinitionTest.php new file mode 100644 index 0000000000000000000000000000000000000000..ecb3c1fb82e4c9bd407df43e17b62d83ae0f8320 --- /dev/null +++ b/core/tests/Drupal/KernelTests/Core/Plugin/Annotation/ContextDefinitionTest.php @@ -0,0 +1,30 @@ +<?php + +namespace Drupal\KernelTests\Core\Plugin\Annotation; + +use Drupal\Core\Plugin\Context\ContextDefinition; +use Drupal\KernelTests\KernelTestBase; + +/** + * @coversDefaultClass \Drupal\Core\Annotation\ContextDefinition + * @group Plugin + */ +class ContextDefinitionTest extends KernelTestBase { + + /** + * {@inheritdoc} + */ + protected static $modules = ['block_test']; + + /** + * Tests adding constraints via annotations. + */ + public function testConstraints() { + $definition = $this->container->get('plugin.manager.block')->getDefinition('test_context_aware'); + $this->assertArrayHasKey('context_definitions', $definition); + $this->assertArrayHasKey('user', $definition['context_definitions']); + $this->assertInstanceOf(ContextDefinition::class, $definition['context_definitions']['user']); + $this->assertEquals(['NotNull' => []], $definition['context_definitions']['user']->getConstraints()); + } + +}