Commit d4c697e2 authored by webchick's avatar webchick

Issue #2292889 by Berdir, tim.plunkett: Fixed ContextHandler does not expect...

Issue #2292889 by Berdir, tim.plunkett: Fixed ContextHandler does not expect ContextDefinitionInterface.
parent 12b4bfba
......@@ -42,7 +42,7 @@ public function setContextDefinition(ContextDefinitionInterface $context_definit
/**
* Gets the provided definition that the context must conform to.
*
* @return array
* @return \Drupal\Component\Plugin\Context\ContextDefinitionInterface
* The defining characteristic representation of the context.
*/
public function getContextDefinition();
......
......@@ -22,7 +22,7 @@ interface ContextAwarePluginInterface extends PluginInspectionInterface {
/**
* Gets the context definitions of the plugin.
*
* @return array
* @return \Drupal\Component\Plugin\Context\ContextDefinitionInterface[]
* The array of context definitions, keyed by context name.
*/
public function getContextDefinitions();
......
<?php
/**
* @file
* Contains \Drupal\Core\Annotation\ContextDefinition.
......@@ -6,6 +7,8 @@
namespace Drupal\Core\Annotation;
use Drupal\Component\Annotation\Plugin;
/**
* @defgroup plugin_context context definition plugin metadata
*
......@@ -46,7 +49,6 @@
* @endcode
* @}
*/
use Drupal\Component\Annotation\Plugin;
/**
* Defines a context definition annotation object.
......
......@@ -7,6 +7,7 @@
namespace Drupal\Core\Plugin\Context;
use Drupal\Component\Utility\String;
use Drupal\Core\TypedData\TypedDataTrait;
/**
......@@ -217,10 +218,15 @@ public function getDataDefinition() {
else {
$definition = $this->getTypedDataManager()->createDataDefinition($this->getDataType());
}
if (!$definition) {
throw new \Exception(String::format('The data type "@type" is invalid', array('@type' => $this->getDataType())));
}
$definition->setLabel($this->getLabel())
->setDescription($this->getDescription())
->setRequired($this->isRequired())
->setConstraints($this->getConstraints());
->setRequired($this->isRequired());
$constraints = $definition->getConstraints() + $this->getConstraints();
$definition->setConstraints($constraints);
return $definition;
}
......
......@@ -8,7 +8,6 @@
namespace Drupal\Core\Plugin\Context;
use Drupal\Component\Plugin\ConfigurablePluginInterface;
use Drupal\Component\Plugin\Context\ContextInterface as ComponentContextInterface;
use Drupal\Component\Plugin\ContextAwarePluginInterface;
use Drupal\Component\Plugin\Exception\ContextException;
use Drupal\Component\Utility\String;
......@@ -51,19 +50,20 @@ public function filterPluginDefinitionsByContexts(array $contexts, array $defini
// Build an array of requirements out of the contexts specified by the
// plugin definition.
$requirements = array();
/** @var $plugin_context \Drupal\Core\Plugin\Context\ContextDefinitionInterface */
foreach ($plugin_definition['context'] as $context_id => $plugin_context) {
$definition = $this->typedDataManager->getDefinition($plugin_context['type']);
$definition['type'] = $plugin_context['type'];
$definition = $this->typedDataManager->getDefinition($plugin_context->getDataType());
$definition['type'] = $plugin_context->getDataType();
// If the plugin specifies additional constraints, add them to the
// constraints defined by the plugin type.
if (isset($plugin_context['constraints'])) {
if ($plugin_constraints = $plugin_context->getConstraints()) {
// Ensure the array exists before adding in constraints.
if (!isset($definition['constraints'])) {
$definition['constraints'] = array();
}
$definition['constraints'] += $plugin_context['constraints'];
$definition['constraints'] += $plugin_constraints;
}
// Assume the requirement is required if unspecified.
......@@ -97,9 +97,8 @@ public function checkRequirements(array $contexts, array $requirements) {
* {@inheritdoc}
*/
public function getMatchingContexts(array $contexts, DataDefinitionInterface $definition) {
return array_filter($contexts, function (ComponentContextInterface $context) use ($definition) {
// @todo getContextDefinition() should return a DataDefinitionInterface.
$context_definition = new DataDefinition($context->getContextDefinition());
return array_filter($contexts, function (ContextInterface $context) use ($definition) {
$context_definition = $context->getContextDefinition()->getDataDefinition();
// If the data types do not match, this context is invalid.
if ($definition->getDataType() != $context_definition->getDataType()) {
......
......@@ -16,9 +16,7 @@
* id = "test_context_aware",
* admin_label = @Translation("Test context-aware block"),
* context = {
* "user" = {
* "type" = "entity:user"
* }
* "user" = @ContextDefinition("entity:user")
* }
* )
*/
......
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