Unverified Commit bab4f6d3 authored by alexpott's avatar alexpott

Issue #3014949 by tim.plunkett: Deprecate 'context' on Block and Condition...

Issue #3014949 by tim.plunkett: Deprecate 'context' on Block and Condition plugin annotations in favor of 'context_definitions'
parent edfd5bbe
......@@ -72,7 +72,7 @@ public function getContextDefinitions() {
return $definition->getContextDefinitions();
}
else {
return !empty($definition['context']) ? $definition['context'] : [];
return !empty($definition['context_definitions']) ? $definition['context_definitions'] : [];
}
}
......@@ -86,8 +86,8 @@ public function getContextDefinition($name) {
return $definition->getContextDefinition($name);
}
}
elseif (!empty($definition['context'][$name])) {
return $definition['context'][$name];
elseif (!empty($definition['context_definitions'][$name])) {
return $definition['context_definitions'][$name];
}
throw new ContextException(sprintf("The %s context is not a valid context.", $name));
}
......
......@@ -14,14 +14,14 @@
* interactions through providing limits, and mapping contexts to appropriate
* plugins. Context definitions can be provided as such:
* @code
* context = {
* context_definitions = {
* "node" = @ContextDefinition("entity:node")
* }
* @endcode
*
* To add a label to a context definition use the "label" key:
* @code
* context = {
* context_definitions = {
* "node" = @ContextDefinition("entity:node", label = @Translation("Node"))
* }
* @endcode
......@@ -29,7 +29,7 @@
* Contexts are required unless otherwise specified. To make an optional
* context use the "required" key:
* @code
* context = {
* context_definitions = {
* "node" = @ContextDefinition("entity:node", required = FALSE, label = @Translation("Node"))
* }
* @endcode
......@@ -37,7 +37,7 @@
* To define multiple contexts, simply provide different key names in the
* context array:
* @code
* context = {
* context_definitions = {
* "artist" = @ContextDefinition("entity:node", label = @Translation("Artist")),
* "album" = @ContextDefinition("entity:node", label = @Translation("Album"))
* }
......@@ -45,7 +45,7 @@
*
* Specifying a default value for the context definition:
* @code
* context = {
* context_definitions = {
* "message" = @ContextDefinition("string",
* label = @Translation("Message"),
* default_value = @Translation("Checkout complete! Thank you for your purchase.")
......
......@@ -38,4 +38,26 @@ class Block extends Plugin {
*/
public $category = '';
/**
* An array of context definitions describing the context used by the plugin.
*
* The array is keyed by context names.
*
* @var \Drupal\Core\Annotation\ContextDefinition[]
*
* @deprecated Providing context definitions via the "context" key is
* deprecated in Drupal 8.7.x and will be removed before Drupal 9.0.0. Use
* the "context_definitions" key instead.
*/
public $context = [];
/**
* An array of context definitions describing the context used by the plugin.
*
* The array is keyed by context names.
*
* @var \Drupal\Core\Annotation\ContextDefinition[]
*/
public $context_definitions = [];
}
......@@ -54,9 +54,22 @@ class Condition extends Plugin {
* The array is keyed by context names.
*
* @var \Drupal\Core\Annotation\ContextDefinition[]
*
* @deprecated Providing context definitions via the "context" key is
* deprecated in Drupal 8.7.x and will be removed before Drupal 9.0.0. Use
* the "context_definitions" key instead.
*/
public $context = [];
/**
* An array of context definitions describing the context used by the plugin.
*
* The array is keyed by context names.
*
* @var \Drupal\Core\Annotation\ContextDefinition[]
*/
public $context_definitions = [];
/**
* The category under which the condition should listed in the UI.
*
......
......@@ -43,8 +43,8 @@ protected function getContextDefinitions($plugin_definition) {
if ($plugin_definition instanceof ContextAwarePluginDefinitionInterface) {
return $plugin_definition->getContextDefinitions();
}
if (is_array($plugin_definition) && isset($plugin_definition['context'])) {
return $plugin_definition['context'];
if (is_array($plugin_definition) && isset($plugin_definition['context_definitions'])) {
return $plugin_definition['context_definitions'];
}
return NULL;
}
......
......@@ -286,6 +286,7 @@ protected function findDefinitions() {
$this->processDefinition($definition, $plugin_id);
}
$this->alterDefinitions($definitions);
$this->fixContextAwareDefinitions($definitions);
// If this plugin was provided by a module that does not exist, remove the
// plugin definition.
foreach ($definitions as $plugin_id => $plugin_definition) {
......@@ -297,6 +298,38 @@ protected function findDefinitions() {
return $definitions;
}
/**
* Fix the definitions of context-aware plugins.
*
* @param array $definitions
* The array of plugin definitions.
*
* @todo Remove before Drupal 9.0.0.
*/
private function fixContextAwareDefinitions(array &$definitions) {
foreach ($definitions as $name => &$definition) {
if (is_array($definition) && (!empty($definition['context']) || !empty($definition['context_definitions']))) {
// Ensure the new definition key is available.
if (!isset($definition['context_definitions'])) {
$definition['context_definitions'] = [];
}
// If a context definition is defined with the old key, add it to the
// new key and trigger a deprecation error.
if (!empty($definition['context'])) {
$definition['context_definitions'] += $definition['context'];
@trigger_error('Providing context definitions via the "context" key is deprecated in Drupal 8.7.x and will be removed before Drupal 9.0.0. Use the "context_definitions" key instead.', E_USER_DEPRECATED);
}
// Copy the context definitions from the new key to the old key for
// backwards compatibility.
if (isset($definition['context'])) {
$definition['context'] = $definition['context_definitions'];
}
}
}
}
/**
* Extracts the provider from a plugin definition.
*
......
......@@ -12,7 +12,7 @@
* @Block(
* id = "test_context_aware",
* admin_label = @Translation("Test context-aware block"),
* context = {
* context_definitions = {
* "user" = @ContextDefinition("entity:user", required = FALSE)
* }
* )
......
......@@ -10,7 +10,7 @@
* @Block(
* id = "test_context_aware_unsatisfied",
* admin_label = @Translation("Test context-aware unsatisfied block"),
* context = {
* context_definitions = {
* "user" = @ContextDefinition("entity:foobar")
* }
* )
......
......@@ -15,7 +15,7 @@
* @Condition(
* id = "language",
* label = @Translation("Language"),
* context = {
* context_definitions = {
* "language" = @ContextDefinition("language", label = @Translation("Language"))
* }
* )
......
......@@ -100,7 +100,7 @@ public function getDerivativeDefinitions($base_plugin_definition) {
$context_definition = EntityContextDefinition::fromEntityType($entity_type)
->addConstraint('Bundle', [$bundle_id]);
$derivative['context'] = [
$derivative['context_definitions'] = [
'entity' => $context_definition,
];
......
......@@ -120,7 +120,7 @@ public function getDerivativeDefinitions($base_plugin_definition) {
$context_definition = EntityContextDefinition::fromEntityTypeId($entity_type_id)->setLabel($entity_type_labels[$entity_type_id]);
$context_definition->addConstraint('Bundle', [$bundle]);
$derivative['context'] = [
$derivative['context_definitions'] = [
'entity' => $context_definition,
];
......
......@@ -206,7 +206,7 @@ protected function getTestBlock(ProphecyInterface $entity_prophecy, array $confi
'category' => 'Test',
'admin_label' => 'Test Block',
'bundles' => ['entity_test'],
'context' => [
'context_definitions' => [
'entity' => EntityContextDefinition::fromEntityTypeId('entity_test')->setLabel('Test'),
],
];
......
......@@ -14,7 +14,7 @@
* @Condition(
* id = "node_type",
* label = @Translation("Node Bundle"),
* context = {
* context_definitions = {
* "node" = @ContextDefinition("entity:node", label = @Translation("Node"))
* }
* )
......
......@@ -10,7 +10,7 @@
* @Condition(
* id = "condition_test_dual_user",
* label = @Translation("Dual user"),
* context = {
* context_definitions = {
* "user1" = @ContextDefinition("entity:user", label = @Translation("User 1")),
* "user2" = @ContextDefinition("entity:user", label = @Translation("User 2"))
* }
......
......@@ -10,7 +10,7 @@
* @Condition(
* id = "condition_test_no_existing_type",
* label = @Translation("No existing type"),
* context = {
* context_definitions = {
* "no_existing_type" = @ContextDefinition("no_existing_type", label = @Translation("No existing type")),
* }
* )
......
......@@ -13,7 +13,7 @@
* @Condition(
* id = "condition_test_optional_context",
* label = @Translation("Optional context"),
* context = {
* context_definitions = {
* "node" = @ContextDefinition("entity:node", label = @Translation("Node"), required = FALSE),
* }
* )
......
......@@ -79,7 +79,7 @@ public function __construct() {
'id' => 'user_name',
'label' => t('User name'),
'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockUserNameBlock',
'context' => [
'context_definitions' => [
'user' => $this->createContextDefinition('entity:user', t('User')),
],
]);
......@@ -89,7 +89,7 @@ public function __construct() {
'id' => 'user_name_optional',
'label' => t('User name optional'),
'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockUserNameBlock',
'context' => [
'context_definitions' => [
'user' => $this->createContextDefinition('entity:user', t('User'), FALSE),
],
]);
......@@ -106,7 +106,7 @@ public function __construct() {
'id' => 'complex_context',
'label' => t('Complex context'),
'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockComplexContextBlock',
'context' => [
'context_definitions' => [
'user' => $this->createContextDefinition('entity:user', t('User')),
'node' => $this->createContextDefinition('entity:node', t('Node')),
],
......
......@@ -11,7 +11,7 @@
* @Condition(
* id = "user_role",
* label = @Translation("User Role"),
* context = {
* context_definitions = {
* "user" = @ContextDefinition("entity:user", label = @Translation("User"))
* }
* )
......
......@@ -115,7 +115,7 @@ public function getDerivativeDefinitions($base_plugin_definition) {
foreach ($display->getHandlers('argument') as $argument_name => $argument) {
/** @var \Drupal\views\Plugin\views\argument\ArgumentPluginBase $argument */
if ($context_definition = $argument->getContextDefinition()) {
$this->derivatives[$delta]['context'][$argument_name] = $context_definition;
$this->derivatives[$delta]['context_definitions'][$argument_name] = $context_definition;
}
}
......
......@@ -81,9 +81,9 @@ public function testBlockContext() {
// Check if context was correctly propagated to the block.
$definition = $this->container->get('plugin.manager.block')
->getDefinition('views_block:test_view_block_with_context-block_1');
$this->assertTrue($definition['context']['nid'] instanceof ContextDefinitionInterface);
$this->assertTrue($definition['context_definitions']['nid'] instanceof ContextDefinitionInterface);
/** @var \Drupal\Core\Plugin\Context\ContextDefinitionInterface $context */
$context = $definition['context']['nid'];
$context = $definition['context_definitions']['nid'];
$this->assertEqual($context->getDataType(), 'entity:node', 'Context definition data type is correct.');
$this->assertEqual($context->getLabel(), 'Content: ID', 'Context definition label is correct.');
$this->assertFalse($context->isRequired(), 'Context is not required.');
......@@ -128,23 +128,23 @@ public function testBlockContext() {
// based on the numeric plugin and the other based on numeric validation.
$definition = $this->container->get('plugin.manager.block')
->getDefinition('views_block:test_view_block_with_context-block_2');
$this->assertTrue($definition['context']['created'] instanceof ContextDefinitionInterface);
$this->assertTrue($definition['context_definitions']['created'] instanceof ContextDefinitionInterface);
/** @var \Drupal\Core\Plugin\Context\ContextDefinitionInterface $context */
$context = $definition['context']['created'];
$context = $definition['context_definitions']['created'];
$this->assertEqual($context->getDataType(), 'integer', 'Context definition data type is correct.');
$this->assertEqual($context->getLabel(), 'Content: Authored on', 'Context definition label is correct.');
$this->assertFalse($context->isRequired(), 'Context is not required.');
$this->assertTrue($definition['context']['vid'] instanceof ContextDefinitionInterface);
$this->assertTrue($definition['context_definitions']['vid'] instanceof ContextDefinitionInterface);
/** @var \Drupal\Core\Plugin\Context\ContextDefinitionInterface $context */
$context = $definition['context']['vid'];
$context = $definition['context_definitions']['vid'];
$this->assertEqual($context->getDataType(), 'integer', 'Context definition data type is correct.');
$this->assertEqual($context->getLabel(), 'Content: Revision ID', 'Context definition label is correct.');
$this->assertFalse($context->isRequired(), 'Context is not required.');
$this->assertTrue($definition['context']['title'] instanceof ContextDefinitionInterface);
$this->assertTrue($definition['context_definitions']['title'] instanceof ContextDefinitionInterface);
/** @var \Drupal\Core\Plugin\Context\ContextDefinitionInterface $context */
$context = $definition['context']['title'];
$context = $definition['context_definitions']['title'];
$this->assertEqual($context->getDataType(), 'string', 'Context definition data type is correct.');
$this->assertEqual($context->getLabel(), 'Content: Title', 'Context definition label is correct.');
$this->assertFalse($context->isRequired(), 'Context is not required.');
......
......@@ -92,7 +92,7 @@ protected function setUp() {
'id' => 'user_name',
'label' => 'User name',
'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockUserNameBlock',
'context' => [
'context_definitions' => [
'user' => EntityContextDefinition::fromEntityTypeId('user')->setLabel('User'),
],
],
......@@ -100,7 +100,7 @@ protected function setUp() {
'id' => 'user_name_optional',
'label' => 'User name optional',
'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockUserNameBlock',
'context' => [
'context_definitions' => [
'user' => EntityContextDefinition::fromEntityTypeId('user')->setLabel('User')->setRequired(FALSE),
],
],
......@@ -113,7 +113,7 @@ protected function setUp() {
'id' => 'complex_context',
'label' => 'Complex context',
'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockComplexContextBlock',
'context' => [
'context_definitions' => [
'user' => EntityContextDefinition::fromEntityTypeId('user')->setLabel('User'),
'node' => EntityContextDefinition::fromEntityTypeId('node')->setLabel('Node'),
],
......
......@@ -220,7 +220,7 @@ public function providerTestFilterPluginDefinitionsByContexts() {
$data[] = [FALSE, $plugins, $plugins];
$plugins = [
'expected_array_plugin' => ['context' => []],
'expected_array_plugin' => ['context_definitions' => []],
'expected_object_plugin' => new ContextAwarePluginDefinition(),
];
// No context, all plugins available.
......@@ -228,7 +228,7 @@ public function providerTestFilterPluginDefinitionsByContexts() {
$plugins = [
'expected_array_plugin' => [
'context' => ['context1' => new ContextDefinition('string')],
'context_definitions' => ['context1' => new ContextDefinition('string')],
],
'expected_object_plugin' => (new ContextAwarePluginDefinition())
->addContextDefinition('context1', new ContextDefinition('string')),
......@@ -241,7 +241,7 @@ public function providerTestFilterPluginDefinitionsByContexts() {
$mismatched_context_definition = (new ContextDefinition('expected_data_type'))->setConstraints(['mismatched_constraint_name' => 'mismatched_constraint_value']);
$plugins = [
'expected_array_plugin' => [
'context' => ['context1' => $mismatched_context_definition],
'context_definitions' => ['context1' => $mismatched_context_definition],
],
'expected_object_plugin' => (new ContextAwarePluginDefinition())
->addContextDefinition('context1', $mismatched_context_definition),
......@@ -253,7 +253,7 @@ public function providerTestFilterPluginDefinitionsByContexts() {
$optional_mismatched_context_definition->setRequired(FALSE);
$plugins = [
'expected_array_plugin' => [
'context' => ['context1' => $optional_mismatched_context_definition],
'context_definitions' => ['context1' => $optional_mismatched_context_definition],
],
'expected_object_plugin' => (new ContextAwarePluginDefinition())
->addContextDefinition('context1', $optional_mismatched_context_definition),
......@@ -264,7 +264,7 @@ public function providerTestFilterPluginDefinitionsByContexts() {
$expected_context_definition = (new ContextDefinition('string'))->setConstraints(['Blank' => []]);
$plugins = [
'expected_array_plugin' => [
'context' => ['context1' => $expected_context_definition],
'context_definitions' => ['context1' => $expected_context_definition],
],
'expected_object_plugin' => (new ContextAwarePluginDefinition())
->addContextDefinition('context1', $expected_context_definition),
......@@ -276,7 +276,7 @@ public function providerTestFilterPluginDefinitionsByContexts() {
$optional_expected_context_definition->setRequired(FALSE);
$plugins = [
'expected_array_plugin' => [
'context' => ['context1' => $optional_expected_context_definition],
'context_definitions' => ['context1' => $optional_expected_context_definition],
],
'expected_object_plugin' => (new ContextAwarePluginDefinition())
->addContextDefinition('context1', $optional_expected_context_definition),
......@@ -287,10 +287,10 @@ public function providerTestFilterPluginDefinitionsByContexts() {
$unexpected_context_definition = (new ContextDefinition('unexpected_data_type'))->setConstraints(['mismatched_constraint_name' => 'mismatched_constraint_value']);
$plugins = [
'unexpected_array_plugin' => [
'context' => ['context1' => $unexpected_context_definition],
'context_definitions' => ['context1' => $unexpected_context_definition],
],
'expected_array_plugin' => [
'context' => ['context2' => new ContextDefinition('string')],
'context_definitions' => ['context2' => new ContextDefinition('string')],
],
'unexpected_object_plugin' => (new ContextAwarePluginDefinition())
->addContextDefinition('context1', $unexpected_context_definition),
......
......@@ -6,6 +6,7 @@
use Drupal\Component\Plugin\Exception\PluginException;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\Context\ContextDefinition;
use Drupal\Core\Plugin\PluginFormInterface;
use Drupal\Tests\UnitTestCase;
......@@ -451,6 +452,35 @@ public function providerTestProcessDefinition() {
return $data;
}
/**
* @covers ::fixContextAwareDefinitions
*
* @group legacy
* @expectedDeprecation Providing context definitions via the "context" key is deprecated in Drupal 8.7.x and will be removed before Drupal 9.0.0. Use the "context_definitions" key instead.
*/
public function testFixContextAwareDefinitions() {
$first_definition = new ContextDefinition('first');
$second_definition = new ContextDefinition('bar');
$definitions = $this->expectedDefinitions;
$definitions['apple']['context'] = ['incorrect' => $first_definition];
$definitions['apple']['context_definitions'] = ['correct' => $second_definition];
$expected = $this->expectedDefinitions;
$expected['apple']['context']['correct'] = $second_definition;
$expected['apple']['context']['incorrect'] = $first_definition;
$expected['apple']['context_definitions']['correct'] = $second_definition;
$expected['apple']['context_definitions']['incorrect'] = $first_definition;
$module_handler = $this->prophesize(ModuleHandlerInterface::class);
$plugin_manager = new TestPluginManager($this->namespaces, $definitions, $module_handler->reveal(), NULL);
$reflection = new \ReflectionMethod($plugin_manager, 'fixContextAwareDefinitions');
$reflection->setAccessible(TRUE);
$reflection->invokeArgs($plugin_manager, [&$definitions]);
$this->assertSame($expected, $definitions);
}
}
class TestPluginManagerWithDefaults extends TestPluginManager {
......
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