093c77dd16b110db2f1f78c7852e155d87243002
[yaffs-website] / web / core / lib / Drupal / Core / Entity / Plugin / DataType / ConfigEntityAdapter.php
1 <?php
2
3 namespace Drupal\Core\Entity\Plugin\DataType;
4
5 use Drupal\Core\TypedData\Exception\MissingDataException;
6
7 /**
8  * Enhances EntityAdapter for config entities.
9  */
10 class ConfigEntityAdapter extends EntityAdapter {
11
12   /**
13    * The wrapped entity object.
14    *
15    * @var \Drupal\Core\Config\Entity\ConfigEntityInterface
16    */
17   protected $entity;
18
19   /**
20    * {@inheritdoc}
21    */
22   public function get($property_name) {
23     if (!isset($this->entity)) {
24       throw new MissingDataException("Unable to get property $property_name as no entity has been provided.");
25     }
26     return $this->getConfigTypedData()->get($property_name);
27   }
28
29   /**
30    * {@inheritdoc}
31    */
32   public function set($property_name, $value, $notify = TRUE) {
33     if (!isset($this->entity)) {
34       throw new MissingDataException("Unable to set property $property_name as no entity has been provided.");
35     }
36     $this->entity->set($property_name, $value, $notify);
37     return $this;
38   }
39
40   /**
41    * {@inheritdoc}
42    */
43   public function getProperties($include_computed = FALSE) {
44     if (!isset($this->entity)) {
45       throw new MissingDataException('Unable to get properties as no entity has been provided.');
46     }
47     return $this->getConfigTypedData()->getProperties($include_computed);
48   }
49
50   /**
51    * {@inheritdoc}
52    */
53   public function onChange($property_name) {
54     if (isset($this->entity)) {
55       // Let the entity know of any changes.
56       $this->getConfigTypedData()->onChange($property_name);
57     }
58   }
59
60   /**
61    * {@inheritdoc}
62    */
63   public function getIterator() {
64     if (isset($this->entity)) {
65       return $this->getConfigTypedData()->getIterator();
66     }
67     return new \ArrayIterator([]);
68   }
69
70   /**
71    * Gets the typed data manager.
72    *
73    * @return \Drupal\Core\Config\TypedConfigManagerInterface
74    *   The typed data manager.
75    */
76   public function getTypedDataManager() {
77     if (empty($this->typedDataManager)) {
78       $this->typedDataManager = \Drupal::service('config.typed');
79     }
80
81     return $this->typedDataManager;
82   }
83
84   /**
85    * {@inheritdoc}
86    */
87   public function applyDefaultValue($notify = TRUE) {
88     // @todo Figure out what to do for this method, see
89     //   https://www.drupal.org/project/drupal/issues/2945635.
90     throw new \BadMethodCallException('Method not supported');
91   }
92
93   /**
94    * Gets typed data for config entity.
95    *
96    * @return \Drupal\Core\TypedData\ComplexDataInterface
97    *   The typed data.
98    */
99   protected function getConfigTypedData() {
100     return $this->getTypedDataManager()->createFromNameAndData($this->entity->getConfigDependencyName(), $this->entity->toArray());
101   }
102
103 }