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

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
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
Showing
with 101 additions and 33 deletions
......@@ -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.');
......
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