diff --git a/core/lib/Drupal/Core/Entity/EntityDisplayModeBase.php b/core/lib/Drupal/Core/Entity/EntityDisplayModeBase.php index b141660..bc7e7f7 100644 --- a/core/lib/Drupal/Core/Entity/EntityDisplayModeBase.php +++ b/core/lib/Drupal/Core/Entity/EntityDisplayModeBase.php @@ -81,6 +81,14 @@ public function getTargetType() { /** * {@inheritdoc} */ + public function setTargetType($target_entity_type) { + $this->targetEntityType = $target_entity_type; + return $this; + } + + /** + * {@inheritdoc} + */ public function calculateDependencies() { parent::calculateDependencies(); $target_entity_type = \Drupal::entityManager()->getDefinition($this->targetEntityType); diff --git a/core/lib/Drupal/Core/Entity/EntityDisplayModeInterface.php b/core/lib/Drupal/Core/Entity/EntityDisplayModeInterface.php index bf21c93..e8ad7da 100644 --- a/core/lib/Drupal/Core/Entity/EntityDisplayModeInterface.php +++ b/core/lib/Drupal/Core/Entity/EntityDisplayModeInterface.php @@ -22,4 +22,14 @@ */ public function getTargetType(); + /** + * Set the entity type this display mode is used for. + * + * @param string $target_entity_type + * The target entity type for this display mode. + * + * @return Drupal\Core\Entity\EntityDisplayModeInterface + * The display mode object, for fluent interface. + */ + public function setTargetType($target_entity_type); } diff --git a/core/modules/field_ui/src/Form/EntityDisplayModeAddForm.php b/core/modules/field_ui/src/Form/EntityDisplayModeAddForm.php index cd207de..24b32e0 100644 --- a/core/modules/field_ui/src/Form/EntityDisplayModeAddForm.php +++ b/core/modules/field_ui/src/Form/EntityDisplayModeAddForm.php @@ -53,7 +53,7 @@ protected function prepareEntity() { throw new NotFoundHttpException(); } - $this->entity->targetEntityType = $this->targetEntityTypeId; + $this->entity->setTargetType($this->targetEntityTypeId); } } diff --git a/core/modules/field_ui/src/Form/EntityFormModeAddForm.php b/core/modules/field_ui/src/Form/EntityFormModeAddForm.php index d7864ca..869d6b5 100644 --- a/core/modules/field_ui/src/Form/EntityFormModeAddForm.php +++ b/core/modules/field_ui/src/Form/EntityFormModeAddForm.php @@ -23,7 +23,7 @@ protected function prepareEntity() { throw new NotFoundHttpException(); } - $this->entity->targetEntityType = $this->targetEntityTypeId; + $this->entity->setTargetType($this->targetEntityTypeId); } } diff --git a/core/tests/Drupal/Tests/Core/Config/Entity/EntityDisplayModeBaseUnitTest.php b/core/tests/Drupal/Tests/Core/Config/Entity/EntityDisplayModeBaseUnitTest.php index 4e62f11..67db134 100644 --- a/core/tests/Drupal/Tests/Core/Config/Entity/EntityDisplayModeBaseUnitTest.php +++ b/core/tests/Drupal/Tests/Core/Config/Entity/EntityDisplayModeBaseUnitTest.php @@ -70,7 +70,6 @@ protected function setUp() { $container->set('entity.manager', $this->entityManager); $container->set('uuid', $this->uuid); \Drupal::setContainer($container); - } /** @@ -101,7 +100,62 @@ public function testCalculateDependencies() { $dependencies = $this->entity->calculateDependencies(); $this->assertContains('test_module', $dependencies['module']); + } + + /** + * @covers ::setTargetType + */ + public function testSetTargetType() { + // Generate mock. + $mock = $this->getMock( + 'Drupal\Core\Entity\EntityDisplayModeBase', + NULL, + array(array('something' => 'nothing'), 'test_type') + ); + + // Some test values. + $bad_target = 'uninitialized'; + $target = 'test_target_type'; + + // Gain access to the protected property. + $property = new \ReflectionProperty($mock, 'targetEntityType'); + $property->setAccessible(TRUE); + // Set the property to a known state. + $property->setValue($mock, $bad_target); + + // Set the target type. + $mock->setTargetType($target); + + // Test the outcome. + $this->assertNotEquals($bad_target, $property->getValue($mock)); + $this->assertEquals($target, $property->getValue($mock)); + } + /** + * @covers ::getTargetType + */ + public function testGetTargetType() { + // Generate mock. + $mock = $this->getMock( + 'Drupal\Core\Entity\EntityDisplayModeBase', + NULL, + array(array('something' => 'nothing'), 'test_type') + ); + + // A test value. + $target = 'test_target_type'; + + // Gain access to the protected property. + $property = new \ReflectionProperty($mock, 'targetEntityType'); + $property->setAccessible(TRUE); + // Set the property to a known state. + $property->setValue($mock, $target); + + // Get the target type. + $value = $mock->getTargetType($target); + + // Test the outcome. + $this->assertEquals($value, $property->getValue($mock)); } }