class ContextDefinition

Same name in this branch
  1. 9 core/lib/Drupal/Core/Plugin/Context/ContextDefinition.php \Drupal\Core\Plugin\Context\ContextDefinition
Same name in other branches
  1. 8.9.x core/lib/Drupal/Core/Annotation/ContextDefinition.php \Drupal\Core\Annotation\ContextDefinition
  2. 8.9.x core/lib/Drupal/Core/Plugin/Context/ContextDefinition.php \Drupal\Core\Plugin\Context\ContextDefinition
  3. 10 core/lib/Drupal/Core/Annotation/ContextDefinition.php \Drupal\Core\Annotation\ContextDefinition
  4. 10 core/lib/Drupal/Core/Plugin/Context/ContextDefinition.php \Drupal\Core\Plugin\Context\ContextDefinition
  5. 11.x core/lib/Drupal/Core/Annotation/ContextDefinition.php \Drupal\Core\Annotation\ContextDefinition
  6. 11.x core/lib/Drupal/Core/Plugin/Context/ContextDefinition.php \Drupal\Core\Plugin\Context\ContextDefinition

Defines a context definition annotation object.

Some plugins require various data contexts in order to function. This class supports that need by allowing the contexts to be easily defined within an annotation and return a ContextDefinitionInterface implementing class.

Hierarchy

Expanded class hierarchy of ContextDefinition

Related topics

File

core/lib/Drupal/Core/Annotation/ContextDefinition.php, line 71

Namespace

Drupal\Core\Annotation
View source
class ContextDefinition extends Plugin {
    
    /**
     * The ContextDefinitionInterface object.
     *
     * @var \Drupal\Core\Plugin\Context\ContextDefinitionInterface
     */
    protected $definition;
    
    /**
     * Constructs a new context definition object.
     *
     * @param array $values
     *   An associative array with the following keys:
     *   - value: The required data type.
     *   - label: (optional) The UI label of this context definition.
     *   - required: (optional) Whether the context definition is required.
     *   - multiple: (optional) Whether the context definition is multivalue.
     *   - description: (optional) The UI description of this context definition.
     *   - default_value: (optional) The default value in case the underlying
     *     value is not set.
     *   - class: (optional) A custom ContextDefinitionInterface class.
     *
     * @throws \Exception
     *   Thrown when the class key is specified with a non
     *   ContextDefinitionInterface implementing class.
     */
    public function __construct(array $values) {
        $values += [
            'required' => TRUE,
            'multiple' => FALSE,
            'default_value' => NULL,
        ];
        // Annotation classes extract data from passed annotation classes directly
        // used in the classes they pass to.
        foreach ([
            'label',
            'description',
        ] as $key) {
            // @todo Remove this workaround in https://www.drupal.org/node/2362727.
            if (isset($values[$key]) && $values[$key] instanceof Translation) {
                $values[$key] = (string) $values[$key]->get();
            }
            else {
                $values[$key] = 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 = $this->getDefinitionClass($values);
        $this->definition = new $class($values['value'], $values['label'], $values['required'], $values['multiple'], $values['description'], $values['default_value']);
        if (isset($values['constraints'])) {
            foreach ($values['constraints'] as $constraint_name => $options) {
                $this->definition
                    ->addConstraint($constraint_name, $options);
            }
        }
    }
    
    /**
     * Determines the context definition class to use.
     *
     * If the annotation specifies a specific context definition class, we use
     * that. Otherwise, we use \Drupal\Core\Plugin\Context\EntityContextDefinition
     * if the data type starts with 'entity:', since it contains specialized logic
     * specific to entities. Otherwise, we fall back to the generic
     * \Drupal\Core\Plugin\Context\ContextDefinition class.
     *
     * @param array $values
     *   The annotation values.
     *
     * @return string
     *   The fully-qualified name of the context definition class.
     */
    protected function getDefinitionClass(array $values) {
        if (isset($values['class'])) {
            return $values['class'];
        }
        if (strpos($values['value'], 'entity:') === 0) {
            return 'Drupal\\Core\\Plugin\\Context\\EntityContextDefinition';
        }
        return 'Drupal\\Core\\Plugin\\Context\\ContextDefinition';
    }
    
    /**
     * Returns the value of an annotation.
     *
     * @return \Drupal\Core\Plugin\Context\ContextDefinitionInterface
     */
    public function get() {
        return $this->definition;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
ContextDefinition::$definition protected property The ContextDefinitionInterface object. Overrides Plugin::$definition
ContextDefinition::get public function Returns the value of an annotation. Overrides Plugin::get
ContextDefinition::getDefinitionClass protected function Determines the context definition class to use.
ContextDefinition::__construct public function Constructs a new context definition object. Overrides Plugin::__construct
Plugin::getClass public function Gets the class of the annotated class. Overrides AnnotationInterface::getClass 1
Plugin::getId public function Gets the unique ID for this annotated class. Overrides AnnotationInterface::getId
Plugin::getProvider public function Gets the name of the provider of the annotated class. Overrides AnnotationInterface::getProvider 1
Plugin::parse protected function Parses an annotation into its definition.
Plugin::setClass public function Sets the class of the annotated class. Overrides AnnotationInterface::setClass 1
Plugin::setProvider public function Sets the name of the provider of the annotated class. Overrides AnnotationInterface::setProvider

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.