diff --git a/core/modules/config_translation/src/ConfigFieldMapper.php b/core/modules/config_translation/src/ConfigFieldMapper.php index e9d48e4..2b00d34 100644 --- a/core/modules/config_translation/src/ConfigFieldMapper.php +++ b/core/modules/config_translation/src/ConfigFieldMapper.php @@ -7,6 +7,8 @@ namespace Drupal\config_translation; +use Drupal\Core\Entity\EntityInterface; + /** * Configuration mapper for fields. * @@ -49,4 +51,21 @@ public function getTypeLabel() { return $this->t('@label fields', array('@label' => $base_entity_info->getLabel())); } + /** + * {@inheritdoc} + */ + public function setEntity(EntityInterface $entity) { + if (parent::setEntity($entity)) { + + // Field storage config can also contain translatable values. Add the name + // of the config as well to the list of configs for this entity. + /** @var \Drupal\field\FieldStorageConfigInterface $field_storage */ + $field_storage = $this->entity->getFieldStorageDefinition(); + $entity_type_info = $this->entityManager->getDefinition($field_storage->getEntityTypeId()); + $this->addConfigName($entity_type_info->getConfigPrefix() . '.' . $field_storage->id()); + return TRUE; + } + return FALSE; + } + } diff --git a/core/modules/config_translation/tests/src/Unit/ConfigEntityMapperTest.php b/core/modules/config_translation/tests/src/Unit/ConfigEntityMapperTest.php index 8a2fc59..14a2dbc 100644 --- a/core/modules/config_translation/tests/src/Unit/ConfigEntityMapperTest.php +++ b/core/modules/config_translation/tests/src/Unit/ConfigEntityMapperTest.php @@ -109,6 +109,10 @@ public function testSetEntity() { ->will($this->returnValue('entity_id')); $entity_type = $this->getMock('Drupal\Core\Config\Entity\ConfigEntityTypeInterface'); + $entity_type + ->expects($this->any()) + ->method('getConfigPrefix') + ->will($this->returnValue('config_prefix')); $this->entityManager ->expects($this->once()) ->method('getDefinition') @@ -118,6 +122,10 @@ public function testSetEntity() { $result = $this->configEntityMapper->setEntity($this->entity); $this->assertTrue($result); + // Ensure that the configuration name was added to the mapper. + $plugin_definition = $this->configEntityMapper->getPluginDefinition(); + $this->assertTrue(in_array('config_prefix.entity_id', $plugin_definition['names'])); + // Make sure setEntity() returns FALSE when called a second time. $result = $this->configEntityMapper->setEntity($this->entity); $this->assertFalse($result); diff --git a/core/modules/config_translation/tests/src/Unit/ConfigFieldMapperTest.php b/core/modules/config_translation/tests/src/Unit/ConfigFieldMapperTest.php new file mode 100644 index 0000000..8b054bd --- /dev/null +++ b/core/modules/config_translation/tests/src/Unit/ConfigFieldMapperTest.php @@ -0,0 +1,109 @@ +entityManager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface'); + $this->entity = $this->getMock('Drupal\field\FieldConfigInterface'); + + $definition = array( + 'class' => '\Drupal\config_translation\ConfigFieldMapper', + 'base_route_name' => 'entity.field_config.node_field_edit_form', + 'title' => '!label field', + 'names' => array(), + 'entity_type' => 'field_config', + ); + + $locale_config_manager = $this->getMockBuilder('Drupal\locale\LocaleConfigManager') + ->disableOriginalConstructor() + ->getMock(); + + $this->configFieldMapper = new ConfigFieldMapper( + 'node_fields', + $definition, + $this->getConfigFactoryStub(), + $this->getMock('Drupal\Core\Config\TypedConfigManagerInterface'), + $locale_config_manager, + $this->getMock('Drupal\config_translation\ConfigMapperManagerInterface'), + $this->getMock('Drupal\Core\Routing\RouteProviderInterface'), + $this->getStringTranslationStub(), + $this->entityManager, + $this->getMock('Drupal\Core\Language\LanguageManagerInterface') + ); + } + + /** + * Tests ConfigFieldMapper::setEntity(). + */ + public function testSetEntity() { + $entity_type = $this->getMock('Drupal\Core\Config\Entity\ConfigEntityTypeInterface'); + $entity_type + ->expects($this->any()) + ->method('getConfigPrefix') + ->will($this->returnValue('config_prefix')); + + $this->entityManager + ->expects($this->any()) + ->method('getDefinition') + ->will($this->returnValue($entity_type)); + + $field_storage = $this->getMock('Drupal\field\FieldStorageConfigInterface'); + $field_storage + ->expects($this->any()) + ->method('id') + ->will($this->returnValue('field_storage_id')); + + $this->entity + ->expects($this->any()) + ->method('getFieldStorageDefinition') + ->will($this->returnValue($field_storage)); + + $result = $this->configFieldMapper->setEntity($this->entity); + $this->assertTrue($result); + + // Ensure that the configuration name was added to the mapper. + $plugin_definition = $this->configFieldMapper->getPluginDefinition(); + $this->assertTrue(in_array('config_prefix.field_storage_id', $plugin_definition['names'])); + + // Make sure setEntity() returns FALSE when called a second time. + $result = $this->configFieldMapper->setEntity($this->entity); + $this->assertFalse($result); + } + +}