diff --git a/core/lib/Drupal/Component/Plugin/Context/Context.php b/core/lib/Drupal/Component/Plugin/Context/Context.php index 85ab94e..c65d7e8 100644 --- a/core/lib/Drupal/Component/Plugin/Context/Context.php +++ b/core/lib/Drupal/Component/Plugin/Context/Context.php @@ -51,6 +51,14 @@ public function setContextValue($value) { * Implements \Drupal\Component\Plugin\Context\ContextInterface::getContextValue(). */ public function getContextValue() { + // Support optional contexts. + if (!isset($this->contextValue)) { + if ($this->getContextDefinition()->isRequired()) { + $type = $this->getContextDefinition()->getDataType(); + throw new ContextException(sprintf("The %s context is required and not present.", $type)); + } + return NULL; + } return $this->contextValue; } diff --git a/core/lib/Drupal/Component/Plugin/ContextAwarePluginBase.php b/core/lib/Drupal/Component/Plugin/ContextAwarePluginBase.php index f04f07d..b684630 100644 --- a/core/lib/Drupal/Component/Plugin/ContextAwarePluginBase.php +++ b/core/lib/Drupal/Component/Plugin/ContextAwarePluginBase.php @@ -7,10 +7,10 @@ namespace Drupal\Component\Plugin; -use Drupal\Component\Plugin\Exception\PluginException; +use Drupal\Component\Plugin\Context\ContextInterface; +use Drupal\Component\Plugin\Exception\ContextException; use Drupal\Component\Plugin\Context\Context; use Symfony\Component\Validator\ConstraintViolationList; -use Drupal\Component\Plugin\Discovery\DiscoveryInterface; /** * Base class for plugins that are context aware. @@ -20,7 +20,7 @@ /** * The data objects representing the context of this plugin. * - * @var array + * @var \Drupal\Component\Plugin\Context\ContextInterface[] */ protected $context; @@ -55,7 +55,7 @@ public function __construct(array $configuration, $plugin_id, $plugin_definition } /** - * Implements \Drupal\Component\Plugin\ContextAwarePluginInterface::getContextDefinitions(). + * {@inheritdoc} */ public function getContextDefinitions() { $definition = $this->getPluginDefinition(); @@ -63,50 +63,47 @@ public function getContextDefinitions() { } /** - * Implements \Drupal\Component\Plugin\ContextAwarePluginInterface::getContextDefinition(). + * {@inheritdoc} */ public function getContextDefinition($name) { $definition = $this->getPluginDefinition(); if (empty($definition['context'][$name])) { - throw new PluginException("The $name context is not a valid context."); + throw new ContextException(sprintf("The %s context is not a valid context.", $name)); } return $definition['context'][$name]; } /** - * Implements \Drupal\Component\Plugin\ContextAwarePluginInterface::getContexts(). + * {@inheritdoc} */ public function getContexts() { - $definitions = $this->getContextDefinitions(); - if ($definitions && empty($this->context)) { - throw new PluginException("There are no set contexts."); - } - $contexts = array(); - foreach (array_keys($definitions) as $name) { - if (empty($this->context[$name])) { - throw new PluginException("The $name context is not yet set."); - } - $contexts[$name] = $this->context[$name]; + // Make sure all context objects are initialized. + foreach ($this->getContextDefinitions() as $name => $definition) { + $this->getContext($name); } - return $contexts; + return $this->context; } /** - * Implements \Drupal\Component\Plugin\ContextAwarePluginInterface::getContext(). + * {@inheritdoc} */ public function getContext($name) { - // Check for a valid context definition. - $this->getContextDefinition($name); // Check for a valid context value. if (!isset($this->context[$name])) { - throw new PluginException("The $name context is not yet set."); + $this->context[$name] = new Context($this->getContextDefinition($name)); } - return $this->context[$name]; } /** - * Implements \Drupal\Component\Plugin\ContextAwarePluginInterface::getContextValues(). + * {@inheritdoc} + */ + public function setContext($name, ContextInterface $context) { + $this->context[$name] = $context; + } + + /** + * {@inheritdoc} */ public function getContextValues() { $values = array(); @@ -117,36 +114,30 @@ public function getContextValues() { } /** - * Implements \Drupal\Component\Plugin\ContextAwarePluginInterface::getContextValue(). + * {@inheritdoc} */ public function getContextValue($name) { return $this->getContext($name)->getContextValue(); } /** - * Implements \Drupal\Component\Plugin\ContextAwarePluginInterface::setContextValue(). + * {@inheritdoc} */ public function setContextValue($name, $value) { - $context_definition = $this->getContextDefinition($name); - $this->context[$name] = new Context($context_definition); - $this->context[$name]->setContextValue($value); + $this->getContext($name)->setContextValue($value); return $this; } /** - * Implements \Drupal\Component\Plugin\ContextAwarePluginInterface::valdidateContexts(). + * {@inheritdoc} */ public function validateContexts() { $violations = new ConstraintViolationList(); // @todo: Implement symfony validator API to let the validator traverse // and set property paths accordingly. - foreach ($this->getContextDefinitions() as $name => $definition) { - // Validate any set values. - if (isset($this->context[$name])) { - $violations->addAll($this->context[$name]->validate()); - } - // @todo: If no value is set, make sure any mapping is validated. + foreach ($this->getContexts() as $context) { + $violations->addAll($context->validate()); } return $violations; } diff --git a/core/lib/Drupal/Component/Plugin/ContextAwarePluginInterface.php b/core/lib/Drupal/Component/Plugin/ContextAwarePluginInterface.php index f29119d..9cb56a1 100644 --- a/core/lib/Drupal/Component/Plugin/ContextAwarePluginInterface.php +++ b/core/lib/Drupal/Component/Plugin/ContextAwarePluginInterface.php @@ -7,7 +7,7 @@ namespace Drupal\Component\Plugin; -use Drupal\Component\Plugin\Exception\PluginException; +use \Drupal\Component\Plugin\Context\ContextInterface; /** * Interface for defining context aware plugins. @@ -36,7 +36,7 @@ public function getContextDefinitions(); * @throws \Drupal\Component\Plugin\Exception\PluginException * If the requested context is not defined. * - * @return array + * @return \Drupal\Component\Plugin\Context\ContextDefinitionInterface. * The definition against which the context value must validate. */ public function getContextDefinition($name); @@ -90,6 +90,16 @@ public function getContextValues(); public function getContextValue($name); /** + * Set a context on this plugin. + * + * @param string $name + * The name of the context in the plugin configuration. + * @param \Drupal\Component\Plugin\Context\ContextInterface $context + * The context object to set. + */ + public function setContext($name, ContextInterface $context); + + /** * Sets the value for a defined context. * * @param string $name diff --git a/core/lib/Drupal/Core/Annotation/ContextDefinition.php b/core/lib/Drupal/Core/Annotation/ContextDefinition.php new file mode 100644 index 0000000..5f4d0f0 --- /dev/null +++ b/core/lib/Drupal/Core/Annotation/ContextDefinition.php @@ -0,0 +1,108 @@ + TRUE, + 'multiple' => FALSE, + 'label' => NULL, + 'description' => NULL, + ); + if (isset($values['class']) && !in_array('Drupal\Core\Plugin\Context\ContextDefinitionInterface', class_implements($values['class']))) { + throw new \Exception('ContextDefinition class must implement \Drupal\Core\Plugin\Context\ContextDefinitionInterface.'); + } + $class = isset($values['class']) ? $values['class'] : 'Drupal\Core\Plugin\Context\ContextDefinition'; + $this->definition = new $class($values['value'], $values['required'], $values['multiple'], $values['label'], $values['description']); + } + + /** + * {@inheritdoc} + * + * @return \Drupal\Core\Plugin\Context\ContextDefinition + */ + public function get() { + return $this->definition; + } + +} \ No newline at end of file diff --git a/core/lib/Drupal/Core/Condition/ConditionAccessResolverTrait.php b/core/lib/Drupal/Core/Condition/ConditionAccessResolverTrait.php index 35587eb..528e662 100644 --- a/core/lib/Drupal/Core/Condition/ConditionAccessResolverTrait.php +++ b/core/lib/Drupal/Core/Condition/ConditionAccessResolverTrait.php @@ -7,7 +7,7 @@ namespace Drupal\Core\Condition; -use Drupal\Component\Plugin\Exception\PluginException; +use Drupal\Component\Plugin\Exception\ContextException; /** * Resolves a set of conditions. @@ -30,7 +30,7 @@ protected function resolveConditions($conditions, $condition_logic) { try { $pass = $condition->execute(); } - catch (PluginException $e) { + catch (ContextException $e) { // If a condition is missing context, consider that a fail. $pass = FALSE; } diff --git a/core/lib/Drupal/Core/Plugin/Context/Context.php b/core/lib/Drupal/Core/Plugin/Context/Context.php index b1ab0f1..a87d49b 100644 --- a/core/lib/Drupal/Core/Plugin/Context/Context.php +++ b/core/lib/Drupal/Core/Plugin/Context/Context.php @@ -8,6 +8,8 @@ namespace Drupal\Core\Plugin\Context; use Drupal\Component\Plugin\Context\Context as ComponentContext; +use Drupal\Component\Plugin\Exception\ContextException; +use Drupal\Component\Utility\String; use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\TypedData\TypedDataInterface; use Drupal\Core\TypedData\TypedDataTrait; @@ -27,10 +29,21 @@ class Context extends ComponentContext implements ContextInterface { protected $contextData; /** + * The definition to which a context must conform. + * + * @var \Drupal\Core\Plugin\Context\ContextDefinitionInterface + */ + protected $contextDefinition; + + /** * {@inheritdoc} */ public function getContextValue() { if (!isset($this->contextData)) { + if ($this->getContextDefinition()->isRequired()) { + $type = $this->getContextDefinition()->getDataType(); + throw new ContextException(String::format("The @type context is required and not present.", array('@type' => $type))); + } return NULL; } // Special case entities. diff --git a/core/lib/Drupal/Core/Plugin/Context/ContextDefinition.php b/core/lib/Drupal/Core/Plugin/Context/ContextDefinition.php index c737157..53bd60f 100644 --- a/core/lib/Drupal/Core/Plugin/Context/ContextDefinition.php +++ b/core/lib/Drupal/Core/Plugin/Context/ContextDefinition.php @@ -83,9 +83,21 @@ public static function create($data_type = 'any') { * * @param string $data_type * The required data type. - */ - public function __construct($data_type = 'any') { + * @param bool $required + * Whether the context definition is required. + * @param bool $multiple + * Whether the context definition is multivalue. + * @param mixed string|null $label + * The label of this context definition for the UI. + * @param mixed string|null $description + * The description of this context definition for the UI. + */ + public function __construct($data_type = 'any', $required = TRUE, $multiple = FALSE, $label = NULL, $description = NULL) { $this->dataType = $data_type; + $this->isRequired = $required; + $this->isMultiple = $multiple; + $this->label = $label; + $this->description = $description; } /** diff --git a/core/lib/Drupal/Core/Plugin/Context/ContextHandler.php b/core/lib/Drupal/Core/Plugin/Context/ContextHandler.php index 672c613..debb21b 100644 --- a/core/lib/Drupal/Core/Plugin/Context/ContextHandler.php +++ b/core/lib/Drupal/Core/Plugin/Context/ContextHandler.php @@ -8,7 +8,7 @@ namespace Drupal\Core\Plugin\Context; use Drupal\Component\Plugin\ConfigurablePluginInterface; -use Drupal\Component\Plugin\Context\ContextInterface; +use Drupal\Component\Plugin\Context\ContextInterface as ComponentContextInterface; use Drupal\Component\Plugin\ContextAwarePluginInterface; use Drupal\Component\Plugin\Exception\ContextException; use Drupal\Component\Utility\String; @@ -97,7 +97,7 @@ public function checkRequirements(array $contexts, array $requirements) { * {@inheritdoc} */ public function getMatchingContexts(array $contexts, DataDefinitionInterface $definition) { - return array_filter($contexts, function (ContextInterface $context) use ($definition) { + return array_filter($contexts, function (ComponentContextInterface $context) use ($definition) { // @todo getContextDefinition() should return a DataDefinitionInterface. $context_definition = new DataDefinition($context->getContextDefinition()); @@ -132,6 +132,7 @@ public function applyContextMapping(ContextAwarePluginInterface $plugin, $contex $plugin_contexts = $plugin->getContextDefinitions(); // Loop through each context and set it on the plugin if it matches one of // the contexts expected by the plugin. + //drupal_set_message('
' . print_r($contexts, TRUE) . '
'); foreach ($contexts as $name => $context) { // If this context was given a specific name, use that. $assigned_name = isset($mappings[$name]) ? $mappings[$name] : $name; diff --git a/core/lib/Drupal/Core/Plugin/ContextAwarePluginBase.php b/core/lib/Drupal/Core/Plugin/ContextAwarePluginBase.php index 17460d2..1fec193 100644 --- a/core/lib/Drupal/Core/Plugin/ContextAwarePluginBase.php +++ b/core/lib/Drupal/Core/Plugin/ContextAwarePluginBase.php @@ -7,173 +7,46 @@ namespace Drupal\Core\Plugin; +use Drupal\Component\Plugin\ContextAwarePluginBase as ComponentContextAwarePluginBase; use Drupal\Component\Plugin\Exception\ContextException; +use Drupal\Core\DependencyInjection\DependencySerializationTrait; use Drupal\Core\Plugin\Context\Context; +use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\Core\TypedData\TypedDataTrait; -use Symfony\Component\DependencyInjection\ContainerInterface; +use Drupal\Component\Plugin\Context\ContextInterface as ComponentContextInterface; use Drupal\Core\Plugin\Context\ContextInterface; -use Symfony\Component\Validator\ConstraintViolationList; /** * Base class for plugins that are context aware. */ -abstract class ContextAwarePluginBase extends PluginBase implements ContextAwarePluginInterface { - +abstract class ContextAwarePluginBase extends ComponentContextAwarePluginBase { use TypedDataTrait; - - /** - * An array of service IDs keyed by property name used for serialization. - * - * @todo Remove when Drupal\Core\DependencyInjection\DependencySerialization - * is converted to a trait in https://drupal.org/node/2208115. - * - * @var array - */ - protected $_serviceIds = array(); - - /** - * The contexts of this plugin. - * - * @var \Drupal\Core\Plugin\Context\ContextInterface[] - */ - protected $contexts; - - /** - * The context definitions of this plugin. - * - * @var \Drupal\Core\Plugin\Context\ContextDefinitionInterface[] - */ - protected $contextDefinitions; - - /** - * {@inheritdoc} - */ - public function getContextDefinitions() { - if (!isset($this->contextDefinitions)) { - $this->contextDefinitions = static::contextDefinitions($this->getTypedDataManager()); - } - return $this->contextDefinitions; - } - - /** - * {@inheritdoc} - */ - public function getContextDefinition($name) { - $definitions = $this->getContextDefinitions(); - if (empty($definitions[$name])) { - throw new ContextException("The $name context is not a valid context."); - } - return $definitions[$name]; - } - - /** - * {@inheritdoc} - */ - public function getContexts() { - // Make sure all context objects are initialized. - foreach ($this->getContextDefinitions() as $name => $definition) { - $this->getContext($name); - } - return $this->contexts; - } + use StringTranslationTrait; + use DependencySerializationTrait; /** * {@inheritdoc} + * + * This code is identical to the Component in order to pick up a different + * Context class. */ public function getContext($name) { // Check for a valid context value. - if (!isset($this->contexts[$name])) { - $this->contexts[$name] = new Context($this->getContextDefinition($name)); - } - return $this->contexts[$name]; - } - - /** - * {@inheritdoc} - */ - public function setContext($name, ContextInterface $context) { - $this->contexts[$name] = $context; - } - - /** - * {@inheritdoc} - */ - public function getContextValues() { - $values = []; - foreach ($this->getContextDefinitions() as $name => $definition) { - $values[$name] = isset($this->contexts[$name]) ? $this->contexts[$name]->getContextValue() : NULL; + if (!isset($this->context[$name])) { + $this->context[$name] = new Context($this->getContextDefinition($name)); } - return $values; + return $this->context[$name]; } /** * {@inheritdoc} */ - public function getContextValue($name) { - return $this->getContext($name)->getContextValue(); - } - - /** - * {@inheritdoc} - */ - public function setContextValue($name, $value) { - $this->getContext($name)->setContextValue($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; - } - - /** - * {@inheritdoc} - * - * @todo Remove when Drupal\Core\DependencyInjection\DependencySerialization - * is converted to a trait in https://drupal.org/node/2208115. - */ - public function __sleep() { - $this->_serviceIds = array(); - $vars = get_object_vars($this); - foreach ($vars as $key => $value) { - if (is_object($value) && isset($value->_serviceId)) { - // If a class member was instantiated by the dependency injection - // container, only store its ID so it can be used to get a fresh object - // on unserialization. - $this->_serviceIds[$key] = $value->_serviceId; - unset($vars[$key]); - } - // Special case the container, which might not have a service ID. - elseif ($value instanceof ContainerInterface) { - $this->_serviceIds[$key] = 'service_container'; - unset($vars[$key]); - } - } - - return array_keys($vars); - } - - /** - * {@inheritdoc} - * - * @todo Remove when Drupal\Core\DependencyInjection\DependencySerialization - * is converted to a trait in https://drupal.org/node/2208115. - */ - public function __wakeup() { - $container = \Drupal::getContainer(); - foreach ($this->_serviceIds as $key => $service_id) { - $this->$key = $container->get($service_id); + 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"); } - unset($this->_serviceIds); + parent::setContext($name, $context); } } diff --git a/core/lib/Drupal/Core/Plugin/ContextAwarePluginInterface.php b/core/lib/Drupal/Core/Plugin/ContextAwarePluginInterface.php deleted file mode 100644 index e4dae54..0000000 --- a/core/lib/Drupal/Core/Plugin/ContextAwarePluginInterface.php +++ /dev/null @@ -1,37 +0,0 @@ - 'language', - 'label' => $this->t('Current language'), - )); + $context = new Context(new ContextDefinition('language', TRUE, FALSE, $this->t('Current language'))); $context->setContextValue($this->languageManager->getCurrentLanguage()); $this->addContext('language', $context); } diff --git a/core/modules/block/src/EventSubscriber/CurrentUserContext.php b/core/modules/block/src/EventSubscriber/CurrentUserContext.php index 14565aa..f467dad 100644 --- a/core/modules/block/src/EventSubscriber/CurrentUserContext.php +++ b/core/modules/block/src/EventSubscriber/CurrentUserContext.php @@ -9,6 +9,7 @@ use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Plugin\Context\Context; +use Drupal\Core\Plugin\Context\ContextDefinition; use Drupal\Core\Session\AccountInterface; use Drupal\Core\StringTranslation\StringTranslationTrait; @@ -52,10 +53,7 @@ public function __construct(AccountInterface $account, EntityManagerInterface $e protected function determineBlockContext() { $current_user = $this->userStorage->load($this->account->id()); - $context = new Context(array( - 'type' => 'entity:user', - 'label' => $this->t('Current user'), - )); + $context = new Context(new ContextDefinition('entity:user', TRUE, FALSE, $this->t('Current user'))); $context->setContextValue($current_user); $this->addContext('current_user', $context); } diff --git a/core/modules/block/src/EventSubscriber/NodeRouteContext.php b/core/modules/block/src/EventSubscriber/NodeRouteContext.php index 117bf21..8d22906 100644 --- a/core/modules/block/src/EventSubscriber/NodeRouteContext.php +++ b/core/modules/block/src/EventSubscriber/NodeRouteContext.php @@ -8,6 +8,7 @@ namespace Drupal\block\EventSubscriber; use Drupal\Core\Plugin\Context\Context; +use Drupal\Core\Plugin\Context\ContextDefinition; use Drupal\node\Entity\Node; use Symfony\Cmf\Component\Routing\RouteObjectInterface; use Symfony\Component\HttpFoundation\RequestStack; @@ -40,7 +41,7 @@ public function __construct(RequestStack $request_stack) { protected function determineBlockContext() { $request = $this->requestStack->getCurrentRequest(); if ($request->attributes->has(RouteObjectInterface::ROUTE_OBJECT) && ($route_contexts = $request->attributes->get(RouteObjectInterface::ROUTE_OBJECT)->getOption('parameters')) && isset($route_contexts['node'])) { - $context = new Context($route_contexts['node']); + $context = new Context(new ContextDefinition($route_contexts['node']['type'])); if ($request->attributes->has('node')) { $context->setContextValue($request->attributes->get('node')); } @@ -48,7 +49,7 @@ protected function determineBlockContext() { } elseif ($request->attributes->get(RouteObjectInterface::ROUTE_NAME) == 'node.add') { $node_type = $request->attributes->get('node_type'); - $context = new Context(array('type' => 'entity:node')); + $context = new Context(new ContextDefinition('entity:node')); $context->setContextValue(Node::create(array('type' => $node_type->id()))); $this->addContext('node', $context); } diff --git a/core/modules/language/src/Plugin/Condition/Language.php b/core/modules/language/src/Plugin/Condition/Language.php index 647c653..46a99cb 100644 --- a/core/modules/language/src/Plugin/Condition/Language.php +++ b/core/modules/language/src/Plugin/Condition/Language.php @@ -9,32 +9,24 @@ use Drupal\Core\Condition\ConditionPluginBase; use Drupal\Core\Language\LanguageInterface; -use Drupal\Core\Plugin\Context\ContextDefinition; -use Drupal\Core\TypedData\TypedDataManager; /** * Provides a 'Language' condition. * * @Condition( * id = "language", - * label = @Translation("Language") + * label = @Translation("Language"), + * context = { + * "language" = @ContextDefinition("language", label = @Translation("Language")) + * } * ) + * */ class Language extends ConditionPluginBase { /** * {@inheritdoc} */ - public static function contextDefinitions() { - $definitions['language'] = ContextDefinition::create('language') - ->setLabel(t('Language')); - - return $definitions; - } - - /** - * {@inheritdoc} - */ public function buildConfigurationForm(array $form, array &$form_state) { if (\Drupal::languageManager()->isMultilingual()) { // Fetch languages. diff --git a/core/modules/node/src/Plugin/Condition/NodeType.php b/core/modules/node/src/Plugin/Condition/NodeType.php index 8157fe0..7973e69 100644 --- a/core/modules/node/src/Plugin/Condition/NodeType.php +++ b/core/modules/node/src/Plugin/Condition/NodeType.php @@ -8,31 +8,24 @@ namespace Drupal\node\Plugin\Condition; use Drupal\Core\Condition\ConditionPluginBase; -use Drupal\Core\Plugin\Context\ContextDefinition; /** * Provides a 'Node Type' condition. * * @Condition( * id = "node_type", - * label = @Translation("Node Bundle") + * label = @Translation("Node Bundle"), + * context = { + * "node" = @ContextDefinition("entity:node", label = @Translation("Node")) + * } * ) + * */ class NodeType extends ConditionPluginBase { /** * {@inheritdoc} */ - public static function contextDefinitions() { - $definitions['node'] = ContextDefinition::create('entity:node') - ->setLabel(t('Node')); - - return $definitions; - } - - /** - * {@inheritdoc} - */ public function buildConfigurationForm(array $form, array &$form_state) { $options = array(); foreach (node_type_get_types() as $type) { diff --git a/core/modules/system/src/Tests/Plugin/DerivativeTest.php b/core/modules/system/src/Tests/Plugin/DerivativeTest.php index 8feeff6..0d634f5 100644 --- a/core/modules/system/src/Tests/Plugin/DerivativeTest.php +++ b/core/modules/system/src/Tests/Plugin/DerivativeTest.php @@ -25,11 +25,11 @@ public static function getInfo() { */ function testDerivativeDecorator() { // Ensure that getDefinitions() returns the expected definitions. - $this->assertIdentical($this->mockBlockManager->getDefinitions(), $this->mockBlockExpectedDefinitions); + $this->assertEqual($this->mockBlockManager->getDefinitions(), $this->mockBlockExpectedDefinitions); // Ensure that getDefinition() returns the expected definition. foreach ($this->mockBlockExpectedDefinitions as $id => $definition) { - $this->assertIdentical($this->mockBlockManager->getDefinition($id), $definition); + $this->assertEqual($this->mockBlockManager->getDefinition($id), $definition); } // Ensure that NULL is returned as the definition of a non-existing base diff --git a/core/modules/system/src/Tests/Plugin/PluginTestBase.php b/core/modules/system/src/Tests/Plugin/PluginTestBase.php index fc406bb..f9cac10 100644 --- a/core/modules/system/src/Tests/Plugin/PluginTestBase.php +++ b/core/modules/system/src/Tests/Plugin/PluginTestBase.php @@ -7,6 +7,7 @@ namespace Drupal\system\Tests\Plugin; +use Drupal\Core\Plugin\Context\ContextDefinition; use Drupal\simpletest\UnitTestBase; use Drupal\plugin_test\Plugin\TestPluginManager; use Drupal\plugin_test\Plugin\MockBlockManager; @@ -81,6 +82,9 @@ public function setUp() { 'user_name' => array( 'label' => 'User name', 'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockUserNameBlock', + 'context' => array( + 'user' => new ContextDefinition('entity:user', TRUE, FALSE, t('User')), + ), ), 'string_context' => array( 'label' => 'String typed data', @@ -89,6 +93,10 @@ public function setUp() { 'complex_context' => array( 'label' => 'Complex context', 'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockComplexContextBlock', + 'context' => array( + 'user' => new ContextDefinition('entity:user', TRUE, FALSE, t('User')), + 'node' => new ContextDefinition('entity:node', TRUE, FALSE, t('Node')), + ), ), ); $this->defaultsTestPluginExpectedDefinitions = array( diff --git a/core/modules/system/tests/modules/plugin_test/src/Plugin/MockBlockManager.php b/core/modules/system/tests/modules/plugin_test/src/Plugin/MockBlockManager.php index 7f5c252..2b39121c 100644 --- a/core/modules/system/tests/modules/plugin_test/src/Plugin/MockBlockManager.php +++ b/core/modules/system/tests/modules/plugin_test/src/Plugin/MockBlockManager.php @@ -11,6 +11,7 @@ use Drupal\Component\Plugin\Discovery\StaticDiscovery; use Drupal\Component\Plugin\Discovery\DerivativeDiscoveryDecorator; use Drupal\Component\Plugin\Factory\ReflectionFactory; +use Drupal\Core\Plugin\Context\ContextDefinition; /** * Defines a plugin manager used by Plugin API derivative unit tests. @@ -76,6 +77,9 @@ public function __construct() { $this->discovery->setDefinition('user_name', array( 'label' => t('User name'), 'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockUserNameBlock', + 'context' => array( + 'user' => new ContextDefinition('entity:user', TRUE, FALSE, t('User')), + ), )); // A block plugin that requires a typed data string context to function. @@ -88,6 +92,10 @@ public function __construct() { $this->discovery->setDefinition('complex_context', array( 'label' => t('Complex context'), 'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockComplexContextBlock', + 'context' => array( + 'user' => new ContextDefinition('entity:user', TRUE, FALSE, t('User')), + 'node' => new ContextDefinition('entity:node', TRUE, FALSE, t('Node')), + ), )); // In addition to finding all of the plugins available for a type, a plugin diff --git a/core/modules/system/tests/modules/plugin_test/src/Plugin/plugin_test/mock_block/MockComplexContextBlock.php b/core/modules/system/tests/modules/plugin_test/src/Plugin/plugin_test/mock_block/MockComplexContextBlock.php index eb6b8bc..8041c3a 100644 --- a/core/modules/system/tests/modules/plugin_test/src/Plugin/plugin_test/mock_block/MockComplexContextBlock.php +++ b/core/modules/system/tests/modules/plugin_test/src/Plugin/plugin_test/mock_block/MockComplexContextBlock.php @@ -17,17 +17,6 @@ */ class MockComplexContextBlock extends ContextAwarePluginBase { - /** - * {@inheritdoc} - */ - public static function contextDefinitions() { - $definitions['user'] = ContextDefinition::create('entity:user') - ->setLabel(t('User')); - $definitions['node'] = ContextDefinition::create('entity:node') - ->setLabel(t('Node')); - return $definitions; - } - public function getTitle() { $user = $this->getContextValue('user'); $node = $this->getContextValue('node'); diff --git a/core/modules/system/tests/modules/plugin_test/src/Plugin/plugin_test/mock_block/MockUserNameBlock.php b/core/modules/system/tests/modules/plugin_test/src/Plugin/plugin_test/mock_block/MockUserNameBlock.php index e214588..07c7a79 100644 --- a/core/modules/system/tests/modules/plugin_test/src/Plugin/plugin_test/mock_block/MockUserNameBlock.php +++ b/core/modules/system/tests/modules/plugin_test/src/Plugin/plugin_test/mock_block/MockUserNameBlock.php @@ -17,14 +17,6 @@ */ class MockUserNameBlock extends ContextAwarePluginBase { - /** - * {@inheritdoc} - */ - public static function contextDefinitions() { - $definitions['user'] = ContextDefinition::create('entity:user')->setLabel(t('User')); - return $definitions; - } - public function getTitle() { $user = $this->getContextValue('user'); return $user->label(); diff --git a/core/modules/system/tests/modules/plugin_test/src/Plugin/plugin_test/mock_block/TypedDataStringBlock.php b/core/modules/system/tests/modules/plugin_test/src/Plugin/plugin_test/mock_block/TypedDataStringBlock.php index f911bfd..14e0ab3 100644 --- a/core/modules/system/tests/modules/plugin_test/src/Plugin/plugin_test/mock_block/TypedDataStringBlock.php +++ b/core/modules/system/tests/modules/plugin_test/src/Plugin/plugin_test/mock_block/TypedDataStringBlock.php @@ -17,13 +17,6 @@ */ class TypedDataStringBlock extends ContextAwarePluginBase { - /** - * {@inheritdoc} - */ - public static function contextDefinitions() { - return []; - } - public function getTitle() { return $this->getContextValue('string'); } diff --git a/core/modules/user/src/Plugin/Condition/UserRole.php b/core/modules/user/src/Plugin/Condition/UserRole.php index 89c640c..872b18a 100644 --- a/core/modules/user/src/Plugin/Condition/UserRole.php +++ b/core/modules/user/src/Plugin/Condition/UserRole.php @@ -15,24 +15,18 @@ * * @Condition( * id = "user_role", - * label = @Translation("User Role") + * label = @Translation("User Role"), + * context = { + * "user" = @ContextDefinition("entity:user", label = @Translation("User")) + * } * ) + * */ class UserRole extends ConditionPluginBase { /** * {@inheritdoc} */ - public static function contextDefinitions() { - $definitions['user'] = ContextDefinition::create('entity:user') - ->setLabel(t('User')); - - return $definitions; - } - - /** - * {@inheritdoc} - */ public function buildConfigurationForm(array $form, array &$form_state) { $form['roles'] = array( '#type' => 'checkboxes', diff --git a/core/tests/Drupal/Tests/Core/Condition/ConditionAccessResolverTraitTest.php b/core/tests/Drupal/Tests/Core/Condition/ConditionAccessResolverTraitTest.php index dc676c5..8081ea8 100644 --- a/core/tests/Drupal/Tests/Core/Condition/ConditionAccessResolverTraitTest.php +++ b/core/tests/Drupal/Tests/Core/Condition/ConditionAccessResolverTraitTest.php @@ -7,8 +7,8 @@ namespace Drupal\Tests\Core\Condition; +use Drupal\Component\Plugin\Exception\ContextException; use Drupal\Core\Condition\ConditionAccessResolverTrait; -use Drupal\Component\Plugin\Exception\PluginException; use Drupal\Tests\UnitTestCase; /** @@ -58,7 +58,7 @@ public function providerTestResolveConditions() { $condition_exception = $this->getMock('Drupal\Core\Condition\ConditionInterface'); $condition_exception->expects($this->any()) ->method('execute') - ->will($this->throwException(new PluginException())); + ->will($this->throwException(new ContextException())); $conditions = array(); $data[] = array($conditions, 'and', TRUE);