Problem/Motivation

When a view uses multiple attachments to be displayed in the same location (e.g. all "above" or all "below"), they are added in order of their machine names, rather than the order specified in the view.
This behavior is not obvious to the user, and changing a display's machine names likely leads to unsemantic naming. If done at a later point, this can furthermore lead to problems because of the old machine name being used in code etc.

Steps to reproduce as an example:

  1. Create a "pets" view with a page and two attachment displays, "cats" and "dogs", both set to output "after" the page.
  2. Click on "Reorder displays" and sort them as "page_1", "dogs", "cats".
  3. On the pets page, the cats will be output before the dogs even though configured differently.

Proposed resolution

Since displays can be rearranged by the user, the attachments should adhere to this order.

Remaining tasks

Write a test for this scenario, review the patch.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

ckaotik created an issue. See original summary.

ckaotik’s picture

Assigned: ckaotik » Unassigned
Status: Active » Needs review
FileSize
831 bytes

I've attached a patch, please review.

Status: Needs review » Needs work
SocialNicheGuru’s picture

This worked for me

zahord’s picture

Hi all, checking the patch #5, the test is failing for the Drupal\Tests\views\Functional\Plugin\DisplayTest because two attachments contains the same position, and when you add this $attached_displays[$display['position']] = $display_id;, a view display id is removed.

in the current patch, the approach is to sort first the view displays regarding the position before to get the attached_displays array.

zahord’s picture

Status: Needs work » Needs review
joachim’s picture

Version: 8.5.x-dev » 8.7.x-dev
Status: Needs review » Needs work
  1. +++ b/core/modules/views/src/Plugin/views/display/DisplayPluginBase.php
    @@ -688,11 +688,15 @@ public function usesExposedFormInBlock() {
    +    // Sort displays regarding position value.
    

    Reads a bit funny.

    How about:

    "Sort the displays by their position."

  2. +++ b/core/modules/views/src/Plugin/views/display/DisplayPluginBase.php
    @@ -688,11 +688,15 @@ public function usesExposedFormInBlock() {
    +    uasort($all_displays, function ($a, $b) { return ($a["position"] <= $b["position"]) ? -1 : 1; });
    

    Should use SortArray::sortByKeyString() here.

Vidushi Mehta’s picture

Patch added.

Vidushi Mehta’s picture

Vidushi Mehta’s picture

FileSize
1.11 KB

Status: Needs review » Needs work

The last submitted patch, 8: interdiff-5-8.patch, failed testing. View results

joachim’s picture

+++ b/core/modules/views/src/Plugin/views/display/DisplayPluginBase.php
@@ -688,11 +689,15 @@ public function usesExposedFormInBlock() {
+    //Sort the displays by their position.

Missing space after the //

+++ b/core/modules/views/src/Plugin/views/display/DisplayPluginBase.php
@@ -692,8 +693,8 @@
+    SortArray::sortByKeyString($all_displays, function ($a, $b) { return ($a["position"] <= $b["position"]) ? -1 : 1; });

That's not how this method is used. It's a callback for uasort().

eli-on-drupal’s picture

Status: Needs work » Needs review
FileSize
1.42 KB
935 bytes

Here's a corrected patch.

joachim’s picture

Status: Needs review » Reviewed & tested by the community

LGTM!

catch’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs tests

Patch itself looks OK, but this needs a test.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

bobbygryzynger’s picture

Adds a test for the work done in #13.

The last submitted patch, 17: 2943293-17-test-only.patch, failed testing. View results

bobbygryzynger’s picture

In order to not break the alphabetical render ordering of existing display attachments, we need an update hook to rewrite positional ordering in alphabetical order.

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

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). 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: 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.

ranjith_kumar_k_u’s picture

Status: Needs review » Needs work

The last submitted patch, 25: 2943293-25.patch, failed testing. View results

ravi.shankar’s picture

Fixing failed tests of patch #25.

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.

Matroskeen’s picture

Re #19:

Let's discuss this by looking at the following example:

1) Display "Block";
2) Display "Attachment 1";
3) Display "Attachment 2";
both attachments were added to "Block".

Sites without this patch will always display attachments in the following order: 2) and 3) ignoring their position.
Sites with this patch will consider the position, so it can be either 2), 3) or 3), 2) depending on the configuration.

The proposed update hook will change the positions and preserve the behavior for sites without the applied patch.
But if the patch is already applied and the position was changed, there is no way to know that, right?

Perhaps the change record will be enough in this case?

Lendude’s picture

@Matroskeen we can't really take sites that already use this patch into consideration here when writing the update. For this issue that MIGHT still be doable, but for larger and longer running issues we just need to let that go. People that have applied patches need to be aware that things can shift.
So we only consider sites that are unaware of this bug when doing the update.

gayatri chahar’s picture

Status: Needs work » Needs review
FileSize
1 byte

Fixed for 9.5.x-dev

Rassoni’s picture

Rerolled patch

Matroskeen’s picture

Version: 9.5.x-dev » 10.0.x-dev
Status: Needs review » Needs work

As per comments #19, #29, and #30, we need an update hook to take care of existing views. Back to Needs Work for that.

rex.barkdoll’s picture

FileSize
246.05 KB

Until we get this fixed properly, using the ReOrder Displays option. I've got a hack - If you go under Advanced in the view and change the Machine Name to be in Alphabetical order for how you want, It will reorder the view's attachments on the front end.

ie:
Let's say your parent view's machine name is "daily" and your attachments' machine names are "attachment_1" and "attachment_2". You want the order to be: daily, attachment_2, attachment1. So now you rename the attachments' machine names: "attachment_2" becomes "daily_1" and "attachment_1" becomes "daily_2"

Screenshot of how to change the machine name

Save the view and you should see everything in the order you want.

Full credit to Ruc Van over here: https://drupal.stackexchange.com/questions/242274/how-can-i-order-views-...
for figuring this out.

drase15’s picture

Having same problem and also same solution as #31

You can use numbers to set your order, but if you don't like to use "standard" names on your attachments, you can use text instead and drupal view will order alphabetical

ameymudras’s picture

FileSize
16.93 KB

Re-rolling patch #32 to 10.0.x, I haven't fixed the issues mentioned in #19, #29, and #30 to write an update update hook. Will work on it once I have some time. Not changing the issue status for the same reason

Version: 10.0.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. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.