Skip to content
Snippets Groups Projects
Commit d0a258ed authored by Aaron Bauman's avatar Aaron Bauman Committed by Tim Rohaly
Browse files

Issue #3344074 by AaronBauman, TR: Remove Context classes

parent ada057e0
No related branches found
No related tags found
2 merge requests!13Resolve #3456818 "Remove outdated versioncompare",!7Issue #3344074: Remove Context classes
<?php
namespace Drupal\typed_data\Context;
use Drupal\Component\Annotation\Doctrine\SimpleAnnotationReader;
use Drupal\Core\Plugin\Discovery\AnnotatedClassDiscovery as CoreAnnotatedClassDiscovery;
/**
* Extends the annotation class discovery for usage with Typed Data context.
*
* We modify the annotations classes for ContextDefinition and for Condition.
* This class makes sure that our plugin managers apply these.
*/
class AnnotatedClassDiscovery extends CoreAnnotatedClassDiscovery {
/**
* {@inheritdoc}
*/
protected function getAnnotationReader() {
if (!isset($this->annotationReader)) {
// Do not call out the parent, but re-configure the simple annotation
// reader on our own, so we can control the order of namespaces.
$this->annotationReader = new SimpleAnnotationReader();
// Make sure to add our namespace first, so our ContextDefinition and
// Condition annotations gets picked.
$this->annotationReader->addNamespace('Drupal\typed_data\Context\Annotation');
// Add the namespaces from the main plugin annotation, like @EntityType.
$namespace = mb_substr($this->pluginDefinitionAnnotationName, 0, mb_strrpos($this->pluginDefinitionAnnotationName, '\\'));
$this->annotationReader->addNamespace($namespace);
// Add general core annotations like @Translation.
$this->annotationReader->addNamespace('Drupal\Core\Annotation');
}
return $this->annotationReader;
}
}
<?php
namespace Drupal\typed_data\Context\Annotation;
use Drupal\Core\Annotation\ContextDefinition as CoreContextDefinition;
use Drupal\Core\Annotation\Translation;
use Drupal\typed_data\Context\ContextDefinition as TypedDataContextDefinition;
/**
* Extends the core context definition annotation object for Typed Data.
*
* Ensures context definitions use
* \Drupal\typed_data\Context\ContextDefinitionInterface.
*
* @Annotation
*
* @ingroup plugin_context
*/
class ContextDefinition extends CoreContextDefinition {
/**
* The ContextDefinitionInterface object.
*
* @var \Drupal\typed_data\Context\ContextDefinitionInterface
*/
protected $definition;
/**
* {@inheritdoc}
*/
public function __construct(array $values) {
// Filter out any @Translation annotation objects.
foreach ($values as $key => $value) {
if ($value instanceof Translation) {
$values[$key] = $value->get();
}
}
$this->definition = TypedDataContextDefinition::createFromArray($values);
}
/**
* Returns the value of an annotation.
*
* @return \Drupal\typed_data\Context\ContextDefinitionInterface
* Return the Typed Data version of the ContextDefinitionInterface.
*/
public function get() {
return $this->definition;
}
}
<?php
namespace Drupal\typed_data\Context;
use Drupal\Component\Plugin\Exception\ContextException;
use Drupal\Core\Plugin\Context\ContextDefinition as ContextDefinitionCore;
/**
* Extends the core context definition class with useful methods.
*/
class ContextDefinition extends ContextDefinitionCore implements ContextDefinitionInterface {
/**
* The mapping of config export keys to internal properties.
*
* @var array
*/
protected static $nameMap = [
'type' => 'dataType',
'label' => 'label',
'description' => 'description',
'multiple' => 'isMultiple',
'required' => 'isRequired',
'default_value' => 'defaultValue',
'constraints' => 'constraints',
'allow_null' => 'allowNull',
'assignment_restriction' => 'assignmentRestriction',
];
/**
* Whether the context value is allowed to be NULL or not.
*
* @var bool
*/
protected $allowNull = FALSE;
/**
* The assignment restriction of this context.
*
* @var string|null
*
* @see \Drupal\typed_data\Context\ContextDefinitionInterface::getAssignmentRestriction()
*/
protected $assignmentRestriction = NULL;
/**
* {@inheritdoc}
*/
public function toArray() {
$values = [];
$defaults = get_class_vars(__CLASS__);
foreach (static::$nameMap as $key => $property_name) {
// Only export values for non-default properties.
if ($this->$property_name !== $defaults[$property_name]) {
$values[$key] = $this->$property_name;
}
}
return $values;
}
/**
* Creates a definition object from an exported array of values.
*
* @param array $values
* The array of values, as returned by toArray().
*
* @return static
* The created definition.
*
* @throws \Drupal\Component\Plugin\Exception\ContextException
* If the required classes are not implemented.
*/
public static function createFromArray(array $values) {
if (isset($values['class']) && !in_array(ContextDefinitionInterface::class, class_implements($values['class']))) {
throw new ContextException('ContextDefinition class must implement ' . ContextDefinitionInterface::class . '.');
}
// Default to Typed Data context definition class.
$values['class'] = isset($values['class']) ? $values['class'] : ContextDefinition::class;
if (!isset($values['value'])) {
$values['value'] = 'any';
}
$definition = $values['class']::create($values['value']);
foreach (array_intersect_key(static::$nameMap, $values) as $key => $name) {
$definition->$name = $values[$key];
}
return $definition;
}
/**
* {@inheritdoc}
*/
public function isAllowedNull() {
return $this->allowNull;
}
/**
* {@inheritdoc}
*/
public function setAllowNull($null_allowed) {
$this->allowNull = $null_allowed;
return $this;
}
/**
* {@inheritdoc}
*/
public function getAssignmentRestriction() {
return $this->assignmentRestriction;
}
/**
* {@inheritdoc}
*/
public function setAssignmentRestriction($restriction) {
$this->assignmentRestriction = $restriction;
return $this;
}
}
<?php
namespace Drupal\typed_data\Context;
use Drupal\Core\Plugin\Context\ContextDefinitionInterface as ContextDefinitionInterfaceCore;
/**
* Context definition information required by Typed Data.
*
* The core interface is extended to add properties that are necessary for
* Typed Data.
*/
interface ContextDefinitionInterface extends ContextDefinitionInterfaceCore {
/**
* Constants for the context assignment restriction mode.
*
* @see ::getAssignmentRestriction()
*/
const ASSIGNMENT_RESTRICTION_INPUT = 'input';
const ASSIGNMENT_RESTRICTION_SELECTOR = 'selector';
/**
* Determines if the context value is allowed to be NULL.
*
* @return bool
* TRUE if NULL values are allowed, FALSE otherwise.
*/
public function isAllowedNull();
/**
* Sets the "allow NULL value" behavior.
*
* @param bool $null_allowed
* TRUE if NULL values should be allowed, FALSE otherwise.
*
* @return $this
*/
public function setAllowNull($null_allowed);
/**
* Determines if this context has an assignment restriction.
*
* @return string|null
* Either ASSIGNMENT_RESTRICTION_INPUT for contexts that are only allowed to
* be provided as input values, ASSIGNMENT_RESTRICTION_SELECTOR for contexts
* that must be provided as data selectors or NULL if there is no
* restriction for this context.
*/
public function getAssignmentRestriction();
/**
* Sets the assignment restriction mode for this context.
*
* @param string|null $restriction
* Either ASSIGNMENT_RESTRICTION_INPUT for contexts that are only allowed to
* be provided as input values, ASSIGNMENT_RESTRICTION_SELECTOR for contexts
* that must be provided as data selectors or NULL if there is no
* restriction for this context.
*
* @return $this
*/
public function setAssignmentRestriction($restriction);
/**
* Exports the definition as an array.
*
* @return array
* An array with values for all definition keys.
*/
public function toArray();
}
......@@ -7,7 +7,7 @@ use Drupal\Core\TypedData\DataDefinitionInterface;
use Drupal\Core\TypedData\ListInterface;
use Drupal\Core\TypedData\OptionsProviderInterface;
use Drupal\Core\TypedData\TypedDataInterface;
use Drupal\typed_data\Context\ContextDefinition;
use Drupal\Core\Plugin\Context\ContextDefinition;
use Drupal\typed_data\Form\SubformState;
use Drupal\typed_data\Widget\FormWidgetBase;
use Symfony\Component\Validator\ConstraintViolationListInterface;
......
......@@ -86,7 +86,7 @@ interface FormWidgetInterface extends ConfigurableInterface, PluginInspectionInt
* @param \Drupal\Core\TypedData\DataDefinitionInterface $definition
* The definition of the edited data.
*
* @return \Drupal\typed_data\Context\ContextDefinitionInterface[]
* @return \Drupal\Core\Plugin\Context\ContextDefinitionInterface[]
* An array of context definitions describing the configuration values,
* keyed by configuration setting name. The keys must match the actual keys
* of the supported configuration.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment