Skip to content
Snippets Groups Projects
Commit 9d6aed91 authored by catch's avatar catch
Browse files

Issue #3153956 by clayfreeman, Spokje, andypost, longwave: Remove code related...

Issue #3153956 by clayfreeman, Spokje, andypost, longwave: Remove code related to "context from configuration" that was deprecated in Drupal 9

(cherry picked from commit 3b4a427f)
parent 8e406249
No related branches found
No related tags found
24 merge requests!8506Draft: Issue #3456536 by ibrahim tameme,!5646Issue #3350972 by nod_: [random test failure]...,!5600Issue #3350972 by nod_: [random test failure]...,!5343Issue #3305066 by quietone, Rename RedirectLeadingSlashesSubscriber,!4350Issue #3307718: Implement xxHash for non-cryptographic use-cases,!3603#ISSUE 3346218 Add a different message on edit comment,!3555Issue #2473873: Views entity operations lack cacheability support, resulting in incorrect dropbuttons,!3494Issue #3327018 by Spokje, longwave, xjm, mondrake: Update PHPStan to 1.9.3 and...,!3410Issue #3340128: UserLoginForm::submitForm has some dead code,!3389Issue #3325184 by Spokje, andypost, xjm, smustgrave: $this->configFactory is...,!3381Issue #3332363: Refactor Claro's menus-and-lists stylesheet,!3307Issue #3326193: CKEditor 5 can grow past the viewport when there is a lot of content,!3236Issue #3332419: Refactor Claro's messages stylesheet,!3231Draft: Issue #3049525 by longwave, fougere, larowlan, kim.pepper, AaronBauman, Wim...,!3212Issue #3294003: Refactor Claro's entity-meta stylesheet,!3194Issue #3330981: Fix PHPStan L1 error "Relying on entity queries to check access by default is deprecated...",!3143Issue #3313342: [PHP 8.1] Deprecated function: strpos(): Passing null to parameter #1 LayoutBuilderUiCacheContext.php on line 28,!3024Issue #3307509: Empty option for views bulk form,!2972Issue #1845004: Replace custom password hashing library with PHP 5.5 password_hash(),!2719Issue #3110137: Remove Classy from core.,!2688Issue #3261452: [PP-1] Remove tracker module from core,!2437Issue #3238257 by hooroomoo, Wim Leers: Fragment link pointing to <textarea>...,!2296Issue #3100732: Allow specifying `meta` data on JSON:API objects,!1626Issue #3256642: Make life better for database drivers that extend another database driver
<?php
namespace Drupal\Component\Plugin;
use Drupal\Component\Plugin\Context\ContextInterface;
use Drupal\Component\Plugin\Definition\ContextAwarePluginDefinitionInterface;
use Drupal\Component\Plugin\Exception\ContextException;
use Drupal\Component\Plugin\Context\Context;
use Symfony\Component\Validator\ConstraintViolationList;
@trigger_error(__NAMESPACE__ . '\ContextAwarePluginBase is deprecated in drupal:9.1.0 and is removed from drupal:10.0.0 without replacement. See https://www.drupal.org/node/3120980', E_USER_DEPRECATED);
/**
* Base class for plugins that are context aware.
*
* @deprecated in drupal:9.1.0 and is removed from drupal:10.0.0 without
* replacement.
*
* @see https://www.drupal.org/node/3120980
*/
abstract class ContextAwarePluginBase extends PluginBase implements ContextAwarePluginInterface {
/**
* The data objects representing the context of this plugin.
*
* @var \Drupal\Component\Plugin\Context\ContextInterface[]
*/
protected $context = [];
/**
* Overrides \Drupal\Component\Plugin\PluginBase::__construct().
*
* Overrides the construction of context aware plugins to allow for
* unvalidated constructor based injection of contexts.
*
* @param array $configuration
* The plugin configuration, i.e. an array with configuration values keyed
* by configuration option name. The special key 'context' may be used to
* initialize the defined contexts by setting it to an array of context
* values keyed by context names.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition) {
$context_configuration = $configuration['context'] ?? [];
unset($configuration['context']);
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);
}
/**
* Creates context objects from any context mappings in configuration.
*
* @param array $context_configuration
* An associative array of context names and values.
*
* @return \Drupal\Component\Plugin\Context\ContextInterface[]
* An array of context objects.
*/
protected function createContextFromConfiguration(array $context_configuration) {
$contexts = [];
foreach ($context_configuration as $key => $value) {
$context_definition = $this->getContextDefinition($key);
$contexts[$key] = new Context($context_definition, $value);
}
return $contexts;
}
/**
* {@inheritdoc}
*/
public function getContextDefinitions() {
$definition = $this->getPluginDefinition();
if ($definition instanceof ContextAwarePluginDefinitionInterface) {
return $definition->getContextDefinitions();
}
else {
return !empty($definition['context_definitions']) ? $definition['context_definitions'] : [];
}
}
/**
* {@inheritdoc}
*/
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 getContexts() {
// Make sure all context objects are initialized.
foreach ($this->getContextDefinitions() as $name => $definition) {
$this->getContext($name);
}
return $this->context;
}
/**
* {@inheritdoc}
*/
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, ContextInterface $context) {
$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->context[$name] = new Context($this->getContextDefinition($name), $value);
return $this;
}
/**
* {@inheritdoc}
*/
public function validateContexts() {
$violations = new ConstraintViolationList();
// @todo: Implement symfony validator API to let the validator traverse
// and set property paths accordingly.
foreach ($this->getContexts() as $context) {
$violations->addAll($context->validate());
}
return $violations;
}
}
<?php
namespace Drupal\Core\Plugin;
use Drupal\Component\Plugin\ContextAwarePluginBase as ComponentContextAwarePluginBase;
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;
@trigger_error(__NAMESPACE__ . '\ContextAwarePluginBase is deprecated in drupal:9.1.0 and is removed from drupal:10.0.0. Use \Drupal\Core\Plugin\ContextAwarePluginTrait instead. 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;
/**
* {@inheritdoc}
*
* @return \Drupal\Core\Plugin\Context\ContextInterface[]
*/
protected function createContextFromConfiguration(array $context_configuration) {
// This method is overridden so that it will use
// \Drupal\Core\Plugin\Context\Context instead.
$contexts = [];
foreach ($context_configuration as $key => $value) {
$context_definition = $this->getContextDefinition($key);
$contexts[$key] = new Context($context_definition, $value);
}
return $contexts;
}
}
......@@ -28,18 +28,6 @@ trait ContextAwarePluginTrait {
*/
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}
*/
......@@ -58,28 +46,6 @@ public function getContexts() {
* 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));
......
<?php
namespace Drupal\KernelTests\Core\Plugin\Context;
use Drupal\Component\Plugin\Definition\ContextAwarePluginDefinitionInterface;
use Drupal\Component\Plugin\Definition\ContextAwarePluginDefinitionTrait;
use Drupal\Component\Plugin\Definition\PluginDefinition;
use Drupal\Component\Plugin\Exception\ContextException;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Plugin\Context\ContextDefinition;
use Drupal\Core\Plugin\ContextAwarePluginBase;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\Core\TypedData\Plugin\DataType\StringData;
use Drupal\Core\TypedData\TypedDataManagerInterface;
use Drupal\KernelTests\KernelTestBase;
/**
* @coversDefaultClass \Drupal\Core\Plugin\ContextAwarePluginBase
*
* @group Plugin
* @group legacy
*/
class ContextAwarePluginBaseTest extends KernelTestBase {
/**
* The plugin instance under test.
*
* @var \Drupal\Core\Plugin\ContextAwarePluginBase
*/
private $plugin;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$configuration = [
'context' => [
'nato_letter' => 'Alpha',
],
];
$plugin_definition = new TestPluginDefinition();
$plugin_definition->addContextDefinition('nato_letter', ContextDefinition::create('string'));
$this->plugin = $this->getMockBuilder(ContextAwarePluginBase::class)
->setConstructorArgs([$configuration, 'the_sisko', $plugin_definition])
->onlyMethods(['setContext'])
->getMockForAbstractClass();
}
/**
* @covers ::getContextDefinitions
*/
public function testGetContextDefinitions() {
$this->assertIsArray($this->plugin->getContextDefinitions());
}
/**
* @covers ::getContextDefinition
*/
public function testGetContextDefinition() {
// The context is not defined, so an exception will be thrown.
$this->expectException(ContextException::class);
$this->expectExceptionMessage('The person context is not a valid context.');
$this->plugin->getContextDefinition('person');
}
/**
* @covers ::getContextValue
*/
public function testGetContextValue() {
// Assert that the context value passed in the plugin configuration is
// available.
$this->assertSame('Alpha', $this->plugin->getContextValue('nato_letter'));
}
/**
* @covers ::setContextValue
*/
public function testSetContextValue() {
$typed_data_manager = $this->prophesize(TypedDataManagerInterface::class);
$container = new ContainerBuilder();
$container->set('typed_data_manager', $typed_data_manager->reveal());
\Drupal::setContainer($container);
$this->plugin->getPluginDefinition()->addContextDefinition('foo', new ContextDefinition('string'));
$this->plugin->expects($this->exactly(1))->method('setContext');
$this->plugin->setContextValue('foo', new StringData(new DataDefinition(), 'bar'));
}
}
class TestPluginDefinition extends PluginDefinition implements ContextAwarePluginDefinitionInterface {
use ContextAwarePluginDefinitionTrait;
}
......@@ -75,34 +75,6 @@ public function testGetContextValue() {
$this->assertSame('Alpha', $this->plugin->getContextValue('nato_letter'));
}
/**
* @covers ::getContextValue
* @group legacy
*/
public function testGetContextValueFromConfiguration() {
$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');
$configuration = [
'context' => [
'nato_letter' => 'Alpha',
],
];
$this->plugin = new TestContextAwarePlugin($configuration, 'the_sisko', $this->plugin->getPluginDefinition());
// Assert that the context value passed in the plugin configuration is
// available.
$this->assertSame('Alpha', $this->plugin->getContextValue('nato_letter'));
}
/**
* @covers ::getContextValue
* @group legacy
*/
public function testConfigurableGetContextValueFromConfiguration() {
$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');
// Assert that the context value passed in the plugin configuration is
// available.
$this->assertSame('Alpha', $this->configurablePlugin->getContextValue('nato_letter'));
}
/**
* @covers ::setContextValue
*/
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment