Problem/Motivation

Layout Builders' section layout UI presents an awkward ordering schema when adding additional, custom layouts.

Proposed resolution

Per u/timplunkett's suggestion on the Drupal.org Slack:

One thing to consider would be a feature request to add weight to the annotation and to the existing sort (which currently sorts by category and label)

Remaining tasks

tbd

User interface changes

tbd

API changes

tbd

Data model changes

tbd

Issue fork drupal-3089418

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

Webbeh created an issue. See original summary.

Webbeh’s picture

Issue summary: View changes
Webbeh’s picture

Issue summary: View changes
Webbeh’s picture

Saving the conversation from Slack for when it expires:

timplunkett (he/him):plunkett: 18 hours ago
You can sort it in hook_plugin_filter_TYPE_alter()

Colin Calnan 18 hours ago
Thanks @timplunkett (he/him) - What is the TYPE in this case?

timplunkett (he/him):plunkett: 18 hours ago
layout

Colin Calnan 18 hours ago
ah, ok...

timplunkett (he/him):plunkett: 18 hours ago
see layout_builder_plugin_filter_layout__layout_builder_alter for an example

timplunkett (he/him):plunkett: 18 hours ago
that's technically hook_plugin_filter_TYPE__CONSUMER_alter

timplunkett (he/him):plunkett: 18 hours ago
where you care about what the "consumer" is. like layout_builder, block_ui, etc

Colin Calnan 18 hours ago
wow, amazing, thanks for the clarity @timplunkett (he/him)

timplunkett (he/him):plunkett: 18 hours ago
np! hope it works out

Colin Calnan 17 hours ago
@timplunkett (he/him) I added an arbitrary weight in the annotation for each definition in my modules

 *   icon_map = {
 *     0 = {"content"}
 *   },
 *   default_region = "content",
 *   weight = "0",

and then sorted by that in the hook you provided above

  function cmp($a, $b)
  {
    return strcmp($a->get('additional')['weight'], $b->get('additional')['weight']);
  }
  
  uasort($definitions, "cmp");
}
tim.plunkett’s picture

Status: Active » Needs review
StatusFileSize
new864 bytes
new3.15 KB

This should work.

The last submitted patch, 5: 3089418-weight-5-FAIL.patch, failed testing. View results

Status: Needs review » Needs work

The last submitted patch, 5: 3089418-weight-5-PASS.patch, failed testing. View results

tim.plunkett’s picture

Status: Needs work » Needs review
Issue tags: +Blocks-Layouts
StatusFileSize
new3.49 KB
new666 bytes

The FAIL patch has the expected failure.

For the PASS patch, I forgot that grouped definitions are also sorted, had to fix that test case too.

tim.plunkett’s picture

Adding credit

colincalnan’s picture

I am having issues applying this to 8.7.7

Through breakpoints I can confirm that `getSortedDefinitions` never seems to get called and so the sorting doesn't apply.

Assuming there's some update in 8.8 and 8.9 that call this but it's missing in 8.7.7?

tim.plunkett’s picture

Issue tags: +Needs tests
StatusFileSize
new4.13 KB
new652 bytes

Well, none of that _actually_ worked because we don't sort our plugins when displaying them in the UI. Which is a bug on its own.
Here's a fix, but this needs a UI test.

Status: Needs review » Needs work

The last submitted patch, 12: 3089418-weight-12.patch, failed testing. View results

tim.plunkett’s picture

Status: Needs work » Needs review
StatusFileSize
new4.9 KB
new1.38 KB

Fixing existing tests. Still needs dedicated test coverage.

Webbeh’s picture

we don't sort our plugins when displaying them in the UI. Which is a bug on its own.

Does this need to be placed into its own issue? If so, happy to do that.

tim.plunkett’s picture

StatusFileSize
new4.34 KB
new2.26 KB

The interactions between getFilteredDefinitions(), getSortedDefinitions(), and getGroupedDefinitions() is odd.

getGroupedDefinitions() always calls getSortedDefinitions()

Of the five calls to getFilteredDefinitions():
2 are followed by calls to getGroupedDefinitions()
1 is followed by a call to getSortedDefinitions()
2 are left unsorted (the case in ChooseSectionController that affects this patch, and in BlockForm when presenting the visibility conditions)

Which means that if hook_plugin_filter_TYPE_alter()/hook_plugin_filter_TYPE__CONSUMER_alter() are used to sort plugins, in 3 of the 5 cases it will be undone by the code that follows. Which is unfortunate.

I see two ways forward.
One is to embrace the pattern of other calls to getFilteredDefinitions() and add a sort after the call.
Another is to add getSortedFilteredDefinitions() and getGroupedFilteredDefinitions() methods, somehow.

And even if we want to do the 2nd approach, I think we can do that after the first approach in a follow-up.

Still needs tests.

tim.plunkett’s picture

Status: Needs review » Needs work

NW for tests

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

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

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

seanb’s picture

StatusFileSize
new4.39 KB

Reroll for 9.3.x.

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

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

anybody’s picture

Just ran into this issue here: #3088073: Use weight to sort the layout selection

Weight is essentially needed and we should add it. I'll add it as TODO and hope we can finally finish this one ;)

Grevil made their first commit to this issue’s fork.

grevil’s picture

I manually applied patch "3089418-22.patch" on the issue branch.

grevil’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests

All done! I hope the provided test changes suffice! Failing tests seem to be unrelated! See #3315678: strnatcasecmp(): Passing null to parameter #2 ($string2) of type string is deprecated, for a similar issue.

EDIT: Still unsure, why the tests are not failing on 11.x-dev... They are caused by line 26-209 of LayoutPluginManager:

      if ($a->getCategory() != $b->getCategory()) {
        return strnatcasecmp($a->getCategory(), $b->getCategory());
      }
      return strnatcasecmp($a->getLabel(), $b->getLabel());

But yea, as we have not touched that part, I still think it is unrelated.

grevil’s picture

Status: Needs review » Needs work

Never mind, there are also failing tests related to this issue.

grevil’s picture

Status: Needs work » Needs review

Alright, now it should be fixed.

anybody’s picture

Status: Needs review » Needs work

@Grevil: Still 6 failing tests here.

grevil’s picture

Status: Needs work » Needs review

As mentioned in #32, they seem unrelated.

smustgrave’s picture

Status: Needs review » Needs work

Have not yet reviewed or tetsed

But seems to have some test failures.

Usually there's just 1 random failure but 6 consistently means the MR is causing them. If they're fixed in #3315678: strnatcasecmp(): Passing null to parameter #2 ($string2) of type string is deprecated then this should be postponed as it can't be merged with failures.

grevil’s picture

Thank you, @smustgrave! I'll take a deeper look later on. The test failures are also very consistent, even after rebasing, so it probably is somehow related to the changes here.

anybody’s picture

@smustgrave #37 references an unrelated issue with the same error. I just ran into exactly this issue in LayoutPluginManager.
We need a separate, but similar issue as referenced in #37 but for LayoutPluginManager!

Here are other similar issues:

anybody’s picture

stomusic made their first commit to this issue’s fork.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.