Version 1
[yaffs-website] / web / core / lib / Drupal / Core / TypedData / DataReferenceBase.php
1 <?php
2
3 namespace Drupal\Core\TypedData;
4
5 /**
6  * Base class for typed data references.
7  *
8  * Data types based on this base class need to be named
9  * "{TARGET_TYPE}_reference", whereas {TARGET_TYPE} is the referenced data type.
10  * For example, an entity reference data type would have to be named
11  * "entity_reference".
12  * Beside that, implementing classes have to implement at least
13  * \Drupal\Core\TypedData\DataReferenceInterface::getTargetIdentifier().
14  *
15  * @see \Drupal\Core\TypedData\DataReferenceDefinition
16  */
17 abstract class DataReferenceBase extends TypedData implements DataReferenceInterface {
18
19   /**
20    * The referenced data.
21    *
22    * @var \Drupal\Core\TypedData\TypedDataInterface
23    */
24   protected $target;
25
26   /**
27    * {@inheritdoc}
28    */
29   public function getTarget() {
30     return $this->target;
31   }
32
33   /**
34    * {@inheritdoc}
35    */
36   public function getValue() {
37     if ($target = $this->getTarget()) {
38       return $target->getValue();
39     }
40   }
41
42   /**
43    * {@inheritdoc}
44    */
45   public function setValue($value, $notify = TRUE) {
46     $this->target = $this->getTypedDataManager()->create($this->definition->getTargetDefinition(), $value);
47     // Notify the parent of any changes.
48     if ($notify && isset($this->parent)) {
49       $this->parent->onChange($this->name);
50     }
51   }
52
53   /**
54    * {@inheritdoc}
55    */
56   public function getString() {
57     return (string) $this->getType() . ':' . $this->getTargetIdentifier();
58   }
59
60 }