public function ContextDefinition::__construct

public ContextDefinition::__construct(array $values)

Constructs a new context definition object.

Parameters

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.

Overrides Plugin::__construct

File

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

Class

ContextDefinition
Defines a context definition annotation object.

Namespace

Drupal\Core\Annotation

Code

public function __construct(array $values) {
  $values += array(
    '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 TranslatableMarkup) {
      $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 = isset($values['class']) ? $values['class'] : 'Drupal\Core\Plugin\Context\ContextDefinition';
  $this->definition = new $class($values['value'], $values['label'], $values['required'], $values['multiple'], $values['description'], $values['default_value']);
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Annotation!ContextDefinition.php/function/ContextDefinition::__construct/8.1.x