diff --git a/core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php b/core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php index b3e23ae..a466b2a 100644 --- a/core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php +++ b/core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php @@ -91,16 +91,7 @@ * Overrides Entity::__construct(). */ public function __construct(array $values, $entity_type) { - $this->entityTypeId = $entity_type; - // If needed, map the config keys to entity properties. - $config_mapping = $this->getEntityType()->getConfigMapping(); - foreach ($values as $key => $value) { - // Check for a config mapping, otherwise use the default key. - if (isset($config_mapping[$key])) { - $key = $config_mapping[$key]; - } - $this->$key = $value; - } + parent::__construct($values, $entity_type); // Backup the original ID, if any. // Configuration entity IDs are strings, and '0' is a valid ID. @@ -260,17 +251,14 @@ public function toArray() { $id_key = $this->getEntityType()->getKey('id'); $config_mapping = $this->getEntityType()->getConfigMapping(); foreach (array_keys($definition['mapping']) as $name) { - // Check for a config mapping, otherwise use the default name. - if (isset($config_mapping[$name])) { - $name = $config_mapping[$name]; - } // Special handling for IDs so that computed compound IDs work. // @see \Drupal\Core\Entity\EntityDisplayBase::id() if ($name == $id_key) { $properties[$name] = $this->id(); } else { - $properties[$name] = $this->get($name); + $property_name = isset($config_mapping[$name]) ? $config_mapping[$name] : $name; + $properties[$name] = $this->get($property_name); } } return $properties; @@ -288,6 +276,21 @@ protected function getTypedConfig() { /** * {@inheritdoc} */ + public static function preCreate(EntityStorageInterface $storage, array &$values) { + // If needed, map the config keys to entity properties. + $config_mapping = $storage->getEntityType()->getConfigMapping(); + foreach ($values as $key => $value) { + if (isset($config_mapping[$key])) { + $new_key = $config_mapping[$key]; + $values[$new_key] = $value; + unset($values[$key]); + } + } + } + + /** + * {@inheritdoc} + */ public function preSave(EntityStorageInterface $storage) { parent::preSave($storage); diff --git a/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityBaseUnitTest.php b/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityBaseUnitTest.php index 5feb644..d5d03d8 100644 --- a/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityBaseUnitTest.php +++ b/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityBaseUnitTest.php @@ -478,6 +478,7 @@ public function testToArrayFallback() { } /** + * @covers ::preCreate * @covers ::toArray */ public function testToArrayConfigMapping() { @@ -487,10 +488,22 @@ public function testToArrayConfigMapping() { $this->entityType->expects($this->atLeastOnce()) ->method('getConfigMapping') ->willReturn(['foo_bar' => 'fooBar']); - $entity = $this->setUpEntity(['foo_bar' => 5]); - $properties = $this->entity->toArray(); + $storage = $this->getMock('\Drupal\Core\Config\Entity\ConfigEntityStorageInterface'); + $storage->expects($this->any()) + ->method('getEntityType') + ->will($this->returnValue($this->entityType)); + + // Confirm that preCreate() maps the values as expected. + $values = ['foo_bar' => 5]; + $entity_class = get_class($this->entity); + $entity_class::preCreate($storage, $values); + $this->assertEquals(['fooBar' => 5], $values); + + // Confirm that toArray() maps the properties back to config keys. + $this->entity = $this->setUpEntity($values); + $properties = $this->entity->toArray(true); $this->assertInternalType('array', $properties); - $this->assertEquals(['id' => $entity->id(), 'dependencies' => [], 'fooBar' => 5], $properties); + $this->assertEquals(['id' => $this->entity->id(), 'dependencies' => [], 'foo_bar' => 5], $properties); } }