abstract class TypedData

The abstract base class for typed data.

Classes deriving from this base class have to declare $value or override getValue() or setValue().

Hierarchy

Expanded class hierarchy of TypedData

16 files declare their use of TypedData
Any.php in drupal/core/lib/Drupal/Core/TypedData/Type/Any.php
Contains \Drupal\Core\TypedData\Type\Any.
Binary.php in drupal/core/lib/Drupal/Core/TypedData/Type/Binary.php
Contains \Drupal\Core\TypedData\Type\Binary.
Boolean.php in drupal/core/lib/Drupal/Core/TypedData/Type/Boolean.php
Contains \Drupal\Core\TypedData\Type\Boolean.
Date.php in drupal/core/lib/Drupal/Core/TypedData/Type/Date.php
Contains \Drupal\Core\TypedData\Type\Date.
Duration.php in drupal/core/lib/Drupal/Core/TypedData/Type/Duration.php
Contains \Drupal\Core\TypedData\Type\Duration.

... See full list

File

drupal/core/lib/Drupal/Core/TypedData/TypedData.php, line 16
Contains \Drupal\Core\TypedData\TypedData.

Namespace

Drupal\Core\TypedData
View source
abstract class TypedData implements TypedDataInterface {

  /**
   * The data definition.
   *
   * @var array
   */
  protected $definition;

  /**
   * The property name.
   *
   * @var string
   */
  protected $name;

  /**
   * The parent typed data object.
   *
   * @var \Drupal\Core\TypedData\TypedDataInterface
   */
  protected $parent;

  /**
   * Constructs a TypedData object given its definition and context.
   *
   * @param array $definition
   *   The data definition.
   * @param string $name
   *   (optional) The name of the created property, or NULL if it is the root
   *   of a typed data tree. Defaults to NULL.
   * @param \Drupal\Core\TypedData\TypedDataInterface $parent
   *   (optional) The parent object of the data property, or NULL if it is the
   *   root of a typed data tree. Defaults to NULL.
   *
   * @see Drupal\Core\TypedData\TypedDataManager::create()
   */
  public function __construct(array $definition, $name = NULL, TypedDataInterface $parent = NULL) {
    $this->definition = $definition;
    $this->parent = $parent;
    $this->name = $name;
  }

  /**
   * Implements \Drupal\Core\TypedData\TypedDataInterface::getType().
   */
  public function getType() {
    return $this->definition['type'];
  }

  /**
   * Implements \Drupal\Core\TypedData\TypedDataInterface::getDefinition().
   */
  public function getDefinition() {
    return $this->definition;
  }

  /**
   * Implements \Drupal\Core\TypedData\TypedDataInterface::getValue().
   */
  public function getValue() {
    return $this->value;
  }

  /**
   * Implements \Drupal\Core\TypedData\TypedDataInterface::setValue().
   */
  public function setValue($value, $notify = TRUE) {

    // Notify the parent of any changes to be made.
    if ($notify && isset($this->parent)) {
      $this->parent
        ->onChange($this->name);
    }
    $this->value = $value;
  }

  /**
   * Implements \Drupal\Core\TypedData\TypedDataInterface::getString().
   */
  public function getString() {
    return (string) $this
      ->getValue();
  }

  /**
   * Implements \Drupal\Core\TypedData\TypedDataInterface::getConstraints().
   */
  public function getConstraints() {

    // @todo: Add the typed data manager as proper dependency.
    return \Drupal::typedData()
      ->getConstraints($this->definition);
  }

  /**
   * Implements \Drupal\Core\TypedData\TypedDataInterface::validate().
   */
  public function validate() {

    // @todo: Add the typed data manager as proper dependency.
    return \Drupal::typedData()
      ->getValidator()
      ->validate($this);
  }

  /**
   * Implements \Drupal\Core\TypedData\TypedDataInterface::setContext().
   */
  public function setContext($name = NULL, TypedDataInterface $parent = NULL) {
    $this->parent = $parent;
    $this->name = $name;
  }

  /**
   * Implements \Drupal\Core\TypedData\TypedDataInterface::getName().
   */
  public function getName() {
    return $this->name;
  }

  /**
   * Implements \Drupal\Core\TypedData\TypedDataInterface::getRoot().
   */
  public function getRoot() {
    if (isset($this->parent)) {
      return $this->parent
        ->getRoot();
    }

    // If no parent is set, this is the root of the data tree.
    return $this;
  }

  /**
   * Implements \Drupal\Core\TypedData\TypedDataInterface::getPropertyPath().
   */
  public function getPropertyPath() {
    if (isset($this->parent)) {

      // The property path of this data object is the parent's path appended
      // by this object's name.
      $prefix = $this->parent
        ->getPropertyPath();
      return (strlen($prefix) ? $prefix . '.' : '') . $this->name;
    }
    elseif (isset($this->name)) {
      return $this->name;
    }
    return '';
  }

  /**
   * Implements \Drupal\Core\TypedData\TypedDataInterface::getParent().
   *
   * @return \Drupal\Core\Entity\Field\FieldInterface
   */
  public function getParent() {
    return $this->parent;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
TypedData::$definition protected property The data definition.
TypedData::$name protected property The property name.
TypedData::$parent protected property The parent typed data object.
TypedData::getConstraints public function Implements \Drupal\Core\TypedData\TypedDataInterface::getConstraints(). Overrides TypedDataInterface::getConstraints 2
TypedData::getDefinition public function Implements \Drupal\Core\TypedData\TypedDataInterface::getDefinition(). Overrides TypedDataInterface::getDefinition
TypedData::getName public function Implements \Drupal\Core\TypedData\TypedDataInterface::getName(). Overrides TypedDataInterface::getName
TypedData::getParent public function Implements \Drupal\Core\TypedData\TypedDataInterface::getParent(). Overrides TypedDataInterface::getParent
TypedData::getPropertyPath public function Implements \Drupal\Core\TypedData\TypedDataInterface::getPropertyPath(). Overrides TypedDataInterface::getPropertyPath
TypedData::getRoot public function Implements \Drupal\Core\TypedData\TypedDataInterface::getRoot(). Overrides TypedDataInterface::getRoot
TypedData::getString public function Implements \Drupal\Core\TypedData\TypedDataInterface::getString(). Overrides TypedDataInterface::getString 7
TypedData::getType public function Implements \Drupal\Core\TypedData\TypedDataInterface::getType(). Overrides TypedDataInterface::getType
TypedData::getValue public function Implements \Drupal\Core\TypedData\TypedDataInterface::getValue(). Overrides TypedDataInterface::getValue 8
TypedData::setContext public function Implements \Drupal\Core\TypedData\TypedDataInterface::setContext(). Overrides TypedDataInterface::setContext 1
TypedData::setValue public function Implements \Drupal\Core\TypedData\TypedDataInterface::setValue(). Overrides TypedDataInterface::setValue 10
TypedData::validate public function Implements \Drupal\Core\TypedData\TypedDataInterface::validate(). Overrides TypedDataInterface::validate 3
TypedData::__construct public function Constructs a TypedData object given its definition and context. 5