diff --git a/core/lib/Drupal/Core/Entity/EntityManager.php b/core/lib/Drupal/Core/Entity/EntityManager.php index 5e2ad03..cf5ae6b 100644 --- a/core/lib/Drupal/Core/Entity/EntityManager.php +++ b/core/lib/Drupal/Core/Entity/EntityManager.php @@ -813,7 +813,7 @@ protected function getAllDisplayModesByEntityType($display_type) { $this->displayModeInfo[$display_type] = array(); foreach ($this->getStorage($display_type)->loadMultiple() as $display_mode) { list($display_mode_entity_type, $display_mode_name) = explode('.', $display_mode->id(), 2); - $this->displayModeInfo[$display_type][$display_mode_entity_type][$display_mode_name] = (array) $display_mode; + $this->displayModeInfo[$display_type][$display_mode_entity_type][$display_mode_name] = $display_mode->toArray(); } $this->moduleHandler->alter($key, $this->displayModeInfo[$display_type]); $this->cacheBackend->set("$key:$langcode", $this->displayModeInfo[$display_type], CacheBackendInterface::CACHE_PERMANENT, array('entity_types' => TRUE, 'entity_field_info' => TRUE)); diff --git a/core/modules/entity/src/EntityDisplayModeBase.php b/core/modules/entity/src/EntityDisplayModeBase.php index 4f98c9f..fc47e7b 100644 --- a/core/modules/entity/src/EntityDisplayModeBase.php +++ b/core/modules/entity/src/EntityDisplayModeBase.php @@ -21,14 +21,14 @@ * * @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. @@ -38,7 +38,7 @@ * * @var string */ - public $targetEntityType; + protected $targetEntityType; /** * Whether or not this form or view mode has custom settings by default. @@ -48,6 +48,8 @@ * mode is enabled), but administrators can later use the Field UI to apply * custom display settings specific to the form or view mode. * + * @todo https://drupal.org/node/2286681 change to protected. + * * @var bool */ public $status = TRUE; @@ -57,7 +59,7 @@ * * @var bool */ - public $cache = TRUE; + protected $cache = TRUE; /** * {@inheritdoc} @@ -66,8 +68,8 @@ public static function sort(ConfigEntityInterface $a, ConfigEntityInterface $b) /** @var \Drupal\entity\EntityDisplayModeInterface $a */ /** @var \Drupal\entity\EntityDisplayModeInterface $b */ // Sort by the type of entity the view mode is used for. - $a_type = $a->getTargetType(); - $b_type = $b->getTargetType(); + $a_type = $a->getTargetEntityTypeId(); + $b_type = $b->getTargetEntityTypeId(); $type_order = strnatcasecmp($a_type, $b_type); return $type_order != 0 ? $type_order : parent::sort($a, $b); } @@ -75,16 +77,32 @@ public static function sort(ConfigEntityInterface $a, ConfigEntityInterface $b) /** * {@inheritdoc} */ - public function getTargetType() { + public function getTargetEntityTypeId() { return $this->targetEntityType; } /** * {@inheritdoc} */ + public function setTargetEntityTypeId($target_entity_type_id) { + $this->set('targetEntityType', $target_entity_type_id); + return $this; + } + + /** + * {@inheritdoc} + */ + public function setLabel($label) { + $this->set('label', $label); + return $this; + } + + /** + * {@inheritdoc} + */ public function calculateDependencies() { parent::calculateDependencies(); - $target_entity_type = \Drupal::entityManager()->getDefinition($this->targetEntityType); + $target_entity_type = \Drupal::entityManager()->getDefinition($this->getTargetEntityTypeId()); $this->addDependency('module', $target_entity_type->getProvider()); return $this->dependencies; } diff --git a/core/modules/entity/src/EntityDisplayModeInterface.php b/core/modules/entity/src/EntityDisplayModeInterface.php index 178df61..cf03260 100644 --- a/core/modules/entity/src/EntityDisplayModeInterface.php +++ b/core/modules/entity/src/EntityDisplayModeInterface.php @@ -20,6 +20,26 @@ * @return string * The entity type name. */ - public function getTargetType(); + public function getTargetEntityTypeId(); + + /** + * Sets the target entity type to be displayed. + * + * @param string target_entity_type_id + * The entity type to be displayed. + * + * @return $this + */ + public function setTargetEntityTypeId($target_entity_type_id); + + /** + * Sets the label. + * + * @param string $label + * The label to be set for the display mode. + * + * @return $this + */ + public function setLabel($label); } diff --git a/core/modules/entity/src/EntityDisplayModeListBuilder.php b/core/modules/entity/src/EntityDisplayModeListBuilder.php index 05c241a..87a115c 100644 --- a/core/modules/entity/src/EntityDisplayModeListBuilder.php +++ b/core/modules/entity/src/EntityDisplayModeListBuilder.php @@ -77,7 +77,7 @@ public function buildRow(EntityInterface $entity) { public function load() { $entities = array(); foreach (parent::load() as $entity) { - $entities[$entity->getTargetType()][] = $entity; + $entities[$entity->getTargetEntityTypeId()][] = $entity; } return $entities; } diff --git a/core/modules/entity/src/Form/EntityDisplayModeAddForm.php b/core/modules/entity/src/Form/EntityDisplayModeAddForm.php index 49be44f..0174779 100644 --- a/core/modules/entity/src/Form/EntityDisplayModeAddForm.php +++ b/core/modules/entity/src/Form/EntityDisplayModeAddForm.php @@ -52,7 +52,7 @@ protected function prepareEntity() { throw new NotFoundHttpException(); } - $this->entity->targetEntityType = $this->targetEntityTypeId; + $this->entity->setTargetEntityTypeId($this->targetEntityTypeId); } } diff --git a/core/modules/entity/src/Form/EntityDisplayModeFormBase.php b/core/modules/entity/src/Form/EntityDisplayModeFormBase.php index e848413..f1b41f9 100644 --- a/core/modules/entity/src/Form/EntityDisplayModeFormBase.php +++ b/core/modules/entity/src/Form/EntityDisplayModeFormBase.php @@ -85,7 +85,7 @@ public function form(array $form, array &$form_state) { '#description' => t('A unique machine-readable name. Can only contain lowercase letters, numbers, and underscores.'), '#disabled' => !$this->entity->isNew(), '#default_value' => $this->entity->id(), - '#field_prefix' => $this->entity->isNew() ? $this->entity->getTargetType() . '.' : '', + '#field_prefix' => $this->entity->isNew() ? $this->entity->getTargetEntityTypeId() . '.' : '', '#machine_name' => array( 'exists' => array($this, 'exists'), 'replace_pattern' => '[^a-z0-9_.]+', diff --git a/core/modules/entity/src/Form/EntityFormModeAddForm.php b/core/modules/entity/src/Form/EntityFormModeAddForm.php index 4cdece5..35cfe5d 100644 --- a/core/modules/entity/src/Form/EntityFormModeAddForm.php +++ b/core/modules/entity/src/Form/EntityFormModeAddForm.php @@ -23,7 +23,7 @@ protected function prepareEntity() { throw new NotFoundHttpException(); } - $this->entity->targetEntityType = $this->targetEntityTypeId; + $this->entity->setTargetEntityTypeId($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 314e044..3034e6a 100644 --- a/core/tests/Drupal/Tests/Core/Config/Entity/EntityDisplayModeBaseUnitTest.php +++ b/core/tests/Drupal/Tests/Core/Config/Entity/EntityDisplayModeBaseUnitTest.php @@ -117,4 +117,35 @@ public function testCalculateDependencies() { } + /** + * @covers ::setTargetEntityTypeId() + * @covers ::getTargetEntityTypeId() + */ + public function testGetTargetEntityTypeId() { + $display = $this->getMockForAbstractClass('\Drupal\entity\EntityDisplayModeBase', array(array(), $this->entityType)); + $display->setTargetEntityTypeId('test'); + $this->assertEquals('test', $display->getTargetEntityTypeId()); + } + + /** + * @covers ::setLabel() + * @covers ::label() + */ + public function testSetLabel() { + $this->entityManager->expects($this->once()) + ->method('getDefinition') + ->with($this->entityType) + ->will($this->returnValue($this->entityInfo)); + $this->entityInfo->expects($this->any()) + ->method('getLabelCallback') + ->will($this->returnValue(NULL)); + $this->entityInfo->expects($this->any()) + ->method('getKey') + ->with('label') + ->will($this->returnValue('label')); + $display = $this->getMockForAbstractClass('\Drupal\entity\EntityDisplayModeBase', array(array(), $this->entityType)); + $display->setLabel('test'); + $this->assertEquals('test', $display->label()); + } + }