Pull merge.
[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\Config\TypedConfigManagerInterface;
6 use Drupal\Core\TypedData\Exception\MissingDataException;
7 use Drupal\Core\TypedData\TypedDataManagerInterface;
8
9 /**
10  * Enhances EntityAdapter for config entities.
11  */
12 class ConfigEntityAdapter extends EntityAdapter {
13
14   /**
15    * The wrapped entity object.
16    *
17    * @var \Drupal\Core\Config\Entity\ConfigEntityInterface
18    */
19   protected $entity;
20
21   /**
22    * The typed config manager.
23    *
24    * @var \Drupal\Core\Config\TypedConfigManagerInterface
25    */
26   protected $typedConfigManager;
27
28   /**
29    * {@inheritdoc}
30    */
31   public function get($property_name) {
32     if (!isset($this->entity)) {
33       throw new MissingDataException("Unable to get property $property_name as no entity has been provided.");
34     }
35     return $this->getConfigTypedData()->get($property_name);
36   }
37
38   /**
39    * {@inheritdoc}
40    */
41   public function set($property_name, $value, $notify = TRUE) {
42     if (!isset($this->entity)) {
43       throw new MissingDataException("Unable to set property $property_name as no entity has been provided.");
44     }
45     $this->entity->set($property_name, $value, $notify);
46     return $this;
47   }
48
49   /**
50    * {@inheritdoc}
51    */
52   public function getProperties($include_computed = FALSE) {
53     if (!isset($this->entity)) {
54       throw new MissingDataException('Unable to get properties as no entity has been provided.');
55     }
56     return $this->getConfigTypedData()->getProperties($include_computed);
57   }
58
59   /**
60    * {@inheritdoc}
61    */
62   public function onChange($property_name) {
63     if (isset($this->entity)) {
64       // Let the entity know of any changes.
65       $this->getConfigTypedData()->onChange($property_name);
66     }
67   }
68
69   /**
70    * {@inheritdoc}
71    */
72   public function getIterator() {
73     if (isset($this->entity)) {
74       return $this->getConfigTypedData()->getIterator();
75     }
76     return new \ArrayIterator([]);
77   }
78
79   /**
80    * Gets the typed config manager.
81    *
82    * @return \Drupal\Core\Config\TypedConfigManagerInterface
83    *   The typed config manager.
84    */
85   protected function getTypedConfigManager() {
86     if (empty($this->typedConfigManager)) {
87       // Use the typed data manager if it is also the typed config manager.
88       // @todo Remove this in https://www.drupal.org/node/3011137.
89       $typed_data_manager = $this->getTypedDataManager();
90       if ($typed_data_manager instanceof TypedConfigManagerInterface) {
91         $this->typedConfigManager = $typed_data_manager;
92       }
93       else {
94         $this->typedConfigManager = \Drupal::service('config.typed');
95       }
96     }
97
98     return $this->typedConfigManager;
99   }
100
101   /**
102    * {@inheritdoc}
103    *
104    * @todo Remove this in https://www.drupal.org/node/3011137.
105    */
106   public function getTypedDataManager() {
107     if (empty($this->typedDataManager)) {
108       $this->typedDataManager = \Drupal::service('config.typed');
109     }
110
111     return $this->typedDataManager;
112   }
113
114   /**
115    * {@inheritdoc}
116    *
117    * @todo Remove this in https://www.drupal.org/node/3011137.
118    */
119   public function setTypedDataManager(TypedDataManagerInterface $typed_data_manager) {
120     $this->typedDataManager = $typed_data_manager;
121     if ($typed_data_manager instanceof TypedConfigManagerInterface) {
122       $this->typedConfigManager = $typed_data_manager;
123     }
124     return $this;
125   }
126
127   /**
128    * {@inheritdoc}
129    */
130   public function applyDefaultValue($notify = TRUE) {
131     // @todo Figure out what to do for this method, see
132     //   https://www.drupal.org/project/drupal/issues/2945635.
133     throw new \BadMethodCallException('Method not supported');
134   }
135
136   /**
137    * Gets typed data for config entity.
138    *
139    * @return \Drupal\Core\TypedData\ComplexDataInterface
140    *   The typed data.
141    */
142   protected function getConfigTypedData() {
143     return $this->getTypedConfigManager()->createFromNameAndData($this->entity->getConfigDependencyName(), $this->entity->toArray());
144   }
145
146 }