Commit 9e18995a authored by Gábor Hojtsy's avatar Gábor Hojtsy

Issue #2982626 by phenaproxima, tim.plunkett, EclipseGc:...

Issue #2982626 by phenaproxima, tim.plunkett, EclipseGc: ContextAwarePluginBase is incompatible with ContextAwarePluginDefinitionInterface
parent 920eb067
......@@ -3,6 +3,7 @@
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;
......@@ -67,7 +68,12 @@ protected function createContextFromConfiguration(array $context_configuration)
*/
public function getContextDefinitions() {
$definition = $this->getPluginDefinition();
return !empty($definition['context']) ? $definition['context'] : [];
if ($definition instanceof ContextAwarePluginDefinitionInterface) {
return $definition->getContextDefinitions();
}
else {
return !empty($definition['context']) ? $definition['context'] : [];
}
}
/**
......@@ -75,10 +81,15 @@ public function getContextDefinitions() {
*/
public function getContextDefinition($name) {
$definition = $this->getPluginDefinition();
if (empty($definition['context'][$name])) {
throw new ContextException(sprintf("The %s context is not a valid context.", $name));
if ($definition instanceof ContextAwarePluginDefinitionInterface) {
if ($definition->hasContextDefinition($name)) {
return $definition->getContextDefinition($name);
}
}
elseif (!empty($definition['context'][$name])) {
return $definition['context'][$name];
}
return $definition['context'][$name];
throw new ContextException(sprintf("The %s context is not a valid context.", $name));
}
/**
......
<?php
namespace Drupal\Tests\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\Plugin\ContextAwarePluginBase;
use Drupal\Tests\UnitTestCase;
/**
* @coversDefaultClass \Drupal\Core\Plugin\ContextAwarePluginBase
* @group Plugin
*/
class ContextAwarePluginBaseTest extends UnitTestCase {
/**
* The plugin instance under test.
*
* @var \Drupal\Core\Plugin\ContextAwarePluginBase
*/
private $plugin;
/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp();
$this->plugin = new TestContextAwarePlugin([], 'the_sisko', new TestPluginDefinition());
}
/**
* @covers ::getContextDefinitions
*/
public function testGetContextDefinitions() {
$this->assertInternalType('array', $this->plugin->getContextDefinitions());
}
/**
* @covers ::getContextDefinition
*/
public function testGetContextDefinition() {
// The context is not defined, so an exception will be thrown.
$this->setExpectedException(ContextException::class, 'The person context is not a valid context.');
$this->plugin->getContextDefinition('person');
}
}
class TestPluginDefinition extends PluginDefinition implements ContextAwarePluginDefinitionInterface {
use ContextAwarePluginDefinitionTrait;
}
class TestContextAwarePlugin extends ContextAwarePluginBase {}
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