diff --git a/core/lib/Drupal/Core/Entity/EntityManager.php b/core/lib/Drupal/Core/Entity/EntityManager.php index 9d749fbedd78b3a60ebc8bbe3d5bc6697563e8f9..e4e0f8daced72625a4ac06ff02103506d045c10b 100644 --- a/core/lib/Drupal/Core/Entity/EntityManager.php +++ b/core/lib/Drupal/Core/Entity/EntityManager.php @@ -1088,15 +1088,29 @@ protected function getDisplayModesByEntityType($display_type, $entity_type_id) { /** * {@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 @@ public function getFormModeOptions($entity_type, $include_disabled = FALSE) { * 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 7b180d27fc83d4083034a07178930e8e1fcdeb61..fad1092f3d931f4083657d8be29e6839733a9c30 100644 --- a/core/lib/Drupal/Core/Entity/EntityManagerInterface.php +++ b/core/lib/Drupal/Core/Entity/EntityManagerInterface.php @@ -432,26 +432,48 @@ public function getFormModes($entity_type_id); * * @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 a56df485815a29a3f3f6e3d06d7f06cc9432f8ed..266b2950297e28cc1670588ddd73288f0b81f6bb 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 @@ public function defaultConfiguration() { * 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 84f8e7f188080d300a8015ebbb1eef23a27852e6..4c76e45504bed7009c28586820239e9d12ba0b7f 100644 --- a/core/modules/block_content/src/Tests/BlockContentCreationTest.php +++ b/core/modules/block_content/src/Tests/BlockContentCreationTest.php @@ -21,12 +21,23 @@ 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. */ @@ -100,20 +111,41 @@ public function testBlockContentCreationMultipleViewModes() { '%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 39637029b3e8a284ee9c1ddce0bf707ee006a321..e9e9919308288541710ee38c8d9d0a525b00be7a 100644 --- a/core/modules/field_ui/src/Form/EntityDisplayFormBase.php +++ b/core/modules/field_ui/src/Form/EntityDisplayFormBase.php @@ -201,26 +201,23 @@ public function form(array $form, FormStateInterface $form_state) { // 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, ); } @@ -856,6 +853,14 @@ abstract protected function getDefaultPlugin($field_type); */ 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. * diff --git a/core/modules/field_ui/src/Form/EntityFormDisplayEditForm.php b/core/modules/field_ui/src/Form/EntityFormDisplayEditForm.php index 7ddd9f4a23c7e22cc8b2aa5ddecad4b8e00fe702..21699782fb178e71f134354e39aa32a349988881 100644 --- a/core/modules/field_ui/src/Form/EntityFormDisplayEditForm.php +++ b/core/modules/field_ui/src/Form/EntityFormDisplayEditForm.php @@ -73,6 +73,13 @@ protected function getDisplayModes() { return $this->entityManager->getFormModes($this->entity->getTargetEntityTypeId()); } + /** + * {@inheritdoc} + */ + protected function getDisplayModeOptions() { + return $this->entityManager->getFormModeOptions($this->entity->getTargetEntityTypeId()); + } + /** * {@inheritdoc} */ diff --git a/core/modules/field_ui/src/Form/EntityViewDisplayEditForm.php b/core/modules/field_ui/src/Form/EntityViewDisplayEditForm.php index 4c5766fa7104f543bfa44a1e9cda5404612983d8..3c38f8967f81ddec65242ebd0bd9700991f3f9a7 100644 --- a/core/modules/field_ui/src/Form/EntityViewDisplayEditForm.php +++ b/core/modules/field_ui/src/Form/EntityViewDisplayEditForm.php @@ -106,6 +106,13 @@ protected function getDisplayModes() { return $this->entityManager->getViewModes($this->entity->getTargetEntityTypeId()); } + /** + * {@inheritdoc} + */ + protected function getDisplayModeOptions() { + return $this->entityManager->getViewModeOptions($this->entity->getTargetEntityTypeId()); + } + /** * {@inheritdoc} */ diff --git a/core/modules/field_ui/src/Tests/EntityDisplayTest.php b/core/modules/field_ui/src/Tests/EntityDisplayTest.php index 75968b21ef8be154cd5a91d9889cb91691425b4e..53a8639fba68166593beca3e5915c07caa83ce58 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 @@ public function testEntityDisplayInvalidateCacheTags() { $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 453962f72e2916254c1c27818d61dfc04d15d413..a126c9e1bed35d471dc0e376d0f8d1b5cd1e4f17 100644 --- a/core/modules/node/src/Form/NodePreviewForm.php +++ b/core/modules/node/src/Form/NodePreviewForm.php @@ -86,7 +86,11 @@ public function buildForm(array $form, FormStateInterface $form_state, EntityInt '#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 @@ public function submitForm(array &$form, FormStateInterface $form_state) { )); } - /** - * 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; - } - }