Commit 09b7d821 authored by catch's avatar catch
Browse files

Issue #2878211 by mfernea, bnjmnm, Lendude, Anchal_gupta, smustgrave, lauriii:...

Issue #2878211 by mfernea, bnjmnm, Lendude, Anchal_gupta, smustgrave, lauriii: Order local tasks for view modes and form modes on name rather than machine name
parent dc6c95f6
Loading
Loading
Loading
Loading
+12 −6
Original line number Diff line number Diff line
@@ -135,8 +135,11 @@ public function getDerivativeDefinitions($base_plugin_definition) {
        ];

        // One local task for each form mode.
        $weight = 0;
        foreach ($this->entityDisplayRepository->getFormModes($entity_type_id) as $form_mode => $form_mode_info) {
        $form_modes = $this->entityDisplayRepository->getFormModes($entity_type_id);
        // Sort all form modes by title.
        $form_modes_titles = array_values(array_map(fn($item) => (string) $item['label'], $form_modes));
        sort($form_modes_titles, SORT_NATURAL);
        foreach ($form_modes as $form_mode => $form_mode_info) {
          $this->derivatives['field_form_display_' . $form_mode . '_' . $entity_type_id] = [
            'title' => $form_mode_info['label'],
            'route_name' => "entity.entity_form_display.$entity_type_id.form_mode",
@@ -144,14 +147,17 @@ public function getDerivativeDefinitions($base_plugin_definition) {
              'form_mode_name' => $form_mode,
            ],
            'parent_id' => "field_ui.fields:form_display_overview_$entity_type_id",
            'weight' => $weight++,
            'weight' => array_flip($form_modes_titles)[(string) $form_mode_info['label']],
            'cache_tags' => $this->entityTypeManager->getDefinition('entity_form_display')->getListCacheTags(),
          ];
        }

        // One local task for each view mode.
        $weight = 0;
        foreach ($this->entityDisplayRepository->getViewModes($entity_type_id) as $view_mode => $form_mode_info) {
        $view_modes = $this->entityDisplayRepository->getViewModes($entity_type_id);
        // Sort all view modes by title.
        $view_modes_titles = array_values(array_map(fn($item) => (string) $item['label'], $view_modes));
        sort($view_modes_titles, SORT_NATURAL);
        foreach ($view_modes as $view_mode => $form_mode_info) {
          $this->derivatives['field_display_' . $view_mode . '_' . $entity_type_id] = [
            'title' => $form_mode_info['label'],
            'route_name' => "entity.entity_view_display.$entity_type_id.view_mode",
@@ -159,7 +165,7 @@ public function getDerivativeDefinitions($base_plugin_definition) {
              'view_mode_name' => $view_mode,
            ],
            'parent_id' => "field_ui.fields:display_overview_$entity_type_id",
            'weight' => $weight++,
            'weight' => array_flip($view_modes_titles)[(string) $form_mode_info['label']],
            'cache_tags' => $this->entityTypeManager->getDefinition('entity_view_display')->getListCacheTags(),
          ];
        }
+84 −0
Original line number Diff line number Diff line
@@ -2,6 +2,8 @@

namespace Drupal\Tests\field_ui\Functional;

use Behat\Mink\Exception\ExpectationException;
use Drupal\Core\Entity\Entity\EntityFormMode;
use Drupal\Core\Url;
use Behat\Mink\Element\NodeElement;
use Drupal\Core\Entity\EntityInterface;
@@ -62,6 +64,7 @@ protected function setUp(): void {
    $admin_user = $this->drupalCreateUser([
      'access content',
      'administer content types',
      'administer display modes',
      'administer node fields',
      'administer node form display',
      'administer node display',
@@ -225,6 +228,57 @@ public function testNoFieldsDisplayOverview() {
    $this->assertSession()->linkByHrefExists(Url::fromRoute('entity.node.field_ui_fields', ['node_type' => 'no_fields'])->toString());
  }

  /**
   * Tests if display mode local tasks appear in alphabetical order by label.
   */
  public function testViewModeLocalTasksOrder() {
    $manage_display = 'admin/structure/types/manage/' . $this->type . '/display';

    // Specify the 'rss' mode, check that the field is displayed the same.
    $edit = [
      'display_modes_custom[rss]' => TRUE,
      'display_modes_custom[teaser]' => TRUE,
    ];
    $this->drupalGet($manage_display);
    $this->submitForm($edit, 'Save');

    $this->assertOrderInPage(['RSS', 'Teaser']);

    $edit = [
      'label' => 'Breezer',
    ];
    $this->drupalGet('admin/structure/display-modes/view/manage/node.teaser');
    $this->submitForm($edit, 'Save');

    $this->assertOrderInPage(['Breezer', 'RSS']);
  }

  /**
   * Tests if form mode local tasks appear in alphabetical order by label.
   */
  public function testFormModeLocalTasksOrder() {
    EntityFormMode::create([
      'id' => 'node.big',
      'label' => 'Big Form',
      'targetEntityType' => 'node',
    ])->save();
    EntityFormMode::create([
      'id' => 'node.little',
      'label' => 'Little Form',
      'targetEntityType' => 'node',
    ])->save();
    $manage_form = 'admin/structure/types/manage/' . $this->type . '/form-display';
    $this->drupalGet($manage_form);
    $this->assertOrderInPage(['Big Form', 'Little Form']);
    $edit = [
      'label' => 'Ultimate Form',
    ];
    $this->drupalGet('admin/structure/display-modes/form/manage/node.big');
    $this->submitForm($edit, 'Save');
    $this->drupalGet($manage_form);
    $this->assertOrderInPage(['Little Form', 'Ultimate Form']);
  }

  /**
   * Asserts that a string is found in the rendered node in a view mode.
   *
@@ -351,4 +405,34 @@ protected function getAllOptionsList(NodeElement $element) {
    return $options;
  }

  /**
   * Asserts that several pieces of markup are in a given order in the page.
   *
   * @param string[] $items
   *   An ordered list of strings.
   *
   * @throws \Behat\Mink\Exception\ExpectationException
   *   When any of the given string is not found.
   *
   * @internal
   *
   * @todo Remove this once https://www.drupal.org/node/2817657 is committed.
   */
  protected function assertOrderInPage(array $items): void {
    $session = $this->getSession();
    $text = $session->getPage()->getHtml();
    $strings = [];
    foreach ($items as $item) {
      if (($pos = strpos($text, $item)) === FALSE) {
        throw new ExpectationException("Cannot find '$item' in the page", $session->getDriver());
      }
      $strings[$pos] = $item;
    }
    ksort($strings);
    $ordered = implode(', ', array_map(function ($item) {
      return "'$item'";
    }, $items));
    $this->assertSame($items, array_values($strings), "Found strings, ordered as: $ordered.");
  }

}