diff --git a/core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php b/core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php index 95bcf58..b3e23ae 100644 --- a/core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php +++ b/core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php @@ -95,14 +95,10 @@ public function __construct(array $values, $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])) { - $new_key = $config_mapping[$key]; - $values[$new_key] = $value; - unset($values[$key]); + $key = $config_mapping[$key]; } - } - // Set initial values. - foreach ($values as $key => $value) { $this->$key = $value; } @@ -257,25 +253,17 @@ public static function sort(ConfigEntityInterface $a, ConfigEntityInterface $b) public function toArray() { $properties = array(); $config_name = $this->getEntityType()->getConfigPrefix() . '.' . $this->id(); - $config_mapping = $this->getEntityType()->getConfigMapping(); $definition = $this->getTypedConfig()->getDefinition($config_name); if (!isset($definition['mapping'])) { throw new SchemaIncompleteException(String::format('Incomplete or missing schema for @config_name', array('@config_name' => $config_name))); } - $config_keys = array_keys($definition['mapping']); - if (empty($config_mapping)) { - // If no config mapping was defined, assume that the entity properties - // match the config keys. - $config_mapping = array_combine($config_keys, $config_keys); - } - $id_key = $this->getEntityType()->getKey('id'); - foreach ($config_keys as $name) { - if (!isset($config_mapping[$name])) { - continue; + $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]; } - - $name = $config_mapping[$name]; // Special handling for IDs so that computed compound IDs work. // @see \Drupal\Core\Entity\EntityDisplayBase::id() if ($name == $id_key) { diff --git a/core/lib/Drupal/Core/Config/Entity/ConfigEntityType.php b/core/lib/Drupal/Core/Config/Entity/ConfigEntityType.php index 15a9b51..845e3ee 100644 --- a/core/lib/Drupal/Core/Config/Entity/ConfigEntityType.php +++ b/core/lib/Drupal/Core/Config/Entity/ConfigEntityType.php @@ -60,7 +60,7 @@ class ConfigEntityType extends EntityType { * * @var array */ - protected $config_mapping; + protected $config_mapping = []; /** * {@inheritdoc} diff --git a/core/lib/Drupal/Core/Entity/EntityType.php b/core/lib/Drupal/Core/Entity/EntityType.php index c2b0b9b..a26ffc4 100644 --- a/core/lib/Drupal/Core/Entity/EntityType.php +++ b/core/lib/Drupal/Core/Entity/EntityType.php @@ -631,7 +631,7 @@ public function getConfigPrefix() { * {@inheritdoc} */ public function getConfigMapping() { - return FALSE; + return []; } /** diff --git a/core/lib/Drupal/Core/Entity/EntityTypeInterface.php b/core/lib/Drupal/Core/Entity/EntityTypeInterface.php index 5ccae81..fc100c7 100644 --- a/core/lib/Drupal/Core/Entity/EntityTypeInterface.php +++ b/core/lib/Drupal/Core/Entity/EntityTypeInterface.php @@ -592,9 +592,8 @@ public function getConfigPrefix(); * * @todo Used for configuration entities only. * - * @return array|bool - * An array mapping each config key to its entity property, or FALSE if - * not a configuration entity type. + * @return array + * An array mapping each config key to its entity property. */ public function getConfigMapping(); diff --git a/core/modules/node/src/Entity/NodeType.php b/core/modules/node/src/Entity/NodeType.php index defe45f..f3cff98 100644 --- a/core/modules/node/src/Entity/NodeType.php +++ b/core/modules/node/src/Entity/NodeType.php @@ -30,14 +30,9 @@ * admin_permission = "administer content types", * config_prefix = "type", * config_mapping = { - * "name" = "name", - * "type" = "type" - * "description" = "description" - * "help" = "help" - * "new_revision" = "newRevision" - * "preview_mode" = "previewMode" - * "display_submitted" = "displaySubmitted" - * "third_party_settings" = "third_party_settings" + * "new_revision" = "newRevision", + * "preview_mode" = "previewMode", + * "display_submitted" = "displaySubmitted", * }, * bundle_of = "node", * entity_keys = { diff --git a/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityBaseUnitTest.php b/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityBaseUnitTest.php index 7af62e5..5feb644 100644 --- a/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityBaseUnitTest.php +++ b/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityBaseUnitTest.php @@ -7,9 +7,7 @@ namespace Drupal\Tests\Core\Config\Entity; -use Drupal\Component\Plugin\ConfigurablePluginInterface; use Drupal\Core\DependencyInjection\ContainerBuilder; -use Drupal\Component\Plugin\PluginBase; use Drupal\Core\Language\Language; use Drupal\Tests\Core\Plugin\Fixtures\TestConfigurablePlugin; use Drupal\Tests\UnitTestCase; @@ -95,11 +93,6 @@ class ConfigEntityBaseUnitTest extends UnitTestCase { */ protected function setUp() { $this->id = $this->randomMachineName(); - $values = array( - 'id' => $this->id, - 'langcode' => 'en', - 'uuid' => '3bb9ee60-bea5-4622-b89b-a63319d10b3a', - ); $this->entityTypeId = $this->randomMachineName(); $this->provider = $this->randomMachineName(); $this->entityType = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface'); @@ -136,7 +129,24 @@ protected function setUp() { $container->set('config.typed', $this->typedConfigManager); \Drupal::setContainer($container); - $this->entity = $this->getMockForAbstractClass('\Drupal\Core\Config\Entity\ConfigEntityBase', array($values, $this->entityTypeId)); + $this->entity = $this->setUpEntity(); + } + + /** + * Sets up an entity object for testing. + * + * @param array $values + * An array of values to pass to the entity constructor. + * + * @return \Drupal\Core\Config\Entity\ConfigEntityBase|\PHPUnit_Framework_MockObject_MockObject + */ + protected function setUpEntity(array $values = []) { + $values += array( + 'id' => $this->id, + 'langcode' => 'en', + 'uuid' => '3bb9ee60-bea5-4622-b89b-a63319d10b3a', + ); + return $this->getMockForAbstractClass('\Drupal\Core\Config\Entity\ConfigEntityBase', array($values, $this->entityTypeId)); } /** @@ -467,4 +477,20 @@ public function testToArrayFallback() { $this->entity->toArray(); } + /** + * @covers ::toArray + */ + public function testToArrayConfigMapping() { + $this->typedConfigManager->expects($this->once()) + ->method('getDefinition') + ->will($this->returnValue(['mapping' => ['id' => '', 'dependencies' => '', 'foo_bar' => '']])); + $this->entityType->expects($this->atLeastOnce()) + ->method('getConfigMapping') + ->willReturn(['foo_bar' => 'fooBar']); + $entity = $this->setUpEntity(['foo_bar' => 5]); + $properties = $this->entity->toArray(); + $this->assertInternalType('array', $properties); + $this->assertEquals(['id' => $entity->id(), 'dependencies' => [], 'fooBar' => 5], $properties); + } + }