TypedData.php in Drupal 9
File
core/lib/Drupal/Core/TypedData/TypedData.php
View source
<?php
namespace Drupal\Core\TypedData;
use Drupal\Component\Plugin\PluginInspectionInterface;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\StringTranslation\StringTranslationTrait;
abstract class TypedData implements TypedDataInterface, PluginInspectionInterface {
use DependencySerializationTrait;
use StringTranslationTrait;
use TypedDataTrait;
protected $definition;
protected $name;
protected $parent;
public static function createInstance($definition, $name = NULL, TraversableTypedDataInterface $parent = NULL) {
return new static($definition, $name, $parent);
}
public function __construct(DataDefinitionInterface $definition, $name = NULL, TypedDataInterface $parent = NULL) {
$this->definition = $definition;
$this->parent = $parent;
$this->name = $name;
}
public function getPluginId() {
return $this->definition['type'];
}
public function getPluginDefinition() {
return $this
->getTypedDataManager()
->getDefinition($this->definition
->getDataType());
}
public function getDataDefinition() {
return $this->definition;
}
public function getValue() {
return $this->value;
}
public function setValue($value, $notify = TRUE) {
$this->value = $value;
if ($notify && isset($this->parent)) {
$this->parent
->onChange($this->name);
}
}
public function getString() {
return (string) $this
->getValue();
}
public function getConstraints() {
$constraint_manager = $this
->getTypedDataManager()
->getValidationConstraintManager();
$constraints = [];
foreach ($this->definition
->getConstraints() as $name => $options) {
$constraints[] = $constraint_manager
->create($name, $options);
}
return $constraints;
}
public function validate() {
return $this
->getTypedDataManager()
->getValidator()
->validate($this);
}
public function applyDefaultValue($notify = TRUE) {
$this
->setValue(NULL, $notify);
return $this;
}
public function setContext($name = NULL, TraversableTypedDataInterface $parent = NULL) {
$this->parent = $parent;
$this->name = $name;
}
public function getName() {
return $this->name;
}
public function getRoot() {
if (isset($this->parent)) {
return $this->parent
->getRoot();
}
return $this;
}
public function getPropertyPath() {
if (isset($this->parent)) {
$prefix = $this->parent
->getPropertyPath();
return (strlen($prefix) ? $prefix . '.' : '') . $this->name;
}
elseif (isset($this->name)) {
return $this->name;
}
return '';
}
public function getParent() {
return $this->parent;
}
}
Classes
Name |
Description |
TypedData |
The abstract base class for typed data. |