summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Pott2015-09-26 12:03:49 (GMT)
committerAlex Pott2015-09-26 12:03:49 (GMT)
commit64d6415c475ee4fd1b55de48e4a1b316cb7a0861 (patch)
tree5d4857a8c0a620191b6f5eb601a870bcd4b42c2a
parent068f6ad739ba56ef69dfbe52dd9019ed068ce7c1 (diff)
Issue #2322503 by swentel, plopesc, Nick_vh, nlisgo, kerby70, epari.siva, mrjmd, yched, axe312, olli: getDisplayModeOptions() returns only full or teaser regardless of the status of the entity display
-rw-r--r--core/lib/Drupal/Core/Entity/EntityManager.php68
-rw-r--r--core/lib/Drupal/Core/Entity/EntityManagerInterface.php34
-rw-r--r--core/modules/block_content/src/Plugin/Block/BlockContentBlock.php5
-rw-r--r--core/modules/block_content/src/Tests/BlockContentCreationTest.php42
-rw-r--r--core/modules/field_ui/src/Form/EntityDisplayFormBase.php27
-rw-r--r--core/modules/field_ui/src/Form/EntityFormDisplayEditForm.php7
-rw-r--r--core/modules/field_ui/src/Form/EntityViewDisplayEditForm.php7
-rw-r--r--core/modules/field_ui/src/Tests/EntityDisplayTest.php51
-rw-r--r--core/modules/node/src/Form/NodePreviewForm.php48
9 files changed, 213 insertions, 76 deletions
diff --git a/core/lib/Drupal/Core/Entity/EntityManager.php b/core/lib/Drupal/Core/Entity/EntityManager.php
index 9d749fb..e4e0f8d 100644
--- a/core/lib/Drupal/Core/Entity/EntityManager.php
+++ b/core/lib/Drupal/Core/Entity/EntityManager.php
@@ -1088,15 +1088,29 @@ class EntityManager extends DefaultPluginManager implements EntityManagerInterfa
/**
* {@inheritdoc}
*/
- public function getViewModeOptions($entity_type, $include_disabled = FALSE) {
- return $this->getDisplayModeOptions('view_mode', $entity_type, $include_disabled);
+ public function getViewModeOptions($entity_type_id) {
+ return $this->getDisplayModeOptions('view_mode', $entity_type_id);
}
/**
* {@inheritdoc}
*/
- public function getFormModeOptions($entity_type, $include_disabled = FALSE) {
- return $this->getDisplayModeOptions('form_mode', $entity_type, $include_disabled);
+ public function getFormModeOptions($entity_type_id) {
+ return $this->getDisplayModeOptions('form_mode', $entity_type_id);
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function getViewModeOptionsByBundle($entity_type_id, $bundle) {
+ return $this->getDisplayModeOptionsByBundle('view_mode', $entity_type_id, $bundle);
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function getFormModeOptionsByBundle($entity_type_id, $bundle) {
+ return $this->getDisplayModeOptionsByBundle('form_mode', $entity_type_id, $bundle);
}
/**
@@ -1106,19 +1120,55 @@ class EntityManager extends DefaultPluginManager implements EntityManagerInterfa
* The display type to be retrieved. It can be "view_mode" or "form_mode".
* @param string $entity_type_id
* The entity type whose display mode options should be returned.
- * @param bool $include_disabled
- * Force to include disabled display modes. Defaults to FALSE.
*
* @return array
* An array of display mode labels, keyed by the display mode ID.
*/
- protected function getDisplayModeOptions($display_type, $entity_type_id, $include_disabled = FALSE) {
+ protected function getDisplayModeOptions($display_type, $entity_type_id) {
$options = array('default' => t('Default'));
foreach ($this->getDisplayModesByEntityType($display_type, $entity_type_id) as $mode => $settings) {
- if (!empty($settings['status']) || $include_disabled) {
- $options[$mode] = $settings['label'];
+ $options[$mode] = $settings['label'];
+ }
+ return $options;
+ }
+
+ /**
+ * Returns an array of display mode options by bundle.
+ *
+ * @param $display_type
+ * The display type to be retrieved. It can be "view_mode" or "form_mode".
+ * @param string $entity_type_id
+ * The entity type whose display mode options should be returned.
+ * @param string $bundle
+ * The name of the bundle.
+ *
+ * @return array
+ * An array of display mode labels, keyed by the display mode ID.
+ */
+ protected function getDisplayModeOptionsByBundle($display_type, $entity_type_id, $bundle) {
+ // Collect all the entity's display modes.
+ $options = $this->getDisplayModeOptions($display_type, $entity_type_id);
+
+ // Filter out modes for which the entity display is disabled
+ // (or non-existent).
+ $load_ids = array();
+ // Get the list of available entity displays for the current bundle.
+ foreach (array_keys($options) as $mode) {
+ $load_ids[] = $entity_type_id . '.' . $bundle . '.' . $mode;
+ }
+
+ // Load the corresponding displays.
+ $displays = $this->getStorage($display_type == 'form_mode' ? 'entity_form_display' : 'entity_view_display')
+ ->loadMultiple($load_ids);
+
+ // Unset the display modes that are not active or do not exist.
+ foreach (array_keys($options) as $mode) {
+ $display_id = $entity_type_id . '.' . $bundle . '.' . $mode;
+ if (!isset($displays[$display_id]) || !$displays[$display_id]->status()) {
+ unset($options[$mode]);
}
}
+
return $options;
}
diff --git a/core/lib/Drupal/Core/Entity/EntityManagerInterface.php b/core/lib/Drupal/Core/Entity/EntityManagerInterface.php
index 7b180d2..fad1092 100644
--- a/core/lib/Drupal/Core/Entity/EntityManagerInterface.php
+++ b/core/lib/Drupal/Core/Entity/EntityManagerInterface.php
@@ -432,26 +432,48 @@ interface EntityManagerInterface extends PluginManagerInterface, EntityTypeListe
*
* @param string $entity_type_id
* The entity type whose view mode options should be returned.
- * @param bool $include_disabled
- * Force to include disabled view modes. Defaults to FALSE.
*
* @return array
* An array of view mode labels, keyed by the display mode ID.
*/
- public function getViewModeOptions($entity_type_id, $include_disabled = FALSE);
+ public function getViewModeOptions($entity_type_id);
/**
* Gets an array of form mode options.
*
* @param string $entity_type_id
* The entity type whose form mode options should be returned.
- * @param bool $include_disabled
- * Force to include disabled form modes. Defaults to FALSE.
*
* @return array
* An array of form mode labels, keyed by the display mode ID.
*/
- public function getFormModeOptions($entity_type_id, $include_disabled = FALSE);
+ public function getFormModeOptions($entity_type_id);
+
+ /**
+ * Returns an array of view mode options by bundle.
+ *
+ * @param string $entity_type_id
+ * The entity type whose view mode options should be returned.
+ * @param string $bundle
+ * The name of the bundle.
+ *
+ * @return array
+ * An array of view mode labels, keyed by the display mode ID.
+ */
+ public function getViewModeOptionsByBundle($entity_type_id, $bundle);
+
+ /**
+ * Returns an array of form mode options by bundle.
+ *
+ * @param string $entity_type_id
+ * The entity type whose form mode options should be returned.
+ * @param string $bundle
+ * The name of the bundle.
+ *
+ * @return array
+ * An array of form mode labels, keyed by the display mode ID.
+ */
+ public function getFormModeOptionsByBundle($entity_type_id, $bundle);
/**
* Loads an entity by UUID.
diff --git a/core/modules/block_content/src/Plugin/Block/BlockContentBlock.php b/core/modules/block_content/src/Plugin/Block/BlockContentBlock.php
index a56df48..266b295 100644
--- a/core/modules/block_content/src/Plugin/Block/BlockContentBlock.php
+++ b/core/modules/block_content/src/Plugin/Block/BlockContentBlock.php
@@ -115,7 +115,10 @@ class BlockContentBlock extends BlockBase implements ContainerFactoryPluginInter
* Adds body and description fields to the block configuration form.
*/
public function blockForm($form, FormStateInterface $form_state) {
- $options = $this->entityManager->getViewModeOptions('block_content');
+ $uuid = $this->getDerivativeId();
+ $block = $this->entityManager->loadEntityByUuid('block_content', $uuid);
+ $options = $this->entityManager->getViewModeOptionsByBundle('block_content', $block->bundle());
+
$form['view_mode'] = array(
'#type' => 'select',
'#options' => $options,
diff --git a/core/modules/block_content/src/Tests/BlockContentCreationTest.php b/core/modules/block_content/src/Tests/BlockContentCreationTest.php
index 84f8e7f..4c76e45 100644
--- a/core/modules/block_content/src/Tests/BlockContentCreationTest.php
+++ b/core/modules/block_content/src/Tests/BlockContentCreationTest.php
@@ -21,13 +21,24 @@ class BlockContentCreationTest extends BlockContentTestBase {
/**
* Modules to enable.
*
- * Enable dummy module that implements hook_block_insert() for exceptions.
+ * Enable dummy module that implements hook_block_insert() for exceptions and
+ * field_ui to edit display settings.
*
* @var array
*/
public static $modules = array('block_content_test', 'dblog', 'field_ui');
/**
+ * Permissions to grant admin user.
+ *
+ * @var array
+ */
+ protected $permissions = array(
+ 'administer blocks',
+ 'administer block_content display'
+ );
+
+ /**
* Sets the test up.
*/
protected function setUp() {
@@ -100,20 +111,41 @@ class BlockContentCreationTest extends BlockContentTestBase {
'%name' => $edit['info[0][value]']
)), 'Basic block created.');
+ // Save our block permanently
+ $this->drupalPostForm(NULL, NULL, t('Save block'));
+
+ // Set test_view_mode as a custom display to be available on the list.
+ $this->drupalGet('admin/structure/block/block-content');
+ $this->drupalGet('admin/structure/block/block-content/types');
+ $this->clickLink(t('Manage display'));
+ $this->drupalGet('admin/structure/block/block-content/manage/basic/display');
+ $custom_view_mode = array(
+ 'display_modes_custom[test_view_mode]' => 1,
+ );
+ $this->drupalPostForm(NULL, $custom_view_mode, t('Save'));
+
+ // Go to the configure page and change the view mode.
+ $this->drupalGet('admin/structure/block/manage/testblock');
+
+ // Test the available view mode options.
+ $this->assertOption('edit-settings-view-mode', 'default', 'The default view mode is available.');
+ $this->assertOption('edit-settings-view-mode', 'test_view_mode', 'The test view mode is available.');
+
+ $view_mode['settings[view_mode]'] = 'test_view_mode';
+ $this->drupalPostForm(NULL, $view_mode, t('Save block'));
+
// Check that the view mode setting is shown because more than one exists.
+ $this->drupalGet('admin/structure/block/manage/testblock');
$this->assertFieldByXPath('//select[@name="settings[view_mode]"]', NULL, 'View mode setting shown because multiple exist');
// Change the view mode.
$view_mode['settings[view_mode]'] = 'test_view_mode';
$this->drupalPostForm(NULL, $view_mode, t('Save block'));
- // Go to the configure page and verify that the new view mode is correct.
+ // Go to the configure page and verify the view mode has changed.
$this->drupalGet('admin/structure/block/manage/testblock');
$this->assertFieldByXPath('//select[@name="settings[view_mode]"]/option[@selected="selected"]/@value', 'test_view_mode', 'View mode changed to Test View Mode');
- // Test the available view mode options.
- $this->assertOption('edit-settings-view-mode', 'default', 'The default view mode is available.');
-
// Check that the block exists in the database.
$blocks = entity_load_multiple_by_properties('block_content', array('info' => $edit['info[0][value]']));
$block = reset($blocks);
diff --git a/core/modules/field_ui/src/Form/EntityDisplayFormBase.php b/core/modules/field_ui/src/Form/EntityDisplayFormBase.php
index 3963702..e9e9919 100644
--- a/core/modules/field_ui/src/Form/EntityDisplayFormBase.php
+++ b/core/modules/field_ui/src/Form/EntityDisplayFormBase.php
@@ -201,26 +201,23 @@ abstract class EntityDisplayFormBase extends EntityForm {
// Custom display settings.
if ($this->entity->getMode() == 'default') {
// Only show the settings if there is at least one custom display mode.
- if ($display_modes = $this->getDisplayModes()) {
+ $display_mode_options = $this->getDisplayModeOptions();
+ // Unset default option.
+ unset($display_mode_options['default']);
+ if ($display_mode_options) {
$form['modes'] = array(
'#type' => 'details',
'#title' => $this->t('Custom display settings'),
);
- // Collect options and default values for the 'Custom display settings'
- // checkboxes.
- $options = array();
+ // Prepare default values for the 'Custom display settings' checkboxes.
$default = array();
- $display_statuses = $this->getDisplayStatuses();
- foreach ($display_modes as $mode_name => $mode_info) {
- $options[$mode_name] = $mode_info['label'];
- if (!empty($display_statuses[$mode_name])) {
- $default[] = $mode_name;
- }
+ if ($display_statuses = array_filter($this->getDisplayStatuses())) {
+ $default = array_keys(array_intersect_key($display_mode_options, $display_statuses));
}
$form['modes']['display_modes_custom'] = array(
'#type' => 'checkboxes',
'#title' => $this->t('Use custom display settings for the following modes'),
- '#options' => $options,
+ '#options' => $display_mode_options,
'#default_value' => $default,
);
}
@@ -857,6 +854,14 @@ abstract class EntityDisplayFormBase extends EntityForm {
abstract protected function getDisplayModes();
/**
+ * Returns an array of form or view mode options.
+ *
+ * @return array
+ * An array of form or view mode options.
+ */
+ abstract protected function getDisplayModeOptions();
+
+ /**
* Returns the region to which a row in the display overview belongs.
*
* @param array $row
diff --git a/core/modules/field_ui/src/Form/EntityFormDisplayEditForm.php b/core/modules/field_ui/src/Form/EntityFormDisplayEditForm.php
index 7ddd9f4..2169978 100644
--- a/core/modules/field_ui/src/Form/EntityFormDisplayEditForm.php
+++ b/core/modules/field_ui/src/Form/EntityFormDisplayEditForm.php
@@ -76,6 +76,13 @@ class EntityFormDisplayEditForm extends EntityDisplayFormBase {
/**
* {@inheritdoc}
*/
+ protected function getDisplayModeOptions() {
+ return $this->entityManager->getFormModeOptions($this->entity->getTargetEntityTypeId());
+ }
+
+ /**
+ * {@inheritdoc}
+ */
protected function getTableHeader() {
return array(
$this->t('Field'),
diff --git a/core/modules/field_ui/src/Form/EntityViewDisplayEditForm.php b/core/modules/field_ui/src/Form/EntityViewDisplayEditForm.php
index 4c5766f..3c38f89 100644
--- a/core/modules/field_ui/src/Form/EntityViewDisplayEditForm.php
+++ b/core/modules/field_ui/src/Form/EntityViewDisplayEditForm.php
@@ -109,6 +109,13 @@ class EntityViewDisplayEditForm extends EntityDisplayFormBase {
/**
* {@inheritdoc}
*/
+ protected function getDisplayModeOptions() {
+ return $this->entityManager->getViewModeOptions($this->entity->getTargetEntityTypeId());
+ }
+
+ /**
+ * {@inheritdoc}
+ */
protected function getTableHeader() {
return array(
$this->t('Field'),
diff --git a/core/modules/field_ui/src/Tests/EntityDisplayTest.php b/core/modules/field_ui/src/Tests/EntityDisplayTest.php
index 75968b2..53a8639 100644
--- a/core/modules/field_ui/src/Tests/EntityDisplayTest.php
+++ b/core/modules/field_ui/src/Tests/EntityDisplayTest.php
@@ -8,6 +8,7 @@
namespace Drupal\field_ui\Tests;
use Drupal\Core\Cache\Cache;
+use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\Core\Entity\Entity\EntityViewMode;
use Drupal\field\Entity\FieldConfig;
@@ -33,7 +34,8 @@ class EntityDisplayTest extends KernelTestBase {
protected function setUp() {
parent::setUp();
$this->installEntitySchema('node');
- $this->installConfig(array('field', 'node'));
+ $this->installEntitySchema('user');
+ $this->installConfig(array('field', 'node', 'user'));
}
/**
@@ -454,4 +456,51 @@ class EntityDisplayTest extends KernelTestBase {
$this->assertFalse($cache->get('cid'));
}
+ /**
+ * Test getDisplayModeOptions().
+ */
+ public function testGetDisplayModeOptions() {
+ NodeType::create(array('type' => 'article'))->save();
+
+ EntityViewDisplay::create(array(
+ 'targetEntityType' => 'node',
+ 'bundle' => 'article',
+ 'mode' => 'default',
+ ))->setStatus(TRUE)->save();
+
+ $display_teaser = EntityViewDisplay::create(array(
+ 'targetEntityType' => 'node',
+ 'bundle' => 'article',
+ 'mode' => 'teaser',
+ ));
+ $display_teaser->save();
+
+ EntityFormDisplay::create(array(
+ 'targetEntityType' => 'user',
+ 'bundle' => 'user',
+ 'mode' => 'default',
+ ))->setStatus(TRUE)->save();
+
+ $form_display_teaser = EntityFormDisplay::create(array(
+ 'targetEntityType' => 'user',
+ 'bundle' => 'user',
+ 'mode' => 'register',
+ ));
+ $form_display_teaser->save();
+
+ // Test getViewModeOptionsByBundle().
+ $view_modes = \Drupal::entityManager()->getViewModeOptionsByBundle('node', 'article');
+ $this->assertEqual($view_modes, array('default' => 'Default'));
+ $display_teaser->setStatus(TRUE)->save();
+ $view_modes = \Drupal::entityManager()->getViewModeOptionsByBundle('node', 'article');
+ $this->assertEqual($view_modes, array('default' => 'Default', 'teaser' => 'Teaser'));
+
+ // Test getFormModeOptionsByBundle().
+ $form_modes = \Drupal::entityManager()->getFormModeOptionsByBundle('user', 'user');
+ $this->assertEqual($form_modes, array('default' => 'Default'));
+ $form_display_teaser->setStatus(TRUE)->save();
+ $form_modes = \Drupal::entityManager()->getFormModeOptionsByBundle('user', 'user');
+ $this->assertEqual($form_modes, array('default' => 'Default', 'register' => 'Register'));
+ }
+
}
diff --git a/core/modules/node/src/Form/NodePreviewForm.php b/core/modules/node/src/Form/NodePreviewForm.php
index 453962f..a126c9e 100644
--- a/core/modules/node/src/Form/NodePreviewForm.php
+++ b/core/modules/node/src/Form/NodePreviewForm.php
@@ -86,7 +86,11 @@ class NodePreviewForm extends FormBase implements ContainerInjectionInterface {
'#options' => array('attributes' => array('class' => array('node-preview-backlink'))) + $query_options,
);
- $view_mode_options = $this->getViewModeOptions($node);
+ $view_mode_options = $this->entityManager->getViewModeOptionsByBundle('node', $node->bundle());
+
+ // Unset view modes that are not used in the front end.
+ unset($view_mode_options['rss']);
+ unset($view_mode_options['search_index']);
$form['uuid'] = array(
'#type' => 'value',
@@ -124,46 +128,4 @@ class NodePreviewForm extends FormBase implements ContainerInjectionInterface {
));
}
- /**
- * Gets the list of available view modes for the current node.
- *
- * @param EntityInterface $node
- * The node being previewed.
- *
- * @return array
- * List of available view modes for the current node.
- */
- protected function getViewModeOptions(EntityInterface $node) {
- $load_ids = array();
- $view_mode_options = array();
-
- // Load all the node's view modes.
- $view_modes = $this->entityManager->getViewModes('node');
-
- // Get the list of available view modes for the current node's bundle.
- $ids = $this->configFactory->listAll('core.entity_view_display.node.' . $node->bundle());
- foreach ($ids as $id) {
- $config_id = str_replace('core.entity_view_display' . '.', '', $id);
- $load_ids[] = $config_id;
- }
- $displays = entity_load_multiple('entity_view_display', $load_ids);
-
- // Generate the display options array.
- foreach ($displays as $display) {
-
- $view_mode_name = $display->get('mode');
-
- // Skip view modes that are not used in the front end.
- if (in_array($view_mode_name, array('rss', 'search_index'))) {
- continue;
- }
-
- if ($display->status()) {
- $view_mode_options[$view_mode_name] = ($view_mode_name == 'default') ? t('Default') : $view_modes[$view_mode_name]['label'];
- }
- }
-
- return $view_mode_options;
- }
-
}