diff --git a/core/lib/Drupal/Core/Config/Entity/ConfigEntityBundleBase.php b/core/lib/Drupal/Core/Config/Entity/ConfigEntityBundleBase.php index eb1cb81..ab79140 100644 --- a/core/lib/Drupal/Core/Config/Entity/ConfigEntityBundleBase.php +++ b/core/lib/Drupal/Core/Config/Entity/ConfigEntityBundleBase.php @@ -26,9 +26,9 @@ protected function renameDisplays() { // Rename entity displays. if ($this->getOriginalId() !== $this->id()) { foreach ($this->loadDisplays('entity_view_display') as $display) { - $new_id = $this->getEntityType()->getBundleOf() . '.' . $this->id() . '.' . $display->mode; + $new_id = $this->getEntityType()->getBundleOf() . '.' . $this->id() . '.' . $display->getMode(); $display->set('id', $new_id); - $display->bundle = $this->id(); + $display->setDisplayBundle($this->id()); $display->save(); } } @@ -36,9 +36,9 @@ protected function renameDisplays() { // Rename entity form displays. if ($this->getOriginalId() !== $this->id()) { foreach ($this->loadDisplays('entity_form_display') as $form_display) { - $new_id = $this->getEntityType()->getBundleOf() . '.' . $this->id() . '.' . $form_display->mode; + $new_id = $this->getEntityType()->getBundleOf() . '.' . $this->id() . '.' . $form_display->getMode(); $form_display->set('id', $new_id); - $form_display->bundle = $this->id(); + $form_display->setDisplayBundle($this->id()); $form_display->save(); } } diff --git a/core/lib/Drupal/Core/Entity/Display/EntityDisplayInterface.php b/core/lib/Drupal/Core/Entity/Display/EntityDisplayInterface.php index d0b0a04..5cce933 100644 --- a/core/lib/Drupal/Core/Entity/Display/EntityDisplayInterface.php +++ b/core/lib/Drupal/Core/Entity/Display/EntityDisplayInterface.php @@ -90,4 +90,76 @@ public function getHighestWeight(); */ public function getRenderer($field_name); + /** + * Returns the entity type this display mode is used for. + * + * @return string + * The entity type id. + */ + 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); + + /** + * Return the view or form mode to be displayed. + * + * @return string + */ + public function getMode(); + + /** + * Sets the view or form mode to be displayed. + * + * @param string mode + * The new mode of this EntityDisplay + * + * @return $this + */ + public function setMode($mode); + + /** + * Return the original view or form mode that was requested (case of view/form modes + * being configured to fall back to the 'default' display). + * + * @return string + */ + public function getOriginalMode(); + + /** + * Set the original view or form mode that was requested (case of view/form modes + * being configured to fall back to the 'default' display). + * + * @param string original_mode + * The original mode of this EntityDisplay + * + * @return $this + */ + public function setOriginalMode($original_mode); + + /** + * Gets the bundle to be displayed. + * + * @return string + * The bundle to be displayed. + */ + public function getDisplayBundle(); + + /** + * Sets the bundle to be displayed. + * + * @param string $bundle + * The bundle to be displayed. + * + * @return $this + */ + public function setDisplayBundle($bundle); + } diff --git a/core/lib/Drupal/Core/Entity/Entity/EntityFormDisplay.php b/core/lib/Drupal/Core/Entity/Entity/EntityFormDisplay.php index 6339c18..9a667a4 100644 --- a/core/lib/Drupal/Core/Entity/Entity/EntityFormDisplay.php +++ b/core/lib/Drupal/Core/Entity/Entity/EntityFormDisplay.php @@ -184,7 +184,7 @@ public function processForm($element, FormStateInterface $form_state, $form) { } // Hide extra fields. - $extra_fields = \Drupal::entityManager()->getExtraFields($this->targetEntityType, $this->bundle); + $extra_fields = \Drupal::entityManager()->getExtraFields($this->getTargetEntityTypeId(), $this->bundle); $extra_fields = isset($extra_fields['form']) ? $extra_fields['form'] : array(); foreach ($extra_fields as $extra_field => $info) { if (!$this->getComponent($extra_field)) { diff --git a/core/lib/Drupal/Core/Entity/EntityDisplayBase.php b/core/lib/Drupal/Core/Entity/EntityDisplayBase.php index c57b3a6..7e36329 100644 --- a/core/lib/Drupal/Core/Entity/EntityDisplayBase.php +++ b/core/lib/Drupal/Core/Entity/EntityDisplayBase.php @@ -26,21 +26,21 @@ * * @var string */ - public $id; + protected $id; /** * Entity type to be displayed. * * @var string */ - public $targetEntityType; + protected $targetEntityType; /** * Bundle to be displayed. * * @var string */ - public $bundle; + protected $bundle; /** * A list of field definitions eligible for configuration in this display. @@ -54,12 +54,14 @@ * * @var string */ - public $mode; + protected $mode; /** * Whether this display is enabled or not. If the entity (form) display * is disabled, we'll fall back to the 'default' display. * + * @todo https://drupal.org/node/2286681 change to protected. + * * @var boolean */ protected $status; @@ -84,7 +86,7 @@ * * @var string */ - public $originalMode; + protected $originalMode; /** * The plugin objects used for this display, keyed by field name. @@ -138,7 +140,7 @@ public function __construct(array $values, $entity_type) { * {@inheritdoc} */ public function id() { - return $this->targetEntityType . '.' . $this->bundle . '.' . $this->mode; + return $this->getTargetEntityTypeId() . '.' . $this->bundle . '.' . $this->mode; } /** @@ -155,7 +157,7 @@ public function preSave(EntityStorageInterface $storage, $update = TRUE) { */ public function calculateDependencies() { parent::calculateDependencies(); - $target_entity_type = \Drupal::entityManager()->getDefinition($this->targetEntityType); + $target_entity_type = \Drupal::entityManager()->getDefinition($this->getTargetEntityTypeId()); $bundle_entity_type_id = $target_entity_type->getBundleEntityType(); if ($bundle_entity_type_id != 'bundle') { @@ -171,7 +173,7 @@ public function calculateDependencies() { // Create dependencies on both hidden and visible fields. $fields = $this->content + $this->hidden; foreach ($fields as $field_name => $component) { - $field = FieldConfig::loadByName($this->targetEntityType, $this->bundle, $field_name); + $field = FieldConfig::loadByName($this->getTargetEntityTypeId(), $this->bundle, $field_name); if ($field) { $this->addDependency('entity', $field->getConfigDependencyName()); } @@ -200,8 +202,8 @@ public function calculateDependencies() { */ public function postSave(EntityStorageInterface $storage, $update = TRUE) { // Reset the render cache for the target entity type. - if (\Drupal::entityManager()->hasHandler($this->targetEntityType, 'view_builder')) { - \Drupal::entityManager()->getViewBuilder($this->targetEntityType)->resetCache(); + if (\Drupal::entityManager()->hasHandler($this->getTargetEntityTypeId(), 'view_builder')) { + \Drupal::entityManager()->getViewBuilder($this->getTargetEntityTypeId())->resetCache(); } } @@ -233,7 +235,7 @@ public function toArray() { protected function init() { // Fill in defaults for extra fields. $context = $this->displayContext == 'view' ? 'display' : $this->displayContext; - $extra_fields = \Drupal::entityManager()->getExtraFields($this->targetEntityType, $this->bundle); + $extra_fields = \Drupal::entityManager()->getExtraFields($this->getTargetEntityTypeId(), $this->bundle); $extra_fields = isset($extra_fields[$context]) ? $extra_fields[$context] : array(); foreach ($extra_fields as $name => $definition) { if (!isset($this->content[$name]) && !isset($this->hidden[$name])) { @@ -338,7 +340,7 @@ public function getHighestWeight() { } // Let other modules feedback about their own additions. - $weights = array_merge($weights, \Drupal::moduleHandler()->invokeAll('field_info_max_weight', array($this->targetEntityType, $this->bundle, $this->displayContext, $this->mode))); + $weights = array_merge($weights, \Drupal::moduleHandler()->invokeAll('field_info_max_weight', array($this->getTargetEntityTypeId(), $this->bundle, $this->displayContext, $this->mode))); return $weights ? max($weights) : NULL; } @@ -357,12 +359,12 @@ protected function getFieldDefinition($field_name) { protected function getFieldDefinitions() { // Entity displays are sometimes created for non-content entities. // @todo Prevent this in https://drupal.org/node/2095195. - if (!\Drupal::entityManager()->getDefinition($this->targetEntityType)->isSubclassOf('\Drupal\Core\Entity\ContentEntityInterface')) { + if (!\Drupal::entityManager()->getDefinition($this->getTargetEntityTypeId())->isSubclassOf('\Drupal\Core\Entity\ContentEntityInterface')) { return array(); } if (!isset($this->fieldDefinitions)) { - $definitions = \Drupal::entityManager()->getFieldDefinitions($this->targetEntityType, $this->bundle); + $definitions = \Drupal::entityManager()->getFieldDefinitions($this->getTargetEntityTypeId(), $this->bundle); $this->fieldDefinitions = array_filter($definitions, array($this, 'fieldHasDisplayOptions')); } @@ -410,4 +412,64 @@ public function onDependencyRemoval(array $dependencies) { } } + /** + * {@inheritdoc} + */ + 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 getMode() { + return $this->get('mode'); + } + + /** + * {@inheritdoc} + */ + public function setMode($mode) { + $this->set('mode', $mode); + return $this; + } + + /** + * {@inheritdoc} + */ + public function getOriginalMode() { + return $this->get('originalMode'); + } + + /** + * {@inheritdoc} + */ + public function setOriginalMode($original_mode) { + $this->set('originalMode', $original_mode); + return $this; + } + + /** + * {@inheritdoc} + */ + public function getDisplayBundle() { + return $this->bundle; + } + + /** + * {@inheritdoc} + */ + public function setDisplayBundle($bundle) { + $this->set('bundle', $bundle); + return $this; + } + } diff --git a/core/modules/field_ui/src/Tests/EntityDisplayTest.php b/core/modules/field_ui/src/Tests/EntityDisplayTest.php index c2ef81c..d5e4649 100644 --- a/core/modules/field_ui/src/Tests/EntityDisplayTest.php +++ b/core/modules/field_ui/src/Tests/EntityDisplayTest.php @@ -84,15 +84,15 @@ public function testEntityDisplayCRUD() { // Check that CreateCopy() creates a new component that can be correclty // saved. - EntityViewMode::create(array('id' => $display->targetEntityType . '.other_view_mode', 'targetEntityType' => $display->targetEntityType))->save(); + EntityViewMode::create(array('id' => $display->getTargetEntityTypeId() . '.other_view_mode', 'targetEntityType' => $display->getTargetEntityTypeId()))->save(); $new_display = $display->createCopy('other_view_mode'); $new_display->save(); $new_display = entity_load('entity_view_display', $new_display->id()); $dependencies = $new_display->calculateDependencies(); $this->assertEqual(array('entity' => array('core.entity_view_mode.entity_test.other_view_mode'), 'module' => array('entity_test')), $dependencies); - $this->assertEqual($new_display->targetEntityType, $display->targetEntityType); - $this->assertEqual($new_display->bundle, $display->bundle); - $this->assertEqual($new_display->mode, 'other_view_mode'); + $this->assertEqual($new_display->getTargetEntityTypeId(), $display->getTargetEntityTypeId()); + $this->assertEqual($new_display->getDisplayBundle(), $display->getDisplayBundle()); + $this->assertEqual($new_display->getMode(), 'other_view_mode'); $this->assertEqual($new_display->getComponents(), $display->getComponents()); } @@ -112,7 +112,7 @@ public function testEntityGetDisplay() { // Check that entity_get_display() returns the correct object. $display = entity_get_display('entity_test', 'entity_test', 'default'); $this->assertFalse($display->isNew()); - $this->assertEqual($display->id, 'entity_test.entity_test.default'); + $this->assertEqual($display->id(), 'entity_test.entity_test.default'); $this->assertEqual($display->getComponent('component_1'), array('weight' => 10)); } @@ -285,11 +285,11 @@ public function testRenameDeleteBundle() { $old_form_display = entity_load('entity_form_display', 'node.article.default'); $this->assertFalse((bool) $old_form_display); $new_display = entity_load('entity_view_display', 'node.article_rename.default'); - $this->assertEqual('article_rename', $new_display->bundle); - $this->assertEqual('node.article_rename.default', $new_display->id); + $this->assertEqual('article_rename', $new_display->getDisplayBundle()); + $this->assertEqual('node.article_rename.default', $new_display->id()); $new_form_display = entity_load('entity_form_display', 'node.article_rename.default'); - $this->assertEqual('article_rename', $new_form_display->bundle); - $this->assertEqual('node.article_rename.default', $new_form_display->id); + $this->assertEqual('article_rename', $new_form_display->getDisplayBundle()); + $this->assertEqual('node.article_rename.default', $new_form_display->id()); $expected_view_dependencies = array( 'entity' => array('field.field.node.article_rename.body', 'node.type.article_rename'), diff --git a/core/modules/field_ui/src/Tests/EntityFormDisplayTest.php b/core/modules/field_ui/src/Tests/EntityFormDisplayTest.php index 12b41c7..c4574d9 100644 --- a/core/modules/field_ui/src/Tests/EntityFormDisplayTest.php +++ b/core/modules/field_ui/src/Tests/EntityFormDisplayTest.php @@ -40,7 +40,7 @@ public function testEntityGetFromDisplay() { // Check that entity_get_form_display() returns the correct object. $form_display = entity_get_form_display('entity_test', 'entity_test', 'default'); $this->assertFalse($form_display->isNew()); - $this->assertEqual($form_display->id, 'entity_test.entity_test.default'); + $this->assertEqual($form_display->id(), 'entity_test.entity_test.default'); $this->assertEqual($form_display->getComponent('component_1'), array('weight' => 10)); } diff --git a/core/modules/history/history.module b/core/modules/history/history.module index f4c9742..29215a5 100644 --- a/core/modules/history/history.module +++ b/core/modules/history/history.module @@ -134,7 +134,7 @@ function history_cron() { */ function history_node_view_alter(array &$build, EntityInterface $node, EntityViewDisplayInterface $display) { // Update the history table, stating that this user viewed this node. - if (($display->originalMode === 'full') && \Drupal::currentUser()->isAuthenticated()) { + if (($display->getOriginalMode() === 'full') && \Drupal::currentUser()->isAuthenticated()) { // When the window's "load" event is triggered, mark the node as read. // This still allows for Drupal behaviors (which are triggered on the // "DOMContentReady" event) to add "new" and "updated" indicators. diff --git a/core/tests/Drupal/Tests/Core/Config/Entity/EntityDisplayBaseTest.php b/core/tests/Drupal/Tests/Core/Config/Entity/EntityDisplayBaseTest.php new file mode 100644 index 0000000..d1dbef3 --- /dev/null +++ b/core/tests/Drupal/Tests/Core/Config/Entity/EntityDisplayBaseTest.php @@ -0,0 +1,61 @@ +getMockForAbstractClass('\Drupal\Core\Entity\EntityDisplayBase', array(), '', FALSE); + $display->setTargetEntityTypeId('test'); + $this->assertEquals('test', $display->getTargetEntityTypeId()); + } + + /** + * @covers ::setMode() + * @covers ::getMode() + */ + public function testGetMode() { + $display = $this->getMockForAbstractClass('\Drupal\Core\Entity\EntityDisplayBase', array(), '', FALSE); + $display->setMode('test'); + $this->assertEquals('test', $display->getMode()); + } + + /** + * @covers ::setOriginalMode() + * @covers ::getOriginalMode() + */ + public function testGetOriginalMode() { + $display = $this->getMockForAbstractClass('\Drupal\Core\Entity\EntityDisplayBase', array(), '', FALSE); + $display->setOriginalMode('test'); + $this->assertEquals('test', $display->getOriginalMode()); + } + + /** + * @covers ::setDisplayBundle() + * @covers ::getDisplayBundle() + */ + public function testGetDisplayBundle() { + $display = $this->getMockForAbstractClass('\Drupal\Core\Entity\EntityDisplayBase', array(), '', FALSE); + $display->setDisplayBundle('test'); + $this->assertEquals('test', $display->getDisplayBundle()); + } + + +}