summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Pott2014-11-24 11:58:18 (GMT)
committerAlex Pott2014-11-24 11:58:18 (GMT)
commit3827a4815cfa8dbb39997be0148512cd7f0bd364 (patch)
tree880b69bef17cf9dc9dcedbc5c2f2110d4596cd83
parentf5d180e1919f9802c66e8192098d54c97f8cf6e2 (diff)
Issue #2030613 by balagan, Mile23, scottrigby, alexpott: Expand EntityViewMode (really EntityDisplayModeBase) with methods
-rw-r--r--core/lib/Drupal/Core/Entity/EntityDisplayModeBase.php18
-rw-r--r--core/lib/Drupal/Core/Entity/EntityDisplayModeInterface.php10
-rw-r--r--core/modules/field_ui/src/Form/EntityDisplayModeAddForm.php2
-rw-r--r--core/modules/field_ui/src/Form/EntityFormModeAddForm.php2
-rw-r--r--core/tests/Drupal/Tests/Core/Config/Entity/EntityDisplayModeBaseUnitTest.php56
5 files changed, 80 insertions, 8 deletions
diff --git a/core/lib/Drupal/Core/Entity/EntityDisplayModeBase.php b/core/lib/Drupal/Core/Entity/EntityDisplayModeBase.php
index 01fefaf..bc7e7f7 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\ConfigEntityBase;
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}
@@ -81,6 +81,14 @@ abstract class EntityDisplayModeBase extends ConfigEntityBase implements EntityD
/**
* {@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 @@ 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 0d6f8bb..d6d801b 100644
--- a/core/modules/field_ui/src/Form/EntityDisplayModeAddForm.php
+++ b/core/modules/field_ui/src/Form/EntityDisplayModeAddForm.php
@@ -53,7 +53,7 @@ class EntityDisplayModeAddForm extends EntityDisplayModeFormBase {
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 @@ class EntityFormModeAddForm extends EntityDisplayModeAddForm {
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 @@ class EntityDisplayModeBaseUnitTest extends UnitTestCase {
$container->set('entity.manager', $this->entityManager);
$container->set('uuid', $this->uuid);
\Drupal::setContainer($container);
-
}
/**
@@ -101,7 +100,62 @@ class EntityDisplayModeBaseUnitTest extends UnitTestCase {
$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));
}
}