Unverified Commit bc0cbe87 authored by alexpott's avatar alexpott

Issue #2273381 by clayfreeman, tim.plunkett, andypost, dawehner, EclipseGc,...

Issue #2273381 by clayfreeman, tim.plunkett, andypost, dawehner, EclipseGc, jungle, xjm, alexpott, Berdir: Convert ContextAwarePluginBase to traits
parent d72c5836
......@@ -42,6 +42,9 @@ public function __construct(array $configuration, $plugin_id, $plugin_definition
parent::__construct($configuration, $plugin_id, $plugin_definition);
if ($context_configuration) {
@trigger_error('Passing context values to plugins via configuration is deprecated in drupal:9.1.0 and will be removed before drupal:10.0.0. Instead, call ::setContextValue() on the plugin itself. See https://www.drupal.org/node/3120980', E_USER_DEPRECATED);
}
$this->context = $this->createContextFromConfiguration($context_configuration);
}
......
......@@ -3,7 +3,9 @@
namespace Drupal\Core\Block;
use Drupal\Core\Plugin\ContextAwarePluginAssignmentTrait;
use Drupal\Core\Plugin\ContextAwarePluginBase;
use Drupal\Core\Plugin\ContextAwarePluginInterface;
use Drupal\Core\Plugin\ContextAwarePluginTrait;
use Drupal\Core\Plugin\PluginBase;
use Drupal\Core\Plugin\PluginWithFormsInterface;
use Drupal\Core\Render\PreviewFallbackInterface;
......@@ -16,9 +18,10 @@
*
* @ingroup block_api
*/
abstract class BlockBase extends ContextAwarePluginBase implements BlockPluginInterface, PluginWithFormsInterface, PreviewFallbackInterface {
abstract class BlockBase extends PluginBase implements BlockPluginInterface, PluginWithFormsInterface, PreviewFallbackInterface, ContextAwarePluginInterface {
use BlockPluginTrait;
use ContextAwarePluginTrait;
use ContextAwarePluginAssignmentTrait;
}
......@@ -60,6 +60,7 @@ public function createInstance($plugin_id, array $configuration = []) {
// If we receive any context values via config set it into the plugin.
if (!empty($configuration['context'])) {
@trigger_error('Passing context values to plugins via configuration is deprecated in drupal:9.1.0 and will be removed before drupal:10.0.0. Instead, call ::setContextValue() on the plugin itself. See https://www.drupal.org/node/3120980', E_USER_DEPRECATED);
foreach ($configuration['context'] as $name => $context) {
$plugin->setContextValue($name, $context);
}
......
......@@ -2,13 +2,18 @@
namespace Drupal\Core\Executable;
use Drupal\Core\Plugin\ContextAwarePluginBase;
use Drupal\Component\Plugin\Exception\PluginException;
use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\Core\Plugin\ContextAwarePluginInterface;
use Drupal\Core\Plugin\ContextAwarePluginTrait;
use Drupal\Core\Plugin\PluginBase;
/**
* Provides the basic architecture for executable plugins.
*/
abstract class ExecutablePluginBase extends ContextAwarePluginBase implements ExecutableInterface {
abstract class ExecutablePluginBase extends PluginBase implements ExecutableInterface, CacheableDependencyInterface, ContextAwarePluginInterface {
use ContextAwarePluginTrait;
/**
* Gets an array of definitions of available configuration options.
......
......@@ -3,21 +3,20 @@
namespace Drupal\Core\Plugin;
use Drupal\Component\Plugin\ContextAwarePluginBase as ComponentContextAwarePluginBase;
use Drupal\Component\Plugin\Exception\ContextException;
use Drupal\Component\Plugin\PluginHelper;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\Plugin\Context\Context;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\TypedData\TypedDataTrait;
use Drupal\Component\Plugin\Context\ContextInterface as ComponentContextInterface;
use Drupal\Core\Plugin\Context\ContextInterface;
@trigger_error(__NAMESPACE__ . '\ContextAwarePluginBase is deprecated in drupal:9.1.0 and will be removed before drupal:10.0.0. Instead, use \Drupal\Core\Plugin\ContextAwarePluginTrait. See https://www.drupal.org/node/3120980', E_USER_DEPRECATED);
/**
* Base class for plugins that are context aware.
*/
abstract class ContextAwarePluginBase extends ComponentContextAwarePluginBase implements ContextAwarePluginInterface, CacheableDependencyInterface {
use ContextAwarePluginTrait;
use TypedDataTrait;
use StringTranslationTrait;
use DependencySerializationTrait;
......@@ -38,139 +37,4 @@ protected function createContextFromConfiguration(array $context_configuration)
return $contexts;
}
/**
* {@inheritdoc}
*
* This code is identical to the Component in order to pick up a different
* Context class.
*
* @return \Drupal\Core\Plugin\Context\ContextInterface
* The context object.
*/
public function getContext($name) {
// Check for a valid context value.
if (!isset($this->context[$name])) {
$this->context[$name] = new Context($this->getContextDefinition($name));
}
return $this->context[$name];
}
/**
* {@inheritdoc}
*/
public function setContext($name, ComponentContextInterface $context) {
// Check that the context passed is an instance of our extended interface.
if (!$context instanceof ContextInterface) {
throw new ContextException("Passed $name context must be an instance of \\Drupal\\Core\\Plugin\\Context\\ContextInterface");
}
parent::setContext($name, $context);
}
/**
* {@inheritdoc}
*/
public function setContextValue($name, $value) {
$this->setContext($name, Context::createFromContext($this->getContext($name), $value));
return $this;
}
/**
* {@inheritdoc}
*/
public function getContextMapping() {
$configuration = PluginHelper::isConfigurable($this) ? $this->getConfiguration() : $this->configuration;
return isset($configuration['context_mapping']) ? $configuration['context_mapping'] : [];
}
/**
* {@inheritdoc}
*/
public function setContextMapping(array $context_mapping) {
if (PluginHelper::isConfigurable($this)) {
$configuration = $this->getConfiguration();
$configuration['context_mapping'] = array_filter($context_mapping);
$this->setConfiguration($configuration);
}
else {
$this->configuration['context_mapping'] = $context_mapping;
}
return $this;
}
/**
* {@inheritdoc}
*
* @return \Drupal\Core\Plugin\Context\ContextDefinitionInterface[]
*/
public function getContextDefinitions() {
return parent::getContextDefinitions();
}
/**
* {@inheritdoc}
*
* @return \Drupal\Core\Plugin\Context\ContextDefinitionInterface
*/
public function getContextDefinition($name) {
return parent::getContextDefinition($name);
}
/**
* Wraps the context handler.
*
* @return \Drupal\Core\Plugin\Context\ContextHandlerInterface
*/
protected function contextHandler() {
return \Drupal::service('context.handler');
}
/**
* {@inheritdoc}
*/
public function getCacheContexts() {
$cache_contexts = [];
// Applied contexts can affect the cache contexts when this plugin is
// involved in caching, collect and return them.
foreach ($this->getContexts() as $context) {
/** @var $context \Drupal\Core\Cache\CacheableDependencyInterface */
if ($context instanceof CacheableDependencyInterface) {
$cache_contexts = Cache::mergeContexts($cache_contexts, $context->getCacheContexts());
}
}
return $cache_contexts;
}
/**
* {@inheritdoc}
*/
public function getCacheTags() {
$tags = [];
// Applied contexts can affect the cache tags when this plugin is
// involved in caching, collect and return them.
foreach ($this->getContexts() as $context) {
/** @var $context \Drupal\Core\Cache\CacheableDependencyInterface */
if ($context instanceof CacheableDependencyInterface) {
$tags = Cache::mergeTags($tags, $context->getCacheTags());
}
}
return $tags;
}
/**
* {@inheritdoc}
*/
public function getCacheMaxAge() {
$max_age = Cache::PERMANENT;
// Applied contexts can affect the cache max age when this plugin is
// involved in caching, collect and return them.
foreach ($this->getContexts() as $context) {
/** @var $context \Drupal\Core\Cache\CacheableDependencyInterface */
if ($context instanceof CacheableDependencyInterface) {
$max_age = Cache::mergeMaxAges($max_age, $context->getCacheMaxAge());
}
}
return $max_age;
}
}
......@@ -8,6 +8,7 @@
* An override of ContextAwarePluginInterface for documentation purposes.
*
* @see \Drupal\Component\Plugin\ContextAwarePluginInterface
* @see \Drupal\Core\Plugin\ContextAwarePluginTrait
*
* @ingroup plugin_api
*/
......
<?php
namespace Drupal\Core\Plugin;
use Drupal\Component\Plugin\ConfigurableInterface;
use Drupal\Component\Plugin\Context\ContextInterface as ComponentContextInterface;
use Drupal\Component\Plugin\Definition\ContextAwarePluginDefinitionInterface;
use Drupal\Component\Plugin\Exception\ContextException;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\Core\Plugin\Context\Context;
use Drupal\Core\Plugin\Context\ContextInterface;
use Symfony\Component\Validator\ConstraintViolationList;
/**
* Provides a trait to add context-aware functionality to plugins.
*
* @see \Drupal\Core\Plugin\ContextAwarePluginInterface
*
* @ingroup plugin_api
*/
trait ContextAwarePluginTrait {
/**
* The data objects representing the context of this plugin.
*
* @var \Drupal\Core\Plugin\Context\ContextInterface[]
*/
protected $context = [];
/**
* Tracks whether the context has been initialized from configuration.
*
* @var bool
*
* @todo Remove this in Drupal 10.0.x.
* See https://www.drupal.org/project/drupal/issues/3153956.
*
* @internal
*/
protected $initializedContextConfig = FALSE;
/**
* {@inheritdoc}
*/
public function getContexts() {
// Make sure all context objects are initialized.
foreach ($this->getContextDefinitions() as $name => $definition) {
$this->getContext($name);
}
return $this->context;
}
/**
* {@inheritdoc}
*
* @return \Drupal\Core\Plugin\Context\ContextInterface
* The context object.
*/
public function getContext($name) {
// @todo Remove this entire block in Drupal 10.0.x.
// See https://www.drupal.org/project/drupal/issues/3153956.
if (!$this->initializedContextConfig) {
$this->initializedContextConfig = TRUE;
if ($this instanceof ConfigurableInterface) {
$configuration = $this->getConfiguration();
}
else {
$reflection = new \ReflectionProperty($this, 'configuration');
$reflection->setAccessible(TRUE);
$configuration = $reflection->getValue($this);
}
if (isset($configuration['context'])) {
@trigger_error('Passing context values to plugins via configuration is deprecated in drupal:9.1.0 and will be removed before drupal:10.0.0. Instead, call ::setContextValue() on the plugin itself. See https://www.drupal.org/node/3120980', E_USER_DEPRECATED);
foreach ($configuration['context'] as $key => $value) {
$context_definition = $this->getContextDefinition($key);
$this->context[$key] = new Context($context_definition, $value);
}
}
}
// Check for a valid context value.
if (!isset($this->context[$name])) {
$this->context[$name] = new Context($this->getContextDefinition($name));
}
return $this->context[$name];
}
/**
* {@inheritdoc}
*/
public function setContext($name, ComponentContextInterface $context) {
// Check that the context passed is an instance of our extended interface.
if (!$context instanceof ContextInterface) {
throw new ContextException("Passed $name context must be an instance of \\Drupal\\Core\\Plugin\\Context\\ContextInterface");
}
$this->context[$name] = $context;
}
/**
* {@inheritdoc}
*/
public function getContextValues() {
$values = [];
foreach ($this->getContextDefinitions() as $name => $definition) {
$values[$name] = isset($this->context[$name]) ? $this->context[$name]->getContextValue() : NULL;
}
return $values;
}
/**
* {@inheritdoc}
*/
public function getContextValue($name) {
return $this->getContext($name)->getContextValue();
}
/**
* {@inheritdoc}
*/
public function setContextValue($name, $value) {
$this->setContext($name, Context::createFromContext($this->getContext($name), $value));
return $this;
}
/**
* {@inheritdoc}
*/
public function getContextMapping() {
$configuration = $this instanceof ConfigurableInterface ? $this->getConfiguration() : $this->configuration;
return isset($configuration['context_mapping']) ? $configuration['context_mapping'] : [];
}
/**
* {@inheritdoc}
*/
public function setContextMapping(array $context_mapping) {
if ($this instanceof ConfigurableInterface) {
$configuration = $this->getConfiguration();
$configuration['context_mapping'] = array_filter($context_mapping);
$this->setConfiguration($configuration);
}
else {
$this->configuration['context_mapping'] = $context_mapping;
}
return $this;
}
/**
* {@inheritdoc}
*/
abstract protected function getPluginDefinition();
/**
* {@inheritdoc}
*
* @return \Drupal\Core\Plugin\Context\ContextDefinitionInterface[]
*/
public function getContextDefinitions() {
$definition = $this->getPluginDefinition();
if ($definition instanceof ContextAwarePluginDefinitionInterface) {
return $definition->getContextDefinitions();
}
return !empty($definition['context_definitions']) ? $definition['context_definitions'] : [];
}
/**
* {@inheritdoc}
*
* @return \Drupal\Core\Plugin\Context\ContextDefinitionInterface
*/
public function getContextDefinition($name) {
$definition = $this->getPluginDefinition();
if ($definition instanceof ContextAwarePluginDefinitionInterface) {
if ($definition->hasContextDefinition($name)) {
return $definition->getContextDefinition($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));
}
/**
* {@inheritdoc}
*/
public function validateContexts() {
$violations = new ConstraintViolationList();
// @todo Implement the Symfony Validator component to let the validator
// traverse and set property paths accordingly.
// See https://www.drupal.org/project/drupal/issues/3153847.
foreach ($this->getContexts() as $context) {
$violations->addAll($context->validate());
}
return $violations;
}
/**
* {@inheritdoc}
*/
public function getCacheContexts() {
$cache_contexts = [];
// Applied contexts can affect the cache contexts when this plugin is
// involved in caching, collect and return them.
foreach ($this->getContexts() as $context) {
/** @var $context \Drupal\Core\Cache\CacheableDependencyInterface */
if ($context instanceof CacheableDependencyInterface) {
$cache_contexts = Cache::mergeContexts($cache_contexts, $context->getCacheContexts());
}
}
return $cache_contexts;
}
/**
* {@inheritdoc}
*/
public function getCacheTags() {
$tags = [];
// Applied contexts can affect the cache tags when this plugin is
// involved in caching, collect and return them.
foreach ($this->getContexts() as $context) {
/** @var $context \Drupal\Core\Cache\CacheableDependencyInterface */
if ($context instanceof CacheableDependencyInterface) {
$tags = Cache::mergeTags($tags, $context->getCacheTags());
}
}
return $tags;
}
/**
* {@inheritdoc}
*/
public function getCacheMaxAge() {
$max_age = Cache::PERMANENT;
// Applied contexts can affect the cache max age when this plugin is
// involved in caching, collect and return them.
foreach ($this->getContexts() as $context) {
/** @var $context \Drupal\Core\Cache\CacheableDependencyInterface */
if ($context instanceof CacheableDependencyInterface) {
$max_age = Cache::mergeMaxAges($max_age, $context->getCacheMaxAge());
}
}
return $max_age;
}
}
......@@ -2,9 +2,11 @@
namespace Drupal\layout_builder\Plugin\SectionStorage;
use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\Core\Plugin\Context\Context;
use Drupal\Core\Plugin\Context\ContextDefinition;
use Drupal\Core\Plugin\ContextAwarePluginBase;
use Drupal\Core\Plugin\ContextAwarePluginTrait;
use Drupal\Core\Plugin\PluginBase;
use Drupal\layout_builder\Routing\LayoutBuilderRoutesTrait;
use Drupal\layout_builder\Section;
use Drupal\layout_builder\SectionStorageInterface;
......@@ -13,8 +15,9 @@
/**
* Provides a base class for Section Storage types.
*/
abstract class SectionStorageBase extends ContextAwarePluginBase implements SectionStorageInterface, TempStoreIdentifierInterface {
abstract class SectionStorageBase extends PluginBase implements SectionStorageInterface, TempStoreIdentifierInterface, CacheableDependencyInterface {
use ContextAwarePluginTrait;
use LayoutBuilderRoutesTrait;
/**
......
......@@ -8,7 +8,8 @@
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\Context\Context;
use Drupal\Core\Plugin\Context\ContextDefinition;
use Drupal\Core\Plugin\ContextAwarePluginBase;
use Drupal\Core\Plugin\ContextAwarePluginTrait;
use Drupal\Core\Plugin\PluginBase;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Drupal\layout_builder\Plugin\SectionStorage\SectionStorageLocalTaskProviderInterface;
......@@ -29,8 +30,9 @@
* }
* )
*/
class SimpleConfigSectionStorage extends ContextAwarePluginBase implements SectionStorageInterface, SectionStorageLocalTaskProviderInterface, ContainerFactoryPluginInterface {
class SimpleConfigSectionStorage extends PluginBase implements SectionStorageInterface, SectionStorageLocalTaskProviderInterface, ContainerFactoryPluginInterface {
use ContextAwarePluginTrait;
use LayoutBuilderRoutesTrait;
use SectionStorageTrait;
......
......@@ -74,6 +74,16 @@ public function testConditions() {
// Check a greater than 2 bundles summary scenario.
$condition->setConfig('bundles', ['page' => 'page', 'article' => 'article', 'test' => 'test']);
$this->assertEqual('The node bundle is page, article or test', $condition->summary());
}
/**
* @group legacy
*/
public function testLegacy() {
$this->expectDeprecation('Passing context values to plugins via configuration is deprecated in drupal:9.1.0 and will be removed before drupal:10.0.0. Instead, call ::setContextValue() on the plugin itself. See https://www.drupal.org/node/3120980');
$manager = $this->container->get('plugin.manager.condition');
$article = Node::create(['type' => 'article', 'title' => $this->randomMachineName(), 'uid' => 1]);
$article->save();
// Test Constructor injection.
$condition = $manager->createInstance('node_type', ['bundles' => ['article' => 'article'], 'context' => ['node' => $article]]);
......
......@@ -2,14 +2,18 @@
namespace Drupal\plugin_test\Plugin\plugin_test\mock_block;
use Drupal\Core\Plugin\ContextAwarePluginBase;
use Drupal\Core\Plugin\ContextAwarePluginInterface;
use Drupal\Core\Plugin\ContextAwarePluginTrait;
use Drupal\Core\Plugin\PluginBase;
/**
* Implementation of a complex context plugin used by Plugin API context tests.
*
* @see \Drupal\plugin_test\Plugin\MockBlockManager
*/
class MockComplexContextBlock extends ContextAwarePluginBase {
class MockComplexContextBlock extends PluginBase implements ContextAwarePluginInterface {
use ContextAwarePluginTrait;
public function getTitle() {
$user = $this->getContextValue('user');
......
......@@ -2,14 +2,18 @@
namespace Drupal\plugin_test\Plugin\plugin_test\mock_block;
use Drupal\Core\Plugin\ContextAwarePluginBase;
use Drupal\Core\Plugin\ContextAwarePluginInterface;
use Drupal\Core\Plugin\ContextAwarePluginTrait;
use Drupal\Core\Plugin\PluginBase;
/**
* Implementation of a user name block plugin used by Plugin API context test.
*
* @see \Drupal\plugin_test\Plugin\MockBlockManager
*/
class MockUserNameBlock extends ContextAwarePluginBase {
class MockUserNameBlock extends PluginBase implements ContextAwarePluginInterface {
use ContextAwarePluginTrait;
public function getTitle() {
$user = $this->getContextValue('user');
......
......@@ -2,7 +2,9 @@
namespace Drupal\plugin_test\Plugin\plugin_test\mock_block;
use Drupal\Core\Plugin\ContextAwarePluginBase;
use Drupal\Core\Plugin\ContextAwarePluginInterface;
use Drupal\Core\Plugin\ContextAwarePluginTrait;
use Drupal\Core\Plugin\PluginBase;
/**
* Implementation of a String TypedData contextual block plugin used by Plugin
......@@ -10,7 +12,9 @@
*
* @see \Drupal\plugin_test\Plugin\MockBlockManager
*/
class TypedDataStringBlock extends ContextAwarePluginBase {
class TypedDataStringBlock extends PluginBase implements ContextAwarePluginInterface {
use ContextAwarePluginTrait;
public function getTitle() {
return $this->getContextValue('string');
......