Problem/Motivation
At this moment, components inside a section are rendered in order of adding.
The weight and region is ignored.
For usecases where you want e.g. an offset calculated between multiple components, it is required that these components are rendered in the correct order.
Steps to reproduce
Create 2 items in a section
- place the last created item first
- put a breakpoint in the item render. The first created component will be rendered first despite its position
Proposed resolution
sort by region and weight in the toRenderArray
Remaining tasks
Write patch and tests
User interface changes
None
API changes
None
Data model changes
None
Release notes snippet
Comments
Comment #2
strykaizerPatch without test added.
Comment #3
strykaizerComment #4
tim.plunkettInteresting. So the resulting markup is correct, but the order of execution is what is causing problems for you?
Comment #5
ankithashettyJust fixed the custom command failure errors in #2. Retaining status as "Needs work" for tests, thanks!
Comment #7
tim.plunkettSwitching to an MR and fixing the broken test. Still needs dedicated test coverage. NR to kick off test bot
Comment #8
strykaizerCorrect, our usecase is a news magazine frontpage where content managers can place a "news listing" block in different positions.
We need those "news listing" blocks rendered in the correct order to keep track of the offset for the next "news_listing" block.
Comment #10
kristen polTagging for manual testing on 9.4.
Patch applies cleanly for Drupal 9.3, 9.4, and 10.
Comment #13
smustgrave commentedThis issue is being reviewed by the kind folks in Slack, #needs-review-queue-initiative. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge request as a guide.
For the tests requested in #4
Did not test.
Comment #15
kevinquillen commentedPerusing the issue queue, I think this is the right issue for this. I also noticed that the 'storage' of the data is stored 'out' of order, as OP reports. It is stored by time of adding. If you edit, items never seem to move, only their region and weight notation.
Here is an example.
I edited a node layout and changed the order of these blocks (field blocks). I would expect that they are stored in the database in the order that they are set. Only the field data has them out of order; I moved a block from the 'second' region to the 'fourth' region, and changed the order of blocks in the third region.
The data (pardon the verbosity, it is serialized):
I would expect that everything is stored in the order it is in the form.
To that end, correcting just the render array is not enough. The output from serialization should also be correct so that anyone using JSON:API for example gets the data in the correct order and does not have to waste frontend resources trying to figure it out.
Related: #2942975: [PP-1] Expose Layout Builder data to REST and JSON:API
While working with that issue and patch, I had to add this sorting code to a JSON Include parser:
Where
$section_datais just one sections worth of components. Ugly, but as far as I could tell it put the data in the order that someone had saved Layout Builder components for JSON:API.I would posit that core itself should ensure the storage of the data is accurate and not leave it up to themers or API consumers to figure it out themselves. To that end, it seems like it would improve overhead performance by not needing to re-sort items for forms, render, or APIs because the data is stored as it was entered.
Also, I had to assume that the regions are ordered (weighted) by how they are defined in the layout YAML. I am not sure if that would always be true, I do not know how people are constructing their own YAML layout definitions.
So, when you get the data from Drupal regardless of the method, it should come like:
sections
--layout
----regions (ordered)
------components (ordered)
--layout
----regions (ordered)
------components (ordered)
etc
Comment #16
dennisdkPatch #5 works great on 10.3.6!
The order that the blocks are put in, is now also the order of that they are rendered!