class TypedData

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/TypedData/TypedData.php \Drupal\Core\TypedData\TypedData
  2. 8.9.x core/lib/Drupal/Core/TypedData/TypedData.php \Drupal\Core\TypedData\TypedData
  3. 10 core/lib/Drupal/Core/TypedData/TypedData.php \Drupal\Core\TypedData\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

Related topics

13 files declare their use of TypedData
Any.php in core/lib/Drupal/Core/TypedData/Plugin/DataType/Any.php
ComputedFileUrl.php in core/modules/file/src/ComputedFileUrl.php
ComputedString.php in core/modules/system/tests/modules/entity_test/src/TypedData/ComputedString.php
DateTimeComputed.php in core/modules/datetime/src/DateTimeComputed.php
Element.php in core/lib/Drupal/Core/Config/Schema/Element.php

... See full list

1 string reference to 'TypedData'
ContentEntityCloneTest::testEntityPropertiesModifications in core/tests/Drupal/KernelTests/Core/Entity/ContentEntityCloneTest.php
Tests references of entity properties after entity cloning.

File

core/lib/Drupal/Core/TypedData/TypedData.php, line 17

Namespace

Drupal\Core\TypedData
View source
abstract class TypedData implements TypedDataInterface, PluginInspectionInterface {
    use DependencySerializationTrait;
    use StringTranslationTrait;
    use TypedDataTrait;
    
    /**
     * The data definition.
     *
     * @var \Drupal\Core\TypedData\DataDefinitionInterface
     */
    protected $definition;
    
    /**
     * The property name.
     *
     * @var string
     */
    protected $name;
    
    /**
     * The parent typed data object.
     *
     * @var \Drupal\Core\TypedData\TraversableTypedDataInterface|null
     */
    protected $parent;
    
    /**
     * {@inheritdoc}
     */
    public static function createInstance($definition, $name = NULL, ?TraversableTypedDataInterface $parent = NULL) {
        return new static($definition, $name, $parent);
    }
    
    /**
     * Constructs a TypedData object given its definition and context.
     *
     * @param \Drupal\Core\TypedData\DataDefinitionInterface $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(DataDefinitionInterface $definition, $name = NULL, ?TypedDataInterface $parent = NULL) {
        $this->definition = $definition;
        $this->parent = $parent;
        $this->name = $name;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getPluginId() {
        return $this->definition['type'];
    }
    
    /**
     * {@inheritdoc}
     */
    public function getPluginDefinition() {
        return $this->getTypedDataManager()
            ->getDefinition($this->definition
            ->getDataType());
    }
    
    /**
     * {@inheritdoc}
     */
    public function getDataDefinition() {
        return $this->definition;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getValue() {
        return $this->value;
    }
    
    /**
     * {@inheritdoc}
     */
    public function setValue($value, $notify = TRUE) {
        $this->value = $value;
        // Notify the parent of any changes.
        if ($notify && isset($this->parent)) {
            $this->parent
                ->onChange($this->name);
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public function getString() {
        return (string) $this->getValue();
    }
    
    /**
     * {@inheritdoc}
     */
    public function getConstraints() {
        $constraint_manager = $this->getTypedDataManager()
            ->getValidationConstraintManager();
        $constraints = [];
        foreach ($this->definition
            ->getConstraints() as $name => $options) {
            $constraints[] = $constraint_manager->create($name, $options);
        }
        return $constraints;
    }
    
    /**
     * {@inheritdoc}
     */
    public function validate() {
        return $this->getTypedDataManager()
            ->getValidator()
            ->validate($this);
    }
    
    /**
     * {@inheritdoc}
     */
    public function applyDefaultValue($notify = TRUE) {
        // Default to no default value.
        $this->setValue(NULL, $notify);
        return $this;
    }
    
    /**
     * {@inheritdoc}
     */
    public function setContext($name = NULL, ?TraversableTypedDataInterface $parent = NULL) {
        $this->parent = $parent;
        $this->name = $name;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getName() {
        return $this->name;
    }
    
    /**
     * {@inheritdoc}
     */
    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;
    }
    
    /**
     * {@inheritdoc}
     */
    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();
            // Variables in double quotes used to leverage fast string concatenation.
            // In PHP 7+ concatenation with variable inside string is the fastest.
            // @see https://blog.blackfire.io/php-7-performance-improvements-encapsed-strings-optimization.html
            // This is being done because the code can run in the critical path.
            return $prefix !== '' ? "{$prefix}.{$this->name}" : $this->name;
        }
        elseif (isset($this->name)) {
            return $this->name;
        }
        return '';
    }
    
    /**
     * {@inheritdoc}
     */
    public function getParent() {
        return $this->parent;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
StringTranslationTrait::$stringTranslation protected property The string translation service. 3
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
TypedData::$definition protected property The data definition. 1
TypedData::$name protected property The property name.
TypedData::$parent protected property The parent typed data object.
TypedData::applyDefaultValue public function Applies the default value. Overrides TypedDataInterface::applyDefaultValue 3
TypedData::createInstance public static function Constructs a TypedData object given its definition and context. Overrides TypedDataInterface::createInstance
TypedData::getConstraints public function Gets a list of validation constraints. Overrides TypedDataInterface::getConstraints 8
TypedData::getDataDefinition public function Gets the data definition. Overrides TypedDataInterface::getDataDefinition
TypedData::getName public function Returns the name of a property or item. Overrides TypedDataInterface::getName
TypedData::getParent public function Returns the parent data structure; i.e. either complex data or a list. Overrides TypedDataInterface::getParent
TypedData::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition
TypedData::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
TypedData::getPropertyPath public function Returns the property path of the data. Overrides TypedDataInterface::getPropertyPath
TypedData::getRoot public function Returns the root of the typed data tree. Overrides TypedDataInterface::getRoot
TypedData::getString public function Returns a string representation of the data. Overrides TypedDataInterface::getString 6
TypedData::getValue public function Gets the data value. Overrides TypedDataInterface::getValue 10
TypedData::setContext public function Sets the context of a property or item via a context aware parent. Overrides TypedDataInterface::setContext
TypedData::setValue public function Sets the data value. Overrides TypedDataInterface::setValue 10
TypedData::validate public function Validates the currently set data value. Overrides TypedDataInterface::validate
TypedData::__construct public function Constructs a TypedData object given its definition and context. 4
TypedDataTrait::$typedDataManager protected property The typed data manager used for creating the data types.
TypedDataTrait::getTypedDataManager public function Gets the typed data manager. 2
TypedDataTrait::setTypedDataManager public function Sets the typed data manager. 2

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