diff --git a/core/lib/Drupal/Core/Config/Entity/ConfigEntityForm.php b/core/lib/Drupal/Core/Config/Entity/ConfigEntityForm.php new file mode 100644 index 0000000..7a78f07 --- /dev/null +++ b/core/lib/Drupal/Core/Config/Entity/ConfigEntityForm.php @@ -0,0 +1,41 @@ +getValues(); + + if ($this->entity instanceof EntityWithPluginCollectionInterface) { + // Do not manually update values represented by plugin collections. + $values = array_diff_key($values, $this->entity->getPluginCollections()); + } + + $config_mapping = $this->entity->getEntityType()->getConfigMapping(); + foreach ($values as $key => $value) { + // If needed, map the config keys to entity properties. + $property_name = isset($config_mapping[$key]) ? $config_mapping[$key] : $key; + $entity->set($property_name, $value); + } + } + +} diff --git a/core/lib/Drupal/Core/Entity/EntityForm.php b/core/lib/Drupal/Core/Entity/EntityForm.php index a0b18fa..f8fb0df 100644 --- a/core/lib/Drupal/Core/Entity/EntityForm.php +++ b/core/lib/Drupal/Core/Entity/EntityForm.php @@ -299,19 +299,7 @@ public function buildEntity(array $form, FormStateInterface $form_state) { * The current state of the form. */ protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) { - $values = $form_state->getValues(); - - if ($this->entity instanceof EntityWithPluginCollectionInterface) { - // Do not manually update values represented by plugin collections. - $values = array_diff_key($values, $this->entity->getPluginCollections()); - } - - // @todo: This relies on a method that only exists for config and content - // entities, in a different way. Consider moving this logic to a config - // entity specific implementation. - foreach ($values as $key => $value) { - $entity->set($key, $value); - } + // Overriden by ConfigEntityForm and ContentEntityForm with specific logic. } /** diff --git a/core/modules/node/src/NodeTypeForm.php b/core/modules/node/src/NodeTypeForm.php index 2292cf0..c7847a6 100644 --- a/core/modules/node/src/NodeTypeForm.php +++ b/core/modules/node/src/NodeTypeForm.php @@ -7,7 +7,7 @@ namespace Drupal\node; -use Drupal\Core\Entity\EntityForm; +use Drupal\Core\Config\Entity\ConfigEntityForm; use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Component\Utility\String; use Drupal\Core\Entity\EntityTypeInterface; @@ -19,7 +19,7 @@ /** * Form controller for node type forms. */ -class NodeTypeForm extends EntityForm { +class NodeTypeForm extends ConfigEntityForm { /** * The entity manager. diff --git a/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityFormTest.php b/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityFormTest.php new file mode 100644 index 0000000..43a5773 --- /dev/null +++ b/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityFormTest.php @@ -0,0 +1,73 @@ +entityForm = new ConfigEntityForm(); + } + + /** + * @covers ::copyFormValuesToEntity + */ + public function testCopyFormValuesToEntity() { + $entity_id = 'test_config_entity_id'; + $values = ['id' => $entity_id]; + $entity_type = $this->getMockBuilder('\Drupal\Core\Config\Entity\ConfigEntityType') + ->disableOriginalConstructor() + ->getMock(); + $entity_type->expects($this->atLeastOnce()) + ->method('getConfigMapping') + ->willReturn(['regular_key' => 'regularKey']); + $entity = $this->getMockBuilder('\Drupal\Tests\Core\Config\Entity\Fixtures\ConfigEntityBaseWithPluginCollections') + ->setConstructorArgs([$values, 'test_config_entity']) + ->setMethods(['getEntityType', 'getPluginCollections']) + ->getMock(); + $entity->expects($this->atLeastOnce()) + ->method('getEntityType') + ->willReturn($entity_type); + $entity->expects($this->atLeastOnce()) + ->method('getPluginCollections') + ->willReturn(['key_controlled_by_plugin_collection' => NULL]); + $this->entityForm->setEntity($entity); + + $form_state = (new FormState())->setValues([ + 'regular_key' => 'foo', + 'key_controlled_by_plugin_collection' => 'bar', + ]); + $result = $this->entityForm->buildEntity([], $form_state); + + $this->assertSame($entity_id, $result->id()); + // The regular key should have a value, but the one controlled by a plugin + // collection should not have been set. + $this->assertSame('foo', $result->get('regularKey')); + $this->assertNull($result->get('key_controlled_by_plugin_collection')); + } + +} diff --git a/core/tests/Drupal/Tests/Core/Entity/EntityFormTest.php b/core/tests/Drupal/Tests/Core/Entity/EntityFormTest.php index ea39a9d..adfc714 100644 --- a/core/tests/Drupal/Tests/Core/Entity/EntityFormTest.php +++ b/core/tests/Drupal/Tests/Core/Entity/EntityFormTest.php @@ -8,7 +8,6 @@ namespace Drupal\Tests\Core\Entity; use Drupal\Core\Entity\EntityForm; -use Drupal\Core\Form\FormState; use Drupal\Tests\UnitTestCase; /** @@ -95,32 +94,4 @@ public function providerTestFormIds() { ); } - /** - * @covers ::copyFormValuesToEntity - */ - public function testCopyFormValuesToEntity() { - $entity_id = 'test_config_entity_id'; - $values = ['id' => $entity_id]; - $entity = $this->getMockBuilder('\Drupal\Tests\Core\Config\Entity\Fixtures\ConfigEntityBaseWithPluginCollections') - ->setConstructorArgs([$values, 'test_config_entity']) - ->setMethods(['getPluginCollections']) - ->getMock(); - $entity->expects($this->atLeastOnce()) - ->method('getPluginCollections') - ->willReturn(['key_controlled_by_plugin_collection' => NULL]); - $this->entityForm->setEntity($entity); - - $form_state = (new FormState())->setValues([ - 'regular_key' => 'foo', - 'key_controlled_by_plugin_collection' => 'bar', - ]); - $result = $this->entityForm->buildEntity([], $form_state); - - $this->assertSame($entity_id, $result->id()); - // The regular key should have a value, but the one controlled by a plugin - // collection should not have been set. - $this->assertSame('foo', $result->get('regular_key')); - $this->assertNull($result->get('key_controlled_by_plugin_collection')); - } - }