Problem/Motivation

In Manage Display / Custom Display Settings tab (e.g. admin/structure/types/manage/article/display) the view modes are ordered based on the machine name.

Proposed resolution

Sort the view modes based on the visible name, rather than the machine name.

This issue especially affects non-English views and custom installations with extra view modes where labels have been changed from those originally created.

The following screenshot illustrates the issue (shown after patch applied):
Illustration of the issue (after patch)

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

mfernea created an issue. See original summary.

mfernea’s picture

Status: Active » Needs review
FileSize
844 bytes

Here is the patch.

LoMo’s picture

Status: Needs review » Reviewed & tested by the community
FileSize
515.52 KB

It wasn't immediately clear to me what was meant, based on the issue description, but I figured it out and have verified that your patch works beautifully. You are right that the display modes should be sorted by the label, not by the machine name. I imagine that if I changed the language, I would be able to see that the display modes are not in alphabetical order, but in English, the machine names of the default display modes and the labels correspond, so the alphabetical order is correct. To illustrate the problem, I created a new display mode, naming it "Apple" (just to make it appear at the top of the list). I then changed the name to "Wapple", which should have moved it to the bottom of the list, but it still appeared at the top. After applying your patch, it appears at the bottom. So I was able to observe the issue (on this test 8.4.x-dev install) without switching to a language where the display mode labels might not correspond to the machine names.

illustration of the issue

I love the simplicity of this one-line patch; tests aren't broken by it, and it does something that really makes sense, so I hope this can just be committed without further ado. :-)

I think this can be marked RTBC.

tstoeckler’s picture

Status: Reviewed & tested by the community » Needs review

Often times in core we use natcasesort() for that instead of asort(), so I think that should be use here, as well. What do you think?

mfernea’s picture

Indeed natcasesort() is a better option. Thanks for the suggestion. Here is the patch.
@LoMo: Feel free to update the title and description of the issue.

LoMo’s picture

Issue summary: View changes

Description updated based on suggestion in #5. I think this makes the issue clearer to quickly understand.

LoMo’s picture

Status: Needs review » Reviewed & tested by the community

I agree that natcasesort() is better. And it works just as well (better, I guess, since case-sensitivity won't affect the sort order). I've tested this locally with the same results as reported in #3. I think we can put this back to RTBC. :-)

tstoeckler’s picture

Perfect, thanks for the updated patch @mfernea and thanks for testing again @LoMo!

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 5: drupal-order-view-modes-2858569-5.patch, failed testing.

LoMo’s picture

Huh? In #5, the message shows that the patch passed, but #9, the system says it failed??? Did it fail in some other combination of PHP/DB versions? If so, why don't we get more information? In any case, I suspect a glitch.

Retesting...

LoMo’s picture

Status: Needs work » Reviewed & tested by the community

Moving back to RTBC. The patch applies, the tests in #5 indicate pass, and it's such a simple patch I really cannot imagine it failed in some combination of environments (except due to a glitch), since any other reason would produce consistent failure (e.g. a test that expects the display modes to be displayed in order of machine name would consistently fail if the machine names and label names were not similar). Why is the system giving mixed messages here?

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 5: drupal-order-view-modes-2858569-5.patch, failed testing.

LoMo’s picture

Now the page does correctly say that #5 failed. But having looked at the results, it seems some tests just aren't as stable as they could be. No single test is consistently failing, and the tests which have failed do not seem likely to be related to this issue, so we'll re-run... again.

LoMo’s picture

Status: Needs work » Reviewed & tested by the community

Setting back to RTBC. Hopefully the tests won't glitch this time, but really they seem highly unlikely to be affected by this patch.

Tests seem to have passed this time (all of them), just like the first time. Only the 2nd and 3rd runs failed. I think this is safe to commit and does what it should.

alexpott’s picture

Category: Feature request » Bug report
Status: Reviewed & tested by the community » Needs work
Issue tags: +Usability, +Needs tests

This is a usability bug and should have an automated test to make sure we don't break it in the future. Discoverability and sensible orders of things help people use Drupal.

LoMo’s picture

Assigned: Unassigned » LoMo

I'm working on building a test for this. Writing such tests falls into my area of expertise, but this will be my first "BrowserTest" for D8 core, so I'll be happy to have any feedback when I'm done.

LoMo’s picture

Assigned: LoMo » Unassigned
Status: Needs work » Needs review
Issue tags: -Needs tests
FileSize
4.07 KB

I've created a browser test which I added to core/modules/field_ui/src/Form/EntityDisplayFormBase.php, where it seemed similar actions were already being tested (and where an environment is set up with the Article content type and an appropriate user). The test fails at the expected point if the original patch is not applied. And it passes if I apply the patch. My test uses patterns seen in other Functional (BrowserTest) tests already in core, which were useful examples.

Note that this patch includes the original single-line change to core/modules/field_ui/src/Form/EntityDisplayFormBase.php (no interdiff created since the original patch was so simple and mine only adds a function to one test suite (and a necessary permission to the test user setup).

mfernea’s picture

Looks good. I'm just adding two patches: one with the test only and one with the test and fix + the interdiff.
I only modified the comments to follow the coding standards and used $this->assertSession()->pageTextContains() instead of $this->assertText() (deprecated).

The last submitted patch, 18: drupal-order-view-modes-2858569-18-tests.patch, failed testing.

LoMo’s picture

Thanks, @mfernea. I figured there were probably some things that needed tweaks in there. Re the test in one patch, it's clear that this should fail, since the fix is not in the same patch, so this looks good.

I didn't realize $this->assertText() was deprecated. I guess that only applies to newer "Functional" tests and the assertText and assertUrl, etc, functions that are deprecated are still perfectly valid if extending one of the legacy Simpletest-based tests (which are slowly being replaced (?). Anyway, I see lots of uses of assertText(), so I guess the process of replacing it would be done in smaller chunks, replacing all such deprecated functions with their recommended replacements (e.g. one core module at a time)... and I assume there must be a roadmap for that.

mfernea’s picture

I have a second thought about placing the tests in the field_layout module. The changes are done in field_ui, so I would add the tests to Drupal\field_ui\Tests\ManageDisplayTest (although is not a PHPUnit test). What do you think?

LoMo’s picture

Valid point made in #21, @mfernea. I'll need to come back to this. For functional tests like this, it's helpful to have a setup that supports the test without the need for great time overhead of creating a new environment/users, etc. That test class already had a test function that opened the same path (admin/structure/types/manage/article/display). Looking at the field_ui functional tests, perhaps it better fits in the EntityDisplayModeTest.php file/test class. I'll try to get back to that a bit later today.

LoMo’s picture

Okay... that wasn't as simple as I hoped it would be. But it's working and in a more appropriate test class now (within the field_ui module, where it should be). We should see the test-only patch end with one failure and the "complete" patch should pass.

The last submitted patch, 23: drupal-order-view-modes-2858569-23-tests.patch, failed testing.

LoMo’s picture

And the tests-only patch fails (as hoped) and the patch with tests passes (ditto). Looking good, I think... :-)

mfernea’s picture

Looks good.
One small thing: drupalCreateUser, in general, is called in the setup phase. Wouldn't it be better to do so here?

tstoeckler’s picture

+++ b/core/modules/field_ui/tests/src/Functional/EntityDisplayModeTest.php
@@ -16,7 +16,7 @@ class EntityDisplayModeTest extends BrowserTestBase {
-  public static $modules = ['block', 'entity_test', 'field_ui'];
+  public static $modules = ['block', 'entity_test', 'field_ui', 'node'];

It is a bit unfortunate to be testing this with nodes. I think it should be possible to test this with the entity test entity type that is already being enabled. Did you try that? Or did you encounter any issues with that maybe?

LoMo’s picture

Issue summary: View changes
FileSize
211.7 KB

Re #26 In some test classes it's appropriate to create an admin user (or whatever) and even log in as such during the setUp function. This test class had existing functions that did the on-the-fly create/login of user after attempting access (anonymous), so logging in during the setUp didn't seem appropriate. Nor did creating another user in the setUp, if it's only used in this test function and not the others. But creating a whole new test class seemed even less appropriate. If you look at the whole file and what other tests are doing, I think you'll understand why I just stuck to the same pattern in use here. But I agree and in the other test class (in the field_layout module) it was done that way. Extending existing test classes is often requires making compromises from what I'd like to do, but I prefer to find a reasonable "home" for a new test function in an existing class to creating a whole new test file. In this case, it's not a beautiful compromise, but I have spent way too much time on the test for this issue, especially given that the test is for a regression that probably will never happen (in this place) ever again, once this patch is committed.

Re #27. @tstoeckler: Yeah, I know. But I'm not making the test more complicated than it has to be, I promise. ;-) This bug only applies to the list of display modes seen on a content type display tab. The sort order bug actually doesn't affect the list of display modes (on admin/structure/display-modes/view, the sort order is already correct). For instance, on that page, if I rename "Teaser" to "Breezer", as this test does, it already appears at the top without this patch. The bug only comes into play when viewing the list of view modes on a content type (see screenshot for the issue). The following is a screenshot from admin/structure/display-modes/view, as seen after making the change the test does, and shows that the bug doesn't appear when just looking at the display-modes separate from a content type:

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

mfernea’s picture

Re-roll of the complete patch at #23.

borisson_’s picture

Status: Needs review » Needs work

This looks really good. It had a failing test in #23 that proves that this fixes the problem.

+++ b/core/modules/field_ui/tests/src/Functional/EntityDisplayModeTest.php
@@ -140,4 +146,62 @@ public function testEntityFormModeUI() {
+    $this->drupalPostForm('admin/structure/display-modes/view/manage/node.teaser', $edit, t('Save'));
+    $this->assertSession()->pageTextContains(t('Saved the Breezer view mode.'));

I have a really small nit to pick, otherwise this is RTBC for me.

The patch doesn't need to have the t() here, as it's only installed in one language.

This is a really, really small nitpick, sorry.

mfernea’s picture

Status: Needs work » Needs review
FileSize
4.41 KB
888 bytes

Here is the new patch and interdiff.

@borisson_ no problem :)

borisson_’s picture

Status: Needs review » Reviewed & tested by the community

The interdiff is the wrong way around, but in the patch it's fixed. Thanks!

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 33: drupal-order-view-modes-2858569-33-complete.patch, failed testing. View results

mfernea’s picture

Status: Needs work » Reviewed & tested by the community

I think the bot went crazy for a moment. :)

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed and pushed 93bf4cb540 to 8.6.x and 043563222a to 8.5.x. Thanks!

  • alexpott committed 93bf4cb on 8.6.x
    Issue #2858569 by mfernea, LoMo, tstoeckler, borisson_: Custom Display...

  • alexpott committed 0435632 on 8.5.x
    Issue #2858569 by mfernea, LoMo, tstoeckler, borisson_: Custom Display...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

edmonkey’s picture

Version: 8.6.x-dev » 9.4.x-dev

I'd love to see these actually sortable, as in my case I want to have a non-alpha order, ideally setting the order with crosshairs at /admin/structure/display-modes/view

Eg; my views modes are: 'Inline, Small, Medium, Large, Extra large' , but the alphabetical order gives them 'Extra large, Inline, Large, Medium, Small' - which is not a a tad frustrating for the user on embeds via WYSIYWG.

Any suggestions?