3 namespace Drupal\Core\Entity\Plugin\DataType;
5 use Drupal\Core\Config\TypedConfigManagerInterface;
6 use Drupal\Core\TypedData\Exception\MissingDataException;
7 use Drupal\Core\TypedData\TypedDataManagerInterface;
10 * Enhances EntityAdapter for config entities.
12 class ConfigEntityAdapter extends EntityAdapter {
15 * The wrapped entity object.
17 * @var \Drupal\Core\Config\Entity\ConfigEntityInterface
22 * The typed config manager.
24 * @var \Drupal\Core\Config\TypedConfigManagerInterface
26 protected $typedConfigManager;
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.");
35 return $this->getConfigTypedData()->get($property_name);
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.");
45 $this->entity->set($property_name, $value, $notify);
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.');
56 return $this->getConfigTypedData()->getProperties($include_computed);
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);
72 public function getIterator() {
73 if (isset($this->entity)) {
74 return $this->getConfigTypedData()->getIterator();
76 return new \ArrayIterator([]);
80 * Gets the typed config manager.
82 * @return \Drupal\Core\Config\TypedConfigManagerInterface
83 * The typed config manager.
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;
94 $this->typedConfigManager = \Drupal::service('config.typed');
98 return $this->typedConfigManager;
104 * @todo Remove this in https://www.drupal.org/node/3011137.
106 public function getTypedDataManager() {
107 if (empty($this->typedDataManager)) {
108 $this->typedDataManager = \Drupal::service('config.typed');
111 return $this->typedDataManager;
117 * @todo Remove this in https://www.drupal.org/node/3011137.
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;
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');
137 * Gets typed data for config entity.
139 * @return \Drupal\Core\TypedData\ComplexDataInterface
142 protected function getConfigTypedData() {
143 return $this->getTypedConfigManager()->createFromNameAndData($this->entity->getConfigDependencyName(), $this->entity->toArray());