Commit 2e92d427 authored by alexpott's avatar alexpott

Issue #2030607 by hussainweb, Mile23, Kingdutch, alexpott, balagan, amateescu,...

Issue #2030607 by hussainweb, Mile23, Kingdutch, alexpott, balagan, amateescu, adci_contributor, Xano, mon_franco, cilefen: Expand EntityDisplayBase with methods
parent b3312d05
......@@ -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->setTargetBundle($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->setTargetBundle($this->id());
$form_display->save();
}
}
......
......@@ -22,10 +22,11 @@
* The new object necessarily has the same $targetEntityType and $bundle
* properties than the original one.
*
* @param $view_mode
* @param string $view_mode
* The view mode for the new object.
*
* @return static
* A duplicate of this object with the given view mode.
*/
public function createCopy($view_mode);
......@@ -91,4 +92,46 @@ public function getHighestWeight();
*/
public function getRenderer($field_name);
/**
* Returns the entity type for which this display is used.
*
* @return string
* The entity type id.
*/
public function getTargetEntityTypeId();
/**
* Returns the view or form mode to be displayed.
*
* @return string
* The mode to be displayed.
*/
public function getMode();
/**
* Returns the original view or form mode that was requested.
*
* @return string
* The original mode that was requested.
*/
public function getOriginalMode();
/**
* Returns the bundle to be displayed.
*
* @return string
* The bundle to be displayed.
*/
public function getTargetBundle();
/**
* Sets the bundle to be displayed.
*
* @param string $bundle
* The bundle to be displayed.
*
* @return $this
*/
public function setTargetBundle($bundle);
}
......@@ -36,21 +36,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.
......@@ -64,7 +64,7 @@
*
* @var string
*/
public $mode = self::CUSTOM_MODE;
protected $mode = self::CUSTOM_MODE;
/**
* Whether this display is enabled or not. If the entity (form) display
......@@ -94,7 +94,7 @@
*
* @var string
*/
public $originalMode;
protected $originalMode;
/**
* The plugin objects used for this display, keyed by field name.
......@@ -144,6 +144,91 @@ public function __construct(array $values, $entity_type) {
$this->init();
}
/**
* Initializes the display.
*
* This fills in default options for components:
* - that are not explicitly known as either "visible" or "hidden" in the
* display,
* - or that are not supposed to be configurable.
*/
protected function init() {
// Only populate defaults for "official" view modes and form modes.
if ($this->mode !== static::CUSTOM_MODE) {
// Fill in defaults for extra fields.
$context = $this->displayContext == 'view' ? 'display' : $this->displayContext;
$extra_fields = \Drupal::entityManager()->getExtraFields($this->targetEntityType, $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])) {
// Extra fields are visible by default unless they explicitly say so.
if (!isset($definition['visible']) || $definition['visible'] == TRUE) {
$this->content[$name] = array(
'weight' => $definition['weight']
);
}
else {
$this->hidden[$name] = TRUE;
}
}
}
// Fill in defaults for fields.
$fields = $this->getFieldDefinitions();
foreach ($fields as $name => $definition) {
if (!$definition->isDisplayConfigurable($this->displayContext) || (!isset($this->content[$name]) && !isset($this->hidden[$name]))) {
$options = $definition->getDisplayOptions($this->displayContext);
if (!empty($options['type']) && $options['type'] == 'hidden') {
$this->hidden[$name] = TRUE;
}
elseif ($options) {
$this->content[$name] = $this->pluginManager->prepareConfiguration($definition->getType(), $options);
}
// Note: (base) fields that do not specify display options are not
// tracked in the display at all, in order to avoid cluttering the
// configuration that gets saved back.
}
}
}
}
/**
* {@inheritdoc}
*/
public function getTargetEntityTypeId() {
return $this->targetEntityType;
}
/**
* {@inheritdoc}
*/
public function getMode() {
return $this->get('mode');
}
/**
* {@inheritdoc}
*/
public function getOriginalMode() {
return $this->get('originalMode');
}
/**
* {@inheritdoc}
*/
public function getTargetBundle() {
return $this->bundle;
}
/**
* {@inheritdoc}
*/
public function setTargetBundle($bundle) {
$this->set('bundle', $bundle);
return $this;
}
/**
* {@inheritdoc}
*/
......@@ -213,55 +298,6 @@ public function toArray() {
return $properties;
}
/**
* Initializes the display.
*
* This fills in default options for components:
* - that are not explicitly known as either "visible" or "hidden" in the
* display,
* - or that are not supposed to be configurable.
*/
protected function init() {
// Only populate defaults for "official" view modes and form modes.
if ($this->mode !== static::CUSTOM_MODE) {
// Fill in defaults for extra fields.
$context = $this->displayContext == 'view' ? 'display' : $this->displayContext;
$extra_fields = \Drupal::entityManager()->getExtraFields($this->targetEntityType, $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])) {
// Extra fields are visible by default unless they explicitly say so.
if (!isset($definition['visible']) || $definition['visible'] == TRUE) {
$this->content[$name] = array(
'weight' => $definition['weight']
);
}
else {
$this->hidden[$name] = TRUE;
}
}
}
// Fill in defaults for fields.
$fields = $this->getFieldDefinitions();
foreach ($fields as $name => $definition) {
if (!$definition->isDisplayConfigurable($this->displayContext) || (!isset($this->content[$name]) && !isset($this->hidden[$name]))) {
$options = $definition->getDisplayOptions($this->displayContext);
if (!empty($options['type']) && $options['type'] == 'hidden') {
$this->hidden[$name] = TRUE;
}
elseif ($options) {
$this->content[$name] = $this->pluginManager->prepareConfiguration($definition->getType(), $options);
}
// Note: (base) fields that do not specify display options are not
// tracked in the display at all, in order to avoid cluttering the
// configuration that gets saved back.
}
}
}
}
/**
* {@inheritdoc}
*/
......
......@@ -149,7 +149,7 @@ public function getRegionOptions() {
*/
protected function getFieldDefinitions() {
$context = $this->displayContext;
return array_filter($this->entityManager->getFieldDefinitions($this->entity->targetEntityType, $this->entity->bundle), function(FieldDefinitionInterface $field_definition) use ($context) {
return array_filter($this->entityManager->getFieldDefinitions($this->entity->getTargetEntityTypeId(), $this->entity->getTargetBundle()), function(FieldDefinitionInterface $field_definition) use ($context) {
return $field_definition->isDisplayConfigurable($context);
});
}
......@@ -164,13 +164,13 @@ public function form(array $form, FormStateInterface $form_state) {
$extra_fields = $this->getExtraFields();
$form += array(
'#entity_type' => $this->entity->targetEntityType,
'#bundle' => $this->entity->bundle,
'#entity_type' => $this->entity->getTargetEntityTypeId(),
'#bundle' => $this->entity->getTargetBundle(),
'#fields' => array_keys($field_definitions),
'#extra' => array_keys($extra_fields),
);
if (empty($field_definitions) && empty($extra_fields) && $route_info = FieldUI::getOverviewRouteInfo($this->entity->targetEntityType, $this->entity->bundle)) {
if (empty($field_definitions) && empty($extra_fields) && $route_info = FieldUI::getOverviewRouteInfo($this->entity->getTargetEntityTypeId(), $this->entity->getTargetBundle())) {
drupal_set_message($this->t('There are no fields yet added. You can add new fields on the <a href="@link">Manage fields</a> page.', array('@link' => $route_info->toString())), 'warning');
return $form;
}
......@@ -217,7 +217,7 @@ public function form(array $form, FormStateInterface $form_state) {
$form['fields'] = $table;
// Custom display settings.
if ($this->entity->mode == 'default') {
if ($this->entity->getMode() == 'default') {
// Only show the settings if there is at least one custom display mode.
if ($display_modes = $this->getDisplayModes()) {
$form['modes'] = array(
......@@ -538,7 +538,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
$form_values = $form_state->getValues();
// Handle the 'display modes' checkboxes if present.
if ($this->entity->mode == 'default' && !empty($form_values['display_modes_custom'])) {
if ($this->entity->getMode() == 'default' && !empty($form_values['display_modes_custom'])) {
$display_modes = $this->getDisplayModes();
$current_statuses = $this->getDisplayStatuses();
......@@ -548,8 +548,8 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
// If no display exists for the newly enabled view mode, initialize
// it with those from the 'default' view mode, which were used so
// far.
if (!$this->entityManager->getStorage($this->entity->getEntityTypeId())->load($this->entity->targetEntityType . '.' . $this->entity->bundle . '.' . $mode)) {
$display = $this->getEntityDisplay($this->entity->targetEntityType, $this->entity->bundle, 'default')->createCopy($mode);
if (!$this->entityManager->getStorage($this->entity->getEntityTypeId())->load($this->entity->getTargetEntityTypeId() . '.' . $this->entity->getTargetBundle() . '.' . $mode)) {
$display = $this->getEntityDisplay($this->entity->getTargetEntityTypeId(), $this->entity->getTargetBundle(), 'default')->createCopy($mode);
$display->save();
}
......@@ -854,7 +854,7 @@ public function reduceOrder($array, $a) {
*/
protected function getExtraFields() {
$context = $this->displayContext == 'view' ? 'display' : $this->displayContext;
$extra_fields = $this->entityManager->getExtraFields($this->entity->targetEntityType, $this->entity->bundle);
$extra_fields = $this->entityManager->getExtraFields($this->entity->getTargetEntityTypeId(), $this->entity->getTargetBundle());
return isset($extra_fields[$context]) ? $extra_fields[$context] : array();
}
......@@ -967,7 +967,7 @@ protected function getDisplays() {
$display_entity_type = $this->entity->getEntityTypeId();
$entity_type = $this->entityManager->getDefinition($display_entity_type);
$config_prefix = $entity_type->getConfigPrefix();
$ids = $this->configFactory()->listAll($config_prefix . '.' . $this->entity->targetEntityType . '.' . $this->entity->bundle . '.');
$ids = $this->configFactory()->listAll($config_prefix . '.' . $this->entity->getTargetEntityTypeId() . '.' . $this->entity->getTargetBundle() . '.');
foreach ($ids as $id) {
$config_id = str_replace($config_prefix . '.', '', $id);
list(,, $display_mode) = explode('.', $config_id);
......
......@@ -67,7 +67,7 @@ protected function getPlugin(FieldDefinitionInterface $field_definition, $config
if ($configuration && $configuration['type'] != 'hidden') {
$plugin = $this->pluginManager->getInstance(array(
'field_definition' => $field_definition,
'form_mode' => $this->entity->mode,
'form_mode' => $this->entity->getMode(),
'configuration' => $configuration
));
}
......@@ -86,7 +86,7 @@ protected function getDefaultPlugin($field_type) {
* {@inheritdoc}
*/
protected function getDisplayModes() {
return $this->entityManager->getFormModes($this->entity->targetEntityType);
return $this->entityManager->getFormModes($this->entity->getTargetEntityTypeId());
}
/**
......@@ -105,10 +105,10 @@ protected function getTableHeader() {
* {@inheritdoc}
*/
protected function getOverviewUrl($mode) {
$entity_type = $this->entityManager->getDefinition($this->entity->targetEntityType);
$entity_type = $this->entityManager->getDefinition($this->entity->getTargetEntityTypeId());
$field_entity_type = $entity_type->getBundleEntityType() != 'bundle'? $entity_type->getBundleEntityType() : $entity_type->id();
return Url::fromRoute('entity.entity_form_display.' . $field_entity_type . '.form_mode', [
$this->bundleEntityTypeId => $this->entity->bundle,
$this->bundleEntityTypeId => $this->entity->getTargetBundle(),
'form_mode_name' => $mode,
]);
}
......@@ -124,7 +124,7 @@ protected function thirdPartySettingsForm(PluginSettingsInterface $plugin, Field
$settings_form[$module] = $this->moduleHandler->invoke($module, 'field_widget_third_party_settings_form', array(
$plugin,
$field_definition,
$this->entity->mode,
$this->entity->getMode(),
$form,
$form_state,
));
......@@ -139,7 +139,7 @@ protected function alterSettingsSummary(array &$summary, PluginSettingsInterface
$context = array(
'widget' => $plugin,
'field_definition' => $field_definition,
'form_mode' => $this->entity->mode,
'form_mode' => $this->entity->getMode(),
);
$this->moduleHandler->alter('field_widget_settings_summary', $summary, $context);
}
......
......@@ -100,7 +100,7 @@ protected function getPlugin(FieldDefinitionInterface $field_definition, $config
if ($configuration && $configuration['type'] != 'hidden') {
$plugin = $this->pluginManager->getInstance(array(
'field_definition' => $field_definition,
'view_mode' => $this->entity->mode,
'view_mode' => $this->entity->getMode(),
'configuration' => $configuration
));
}
......@@ -119,7 +119,7 @@ protected function getDefaultPlugin($field_type) {
* {@inheritdoc}
*/
protected function getDisplayModes() {
return $this->entityManager->getViewModes($this->entity->targetEntityType);
return $this->entityManager->getViewModes($this->entity->getTargetEntityTypeId());
}
/**
......@@ -139,10 +139,10 @@ protected function getTableHeader() {
* {@inheritdoc}
*/
protected function getOverviewUrl($mode) {
$entity_type = $this->entityManager->getDefinition($this->entity->targetEntityType);
$entity_type = $this->entityManager->getDefinition($this->entity->getTargetEntityTypeId());
$field_entity_type = $entity_type->getBundleEntityType() != 'bundle'? $entity_type->getBundleEntityType() : $entity_type->id();
return Url::fromRoute('entity.entity_view_display.' . $field_entity_type . '.view_mode', [
$this->bundleEntityTypeId => $this->entity->bundle,
$this->bundleEntityTypeId => $this->entity->getTargetBundle(),
'view_mode_name' => $mode,
]);
}
......@@ -173,7 +173,7 @@ protected function thirdPartySettingsForm(PluginSettingsInterface $plugin, Field
$settings_form[$module] = $this->moduleHandler->invoke($module, 'field_formatter_third_party_settings_form', array(
$plugin,
$field_definition,
$this->entity->mode,
$this->entity->getMode(),
$form,
$form_state,
));
......@@ -188,7 +188,7 @@ protected function alterSettingsSummary(array &$summary, PluginSettingsInterface
$context = array(
'formatter' => $plugin,
'field_definition' => $field_definition,
'view_mode' => $this->entity->mode,
'view_mode' => $this->entity->getMode(),
);
$this->moduleHandler->alter('field_formatter_settings_summary', $summary, $context);
}
......
......@@ -87,15 +87,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('config' => 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->getTargetBundle(), $display->getTargetBundle());
$this->assertEqual($new_display->getMode(), 'other_view_mode');
$this->assertEqual($new_display->getComponents(), $display->getComponents());
}
......@@ -115,7 +115,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, 'settings' => array(), 'third_party_settings' => array()));
}
......@@ -287,11 +287,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->getTargetBundle());
$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->getTargetBundle());
$this->assertEqual('node.article_rename.default', $new_form_display->id());
$expected_view_dependencies = array(
'config' => array('field.field.node.article_rename.body', 'node.type.article_rename'),
......
......@@ -39,7 +39,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, 'settings' => array(), 'third_party_settings' => array()));
}
......
......@@ -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.
......
<?php
/**
* @file
* Contains \Drupal\Tests\Core\Config\Entity\EntityDisplayBaseTest.
*/
namespace Drupal\Tests\Core\Config\Entity;
use Drupal\Tests\UnitTestCase;
/**
* @coversDefaultClass \Drupal\entity\EntityDisplayBase
*
* @group Config
*/
class EntityDisplayBaseTest extends UnitTestCase {
/**
* @covers ::getTargetEntityTypeId()
*/
public function testGetTargetEntityTypeId() {
$mock = $this->getMockForAbstractClass('\Drupal\Core\Entity\EntityDisplayBase', [], '', FALSE);
$reflection = new \ReflectionProperty($mock, 'targetEntityType');
$reflection->setAccessible(TRUE);
$reflection->setValue($mock, 'test');
$this->assertEquals('test', $mock->getTargetEntityTypeId());
}
/**
* @covers ::getMode()
*/
public function testGetMode() {
$mock = $this->getMockForAbstractClass('\Drupal\Core\Entity\EntityDisplayBase', [], '', FALSE);
$reflection = new \ReflectionProperty($mock, 'mode');
$reflection->setAccessible(TRUE);
$reflection->setValue($mock, 'test');
$this->assertEquals('test', $mock->getMode());
}
/**
* @covers ::getOriginalMode()
*/
public function testGetOriginalMode() {
$mock = $this->getMockForAbstractClass('\Drupal\Core\Entity\EntityDisplayBase', [], '', FALSE);
$reflection = new \ReflectionProperty($mock, 'originalMode');
$reflection->setAccessible(TRUE);
$reflection->setValue($mock, 'test');
$this->assertEquals('test', $mock->getOriginalMode());
}
/**
* @covers ::getDisplayBundle()
*/
public function testGetDisplayBundle() {
$mock = $this->getMockForAbstractClass('\Drupal\Core\Entity\EntityDisplayBase', [], '', FALSE);
$reflection = new \ReflectionProperty($mock, 'bundle');
$reflection->setAccessible(TRUE);
$reflection->setValue($mock, 'test');
$this->assertEquals('test', $mock->getTargetBundle());
}
/**
* @covers ::setDisplayBundle()
*/
public function testSetDisplayBundle() {
$mock = $this->getMockForAbstractClass('\Drupal\Core\Entity\EntityDisplayBase', [], '', FALSE);
$reflection = new \ReflectionProperty($mock, 'bundle');
$reflection->setAccessible(TRUE);
$mock->setTargetBundle('test');
$this->assertEquals('test', $reflection->getValue($mock));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment