diff --git a/core/lib/Drupal/Core/Entity/EntityDisplayModeBase.php b/core/lib/Drupal/Core/Entity/EntityDisplayModeBase.php index 01fefaf85a0f3a5865882cfe568382e640db82d1..bc7e7f758d5b598263e8d435ac982218c2e6e7aa 100644 --- a/core/lib/Drupal/Core/Entity/EntityDisplayModeBase.php +++ b/core/lib/Drupal/Core/Entity/EntityDisplayModeBase.php @@ -11,7 +11,7 @@ use Drupal\Core\Config\Entity\ConfigEntityInterface; /** - * Base class for config entity types that hold settings for form and view modes. + * Base class for config entity types with settings for form and view modes. */ abstract class EntityDisplayModeBase extends ConfigEntityBase implements EntityDisplayModeInterface { @@ -20,14 +20,14 @@ abstract class EntityDisplayModeBase extends ConfigEntityBase implements EntityD * * @var string */ - public $id; + protected $id; /** * The human-readable name of the form or view mode. * * @var string */ - public $label; + protected $label; /** * The entity type this form or view mode is used for. @@ -37,7 +37,7 @@ abstract class EntityDisplayModeBase extends ConfigEntityBase implements EntityD * * @var string */ - public $targetEntityType; + protected $targetEntityType; /** * Whether or not this form or view mode has custom settings by default. @@ -56,7 +56,7 @@ abstract class EntityDisplayModeBase extends ConfigEntityBase implements EntityD * * @var bool */ - public $cache = TRUE; + protected $cache = TRUE; /** * {@inheritdoc} @@ -78,6 +78,14 @@ public function getTargetType() { return $this->targetEntityType; } + /** + * {@inheritdoc} + */ + public function setTargetType($target_entity_type) { + $this->targetEntityType = $target_entity_type; + return $this; + } + /** * {@inheritdoc} */ diff --git a/core/lib/Drupal/Core/Entity/EntityDisplayModeInterface.php b/core/lib/Drupal/Core/Entity/EntityDisplayModeInterface.php index bf21c93f2da32965e764a27ca5009755591eae5c..e8ad7da60d3b8cf653d1208235e7e9ac804a2138 100644 --- a/core/lib/Drupal/Core/Entity/EntityDisplayModeInterface.php +++ b/core/lib/Drupal/Core/Entity/EntityDisplayModeInterface.php @@ -22,4 +22,14 @@ interface EntityDisplayModeInterface extends ConfigEntityInterface { */ 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 0d6f8bbc0a6b85a4d92535d1d6c3db6665189357..d6d801b7551c41bd59397fac5de391a6695933b5 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 d7864ca8225060e42c896f9bca2eb120b36d4f5d..869d6b5ed974063afd462eaa31391cec30f24dc4 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 4e62f114fc4788d5fc05b19d0cf88633417d2e76..67db1342aafa74c92230414a19ba4afda9eb3779 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)); } }