Commit 64d6415c authored by alexpott's avatar alexpott

Issue #2322503 by swentel, plopesc, Nick_vh, nlisgo, kerby70, epari.siva,...

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
parent 068f6ad7
......@@ -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;
}
......
......@@ -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.
......
......@@ -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,
......
......@@ -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);
......
......@@ -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 @@ protected function getPluginOptions(FieldDefinitionInterface $field_definition)
*/
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.
*
......
......@@ -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}
*/
......
......@@ -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}
*/
......
......@@ -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'));
}
}
......@@ -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;
}
}
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