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

Issue #3016420 by tim.plunkett, EclipseGc: Allow context definition...

Issue #3016420 by tim.plunkett, EclipseGc: Allow context definition annotations to specify constraints
parent 4b2c32c4
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
......@@ -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);
}
}
}
/**
......
......@@ -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" = {} }
* ),
* }
* )
*/
......
......@@ -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().
*/
......
......@@ -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"),
* }
* )
......
<?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());
}
}
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