3 namespace Drupal\Core\TypedData;
5 use Drupal\Component\Plugin\PluginInspectionInterface;
6 use Drupal\Core\StringTranslation\StringTranslationTrait;
9 * The abstract base class for typed data.
11 * Classes deriving from this base class have to declare $value
12 * or override getValue() or setValue().
16 abstract class TypedData implements TypedDataInterface, PluginInspectionInterface {
18 use StringTranslationTrait;
22 * The data definition.
24 * @var \Drupal\Core\TypedData\DataDefinitionInterface
26 protected $definition;
36 * The parent typed data object.
38 * @var \Drupal\Core\TypedData\TraversableTypedDataInterface|null
45 public static function createInstance($definition, $name = NULL, TraversableTypedDataInterface $parent = NULL) {
46 return new static($definition, $name, $parent);
50 * Constructs a TypedData object given its definition and context.
52 * @param \Drupal\Core\TypedData\DataDefinitionInterface $definition
53 * The data definition.
55 * (optional) The name of the created property, or NULL if it is the root
56 * of a typed data tree. Defaults to NULL.
57 * @param \Drupal\Core\TypedData\TypedDataInterface $parent
58 * (optional) The parent object of the data property, or NULL if it is the
59 * root of a typed data tree. Defaults to NULL.
61 * @see \Drupal\Core\TypedData\TypedDataManager::create()
63 public function __construct(DataDefinitionInterface $definition, $name = NULL, TypedDataInterface $parent = NULL) {
64 $this->definition = $definition;
65 $this->parent = $parent;
72 public function getPluginId() {
73 return $this->definition['type'];
79 public function getPluginDefinition() {
80 return $this->getTypedDataManager()->getDefinition($this->definition->getDataType());
86 public function getDataDefinition() {
87 return $this->definition;
93 public function getValue() {
100 public function setValue($value, $notify = TRUE) {
101 $this->value = $value;
102 // Notify the parent of any changes.
103 if ($notify && isset($this->parent)) {
104 $this->parent->onChange($this->name);
111 public function getString() {
112 return (string) $this->getValue();
118 public function getConstraints() {
119 $constraint_manager = $this->getTypedDataManager()->getValidationConstraintManager();
121 foreach ($this->definition->getConstraints() as $name => $options) {
122 $constraints[] = $constraint_manager->create($name, $options);
130 public function validate() {
131 return $this->getTypedDataManager()->getValidator()->validate($this);
137 public function applyDefaultValue($notify = TRUE) {
138 // Default to no default value.
139 $this->setValue(NULL, $notify);
146 public function setContext($name = NULL, TraversableTypedDataInterface $parent = NULL) {
147 $this->parent = $parent;
154 public function getName() {
161 public function getRoot() {
162 if (isset($this->parent)) {
163 return $this->parent->getRoot();
165 // If no parent is set, this is the root of the data tree.
172 public function getPropertyPath() {
173 if (isset($this->parent)) {
174 // The property path of this data object is the parent's path appended
175 // by this object's name.
176 $prefix = $this->parent->getPropertyPath();
177 return (strlen($prefix) ? $prefix . '.' : '') . $this->name;
179 // If no parent is set, this is the root of the data tree. Thus the property
180 // path equals the name of this data object.
181 elseif (isset($this->name)) {
190 public function getParent() {
191 return $this->parent;
197 public function __sleep() {
198 $vars = get_object_vars($this);
199 // Prevent services from being serialized. static::getStringTranslation()
200 // and static::getTypedDataManager() lazy-load them after $this has been
202 // @todo Replace this with
203 // \Drupal\Core\DependencyInjection\DependencySerializationTrait before
204 // Drupal 9.0.0. We cannot use that now, because child classes already use
205 // it and PHP 5 would consider that conflicts.
206 unset($vars['stringTranslation']);
207 unset($vars['typedDataManager']);
209 return array_keys($vars);