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 * @todo When \Drupal\Core\Config\TypedConfigManager has been fixed to use
64 * class-based definitions, type-hint $definition to
65 * DataDefinitionInterface. https://www.drupal.org/node/1928868
67 public function __construct($definition, $name = NULL, TypedDataInterface $parent = NULL) {
68 $this->definition = $definition;
69 $this->parent = $parent;
76 public function getPluginId() {
77 return $this->definition['type'];
83 public function getPluginDefinition() {
84 return $this->getTypedDataManager()->getDefinition($this->definition->getDataType());
90 public function getDataDefinition() {
91 return $this->definition;
97 public function getValue() {
104 public function setValue($value, $notify = TRUE) {
105 $this->value = $value;
106 // Notify the parent of any changes.
107 if ($notify && isset($this->parent)) {
108 $this->parent->onChange($this->name);
115 public function getString() {
116 return (string) $this->getValue();
122 public function getConstraints() {
123 $constraint_manager = $this->getTypedDataManager()->getValidationConstraintManager();
125 foreach ($this->definition->getConstraints() as $name => $options) {
126 $constraints[] = $constraint_manager->create($name, $options);
134 public function validate() {
135 return $this->getTypedDataManager()->getValidator()->validate($this);
141 public function applyDefaultValue($notify = TRUE) {
142 // Default to no default value.
143 $this->setValue(NULL, $notify);
150 public function setContext($name = NULL, TraversableTypedDataInterface $parent = NULL) {
151 $this->parent = $parent;
158 public function getName() {
165 public function getRoot() {
166 if (isset($this->parent)) {
167 return $this->parent->getRoot();
169 // If no parent is set, this is the root of the data tree.
176 public function getPropertyPath() {
177 if (isset($this->parent)) {
178 // The property path of this data object is the parent's path appended
179 // by this object's name.
180 $prefix = $this->parent->getPropertyPath();
181 return (strlen($prefix) ? $prefix . '.' : '') . $this->name;
183 // If no parent is set, this is the root of the data tree. Thus the property
184 // path equals the name of this data object.
185 elseif (isset($this->name)) {
194 public function getParent() {
195 return $this->parent;
201 public function __sleep() {
202 $vars = get_object_vars($this);
203 // Prevent services from being serialized. static::getStringTranslation()
204 // and static::getTypedDataManager() lazy-load them after $this has been
206 // @todo Replace this with
207 // \Drupal\Core\DependencyInjection\DependencySerializationTrait before
208 // Drupal 9.0.0. We cannot use that now, because child classes already use
209 // it and PHP 5 would consider that conflicts.
210 unset($vars['stringTranslation']);
211 unset($vars['typedDataManager']);
213 return array_keys($vars);