Commit 0ede95dd authored by alexpott's avatar alexpott

Issue #2293205 by Berdir | tim.plunkett: Consider removing usage of...

Issue #2293205 by Berdir | tim.plunkett: Consider removing usage of DataDefinition in ContextHandler.
parent 5c3b66c8
......@@ -11,32 +11,12 @@
use Drupal\Component\Plugin\ContextAwarePluginInterface;
use Drupal\Component\Plugin\Exception\ContextException;
use Drupal\Component\Utility\String;
use Drupal\Core\TypedData\DataDefinitionInterface;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\Core\TypedData\TypedDataManager;
/**
* Provides methods to handle sets of contexts.
*/
class ContextHandler implements ContextHandlerInterface {
/**
* The typed data manager.
*
* @var \Drupal\Core\TypedData\TypedDataManager
*/
protected $typedDataManager;
/**
* Constructs a new ContextHandler.
*
* @param \Drupal\Core\TypedData\TypedDataManager $typed_data
* The typed data manager.
*/
public function __construct(TypedDataManager $typed_data) {
$this->typedDataManager = $typed_data;
}
/**
* {@inheritdoc}
*/
......@@ -47,37 +27,8 @@ public function filterPluginDefinitionsByContexts(array $contexts, array $defini
return TRUE;
}
// 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->getDataType());
$definition['type'] = $plugin_context->getDataType();
// If the plugin specifies additional constraints, add them to the
// constraints defined by the plugin type.
if ($plugin_constraints = $plugin_context->getConstraints()) {
// Ensure the array exists before adding in constraints.
if (!isset($definition['constraints'])) {
$definition['constraints'] = array();
}
$definition['constraints'] += $plugin_constraints;
}
// Assume the requirement is required if unspecified.
if (!isset($definition['required'])) {
$definition['required'] = TRUE;
}
// @todo Use context definition objects after
// https://drupal.org/node/2281635.
$requirements[$context_id] = new DataDefinition($definition);
}
// Check the set of contexts against the requirements.
return $this->checkRequirements($contexts, $requirements);
return $this->checkRequirements($contexts, $plugin_definition['context']);
});
}
......@@ -96,17 +47,17 @@ public function checkRequirements(array $contexts, array $requirements) {
/**
* {@inheritdoc}
*/
public function getMatchingContexts(array $contexts, DataDefinitionInterface $definition) {
public function getMatchingContexts(array $contexts, ContextDefinitionInterface $definition) {
return array_filter($contexts, function (ContextInterface $context) use ($definition) {
$context_definition = $context->getContextDefinition()->getDataDefinition();
$context_definition = $context->getContextDefinition();
// If the data types do not match, this context is invalid.
if ($definition->getDataType() != $context_definition->getDataType()) {
// If the data types do not match, this context is invalid unless the
// expected data type is any, which means all data types are supported.
if ($definition->getDataType() != 'any' && $definition->getDataType() != $context_definition->getDataType()) {
return FALSE;
}
// If any constraint does not match, this context is invalid.
// @todo This is too restrictive, consider only relying on data types.
foreach ($definition->getConstraints() as $constraint_name => $constraint) {
if ($context_definition->getConstraint($constraint_name) != $constraint) {
return FALSE;
......
......@@ -8,7 +8,6 @@
namespace Drupal\Core\Plugin\Context;
use Drupal\Component\Plugin\ContextAwarePluginInterface;
use Drupal\Core\TypedData\DataDefinitionInterface;
/**
* Provides an interface for handling sets of contexts.
......@@ -53,13 +52,13 @@ public function checkRequirements(array $contexts, array $requirements);
*
* @param \Drupal\Component\Plugin\Context\ContextInterface[] $contexts
* An array of contexts.
* @param \Drupal\Core\TypedData\DataDefinitionInterface $definition
* @param \Drupal\Core\Plugin\Context\ContextDefinitionInterface $definition
* The definition to satisfy.
*
* @return \Drupal\Component\Plugin\Context\ContextInterface[]
* An array of matching contexts.
*/
public function getMatchingContexts(array $contexts, DataDefinitionInterface $definition);
public function getMatchingContexts(array $contexts, ContextDefinitionInterface $definition);
/**
* Prepares a plugin for evaluation.
......
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