diff --git a/core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php b/core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php index c650e6977d..0401b7bdec 100644 --- a/core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php +++ b/core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php @@ -639,18 +639,6 @@ public function save() { return $return; } - /** - * {@inheritdoc} - */ - public function getTypedData() { - if (!isset($this->typedData)) { - /** @var \Drupal\Core\Config\TypedConfigManagerInterface $type_config_manager */ - $type_config_manager = \Drupal::service('config.typed'); - $this->typedData = $type_config_manager->createFromNameAndData($this->getConfigDependencyName(), $this->toArray()); - } - return $this->typedData; - } - /** * {@inheritdoc} */ diff --git a/core/lib/Drupal/Core/Entity/Plugin/DataType/EntityAdapter.php b/core/lib/Drupal/Core/Entity/Plugin/DataType/EntityAdapter.php index 8895440ece..a7f62e79fe 100644 --- a/core/lib/Drupal/Core/Entity/Plugin/DataType/EntityAdapter.php +++ b/core/lib/Drupal/Core/Entity/Plugin/DataType/EntityAdapter.php @@ -46,10 +46,8 @@ class EntityAdapter extends TypedData implements \IteratorAggregate, ComplexData */ public static function createFromEntity(EntityInterface $entity) { $definition = EntityDataDefinition::create() - ->setEntityTypeId($entity->getEntityTypeId()); - if ($entity instanceof FieldableEntityInterface) { - $definition->setBundles([$entity->bundle()]); - } + ->setEntityTypeId($entity->getEntityTypeId()) + ->setBundles([$entity->bundle()]); $instance = new static($definition); $instance->setValue($entity); return $instance; @@ -81,7 +79,7 @@ public function get($property_name) { throw new MissingDataException("Unable to get property $property_name as no entity has been provided."); } if (!$this->entity instanceof FieldableEntityInterface) { - return $this->entity->getTypedData()->get($property_name); + return $this->getConfigTypedData()->get($property_name); } // This will throw an exception for unknown fields. return $this->entity->get($property_name); @@ -94,11 +92,14 @@ public function set($property_name, $value, $notify = TRUE) { if (!isset($this->entity)) { throw new MissingDataException("Unable to set property $property_name as no entity has been provided."); } - if (!$this->entity instanceof FieldableEntityInterface) { - $this->entity->getTypedData()->set($property_name, $value, $notify); + if ($this->entity instanceof ConfigEntityInterface) { + $this->entity->set($property_name, $value, $notify); } - // This will throw an exception for unknown fields. - $this->entity->set($property_name, $value, $notify); + else { + // This will throw an exception for unknown fields. + $this->entity->set($property_name, $value, $notify); + } + return $this; } @@ -110,7 +111,7 @@ public function getProperties($include_computed = FALSE) { throw new MissingDataException('Unable to get properties as no entity has been provided.'); } if (!$this->entity instanceof FieldableEntityInterface) { - return $this->entity->getTypedData()->getProperties($include_computed); + return $this->getConfigTypedData()->getProperties($include_computed); } return $this->entity->getFields($include_computed); } @@ -164,7 +165,15 @@ public function applyDefaultValue($notify = TRUE) { * {@inheritdoc} */ public function getIterator() { - return isset($this->entity) ? $this->entity->getIterator() : new \ArrayIterator([]); + if (isset($this->entity)) { + if ($this->entity instanceof FieldableEntityInterface) { + $this->entity->getIterator(); + } + else { + $this->getConfigTypedData()->getIterator(); + } + } + return new \ArrayIterator([]); } /** @@ -186,4 +195,27 @@ public function getTypedDataManager() { return $this->typedDataManager; } + /** + * Get typed data for config entity. + * + * @return \Drupal\Core\TypedData\TraversableTypedDataInterface + * The typed data. + */ + protected function getConfigTypedData() { + /** @var \Drupal\Core\Config\TypedConfigManagerInterface $type_config_manager */ + $type_config_manager = \Drupal::service('config.typed'); + return $type_config_manager->createFromNameAndData($this->entity->getConfigDependencyName(), $this->entity->toArray()); + } + + /** + * {@inheritdoc} + */ + public function validate() { + if ($this->entity instanceof ConfigEntityInterface) { + return $this->getConfigTypedData()->validate(); + } + return parent::validate(); + } + + }