Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Squeezed into #1260860-45: Rework language list admin user interface...
Problem
- Link lists are hardly extensible and alterable.
Example
- You have a table, with Operations column.
- You have one or more modules integrating with that table, adding operations.
- You have a renderable structure, but all the links get stacked into an odd #links property of a single element.
- Say hello to array_splice(), array_slice(), array_search(), array_pop(), and array_unshift()! Your only way to inject a link at a certain position.
Solution
- Introduce #type 'links'.
- Renders sub-elements of #type 'link', 'markup', or whatnot first, and as usual. (respecting #weight, #ajax, and whatever else you want to do)
- Takes the rendered #children of each sub-element and turns them into items of an item list.
- Profit.
Comment | File | Size | Author |
---|---|---|---|
#32 | 1300744-32.patch | 6.27 KB | kmox83 |
#17 | 1300744-17.patch | 1.89 KB | jibran |
#12 | 1300744-12.patch | 2 KB | jibran |
#12 | interdiff.txt | 1.19 KB | jibran |
#9 | drupal-1300744-9.patch | 8.71 KB | dawehner |
Comments
Comment #1
neclimdulsub
Comment #2
sunSo what this patch kinda cries for is a
#pre_theme[_wrappers]
facility in drupal_render().Essentially, we want drupal_render() to run on the renderable (sub-)elements as usual.
But then, instead of outputting the results, do some quick shuffling on the rendered #children of the sub-elements, and pass the ultimate result along to an already existing theme function.
I could imagine that this technique could become kind of a pattern to expose a theme function not only as theme function, but also as a #type that works with renderable arrays as input.
Comment #3
JacineHell yes! :D I love love love this idea.
Comment #4
sunWanted to know how it would look like with #pre_theme_wrappers, so here we go.
To be compared with #0.
Comment #5
sunAnd, FWIW, a funky #pre_render approach. :)
To be compared with #4 and #0.
Comment #6
dozymoe CreditAttribution: dozymoe commentedOne other option is in http://drupal.org/node/1295958. Dunno.
Comment #7
jwilson3@dozymoe, I like generalized solutions as much as anyone, but as you point out in comment #10, on #1295958: Arguments to theme as renderable array in a render array. Allow better nesting of html element., the code there could really lend itself to noticable performance issues for deeply nested use of render arrays.
On the other hand, in the case of links, where often lists of links are multi-level -- I'm wondering how this solution differs from that one. It looks potentially like by limiting this to a single #links option, second and third level links would have to be pre-rendered into html as their own list of links, before being added to the parent (which well could solves the performance problem of nested arrays of hell).
Comment #8
jwilson3There is really good info here. Tagging for visibility and consideration in theme cleanup
Comment #9
dawehnerLet's fix the @todo in the patch
Comment #11
jibranRelated #1828536: Rename 'type' variable of theme_item_list() to 'list_type'
Comment #12
jibrannode_page_default
is removed in #1806334-168: Replace the node listing at /node with a view so I exclude it form the patch. Added new@todo
.Comment #13
thedavidmeister CreditAttribution: thedavidmeister commentedRelated #2005970: In renderable arrays, #type should provide a #theme suggestion, similar to how drupal_prepare_form() works
Comment #14
thedavidmeister CreditAttribution: thedavidmeister commentedalso related #2025629: [meta] Ensure that all #theme/#theme_wrappers hooks provided by core are associated with a sensible, re-usable element #type
Comment #15
thedavidmeister CreditAttribution: thedavidmeister commentedThe patch no longer applies for me:
error: patch failed: core/modules/system/system.module:545
error: core/modules/system/system.module: patch does not apply
Comment #16
star-szrComment #17
jibranreroll
Comment #18
Eric_A CreditAttribution: Eric_A commentedtheme_item_list() does not render the #children property, so it's not suitable for #theme_wrappers.
Apart from that I think we should be moving this towards the approach from #2025629: [meta] Ensure that all #theme/#theme_wrappers hooks provided by core are associated with a sensible, re-usable element #type.
Comment #19
Eric_A CreditAttribution: Eric_A commentedWorking on this.
Comment #20
Eric_A CreditAttribution: Eric_A commentedComment #21
Eric_A CreditAttribution: Eric_A commentedRemoving "Needs reroll" tag.
Comment #22
Eric_A CreditAttribution: Eric_A commentedAnd now for real.
Comment #23
Eric_A CreditAttribution: Eric_A commentedThe focus of #2025629: [meta] Ensure that all #theme/#theme_wrappers hooks provided by core are associated with a sensible, re-usable element #type is on existing render arrays that declare a '#theme' property but not a type. I'm going to compile a list of files to deal with.
Replacing direct calls to theme() with render arrays is dealt with elsewhere and should probably be left out here.
Comment #24
Eric_A CreditAttribution: Eric_A commentedFrom the above files only views.api.php was left untouched.
Comment #25
Eric_A CreditAttribution: Eric_A commentedRemoved the #theme property.
Comment #26
thedavidmeister CreditAttribution: thedavidmeister commentedThe patch in #25 looks good from the perspective of #2025629: [meta] Ensure that all #theme/#theme_wrappers hooks provided by core are associated with a sensible, re-usable element #type.
I don't think it addresses @sun's concerns in the issue summary and subsequent comments though.
The very fist patch, in the issue summary introduces a new theme function that can be used in #theme_wrappers - although I'd prefer to call it theme_links_wrapper than theme_wrap_links to be more consistent with similar functions elsewhere. Could we try to incorporate that idea, or the #pre_render function proposed in #5 into #25?
I don't think we should be looking into anything like #2 and #4 here though.
The main reservation I have with the idea of any kind of processing step that comes after the render step (like #pre_theme_wrappers) is that I don't see (and nobody has ever explained to me) how that processing could ever be compatible with the "drillability" idea #2008450: Provide for a drillable variable structure in Twig templates, in the same vein as #2009132: Remove #pre_render and #post_render from drupal_render() as it prevents proper Twig drillability.
At this point I'm not sure if that means "drillability" is just a fantasy we've made up recently or a criticism of arbitrarily interleaving multiple render and processing steps.
Regardless, IMO this issue shouldn't be introducing any new rendering/processing steps into drupal_render(), that kind of thing needs discussion, tests written and to have its own issue. If we can address @sun's problems with #theme_wrappers or #pre_render then great, otherwise I think #25 will have to do as a first "baby step" for now and we can split out some follow-up issues to address whatever is getting in the way.
Comment #27
thedavidmeister CreditAttribution: thedavidmeister commentedOr... maybe we shouldn't be trying to render the individual links at all because in general I believe we're supposed to be as lazy as possible about calling drupal_render() so we can keep our data as structured data for as long as possible rather than "flattening" them into strings...
In which case, #25 is probably exactly what we should be doing here.
Comment #28
Eric_A CreditAttribution: Eric_A commentedRelated: #311011: Replace links.html.twig with item-list--links.html.twig. Haven't had the time to really look into it, but fixing that one first might get us towards a much, much more interesting implementation of #type links.
Comment #29
jibran#25: 1300744-25.patch queued for re-testing.
Comment #30.0
(not verified) CreditAttribution: commentedUpdated issue summary.
Comment #31
PinoloWorking on this one during today's DrupalItalia code sprint
Comment #32
kmox83 CreditAttribution: kmox83 commentedRemoved the part of the patch related to the files which have been removed from the core (Issue #2056513). Extended the patch to all the classes in core which uses the links.
Comment #33
kmox83 CreditAttribution: kmox83 commentedRelated to the comment #32
Comment #34
PinoloComment #35
neclimdulLooking good guys.
Comment #36
neclimdulI think you missed one though.
Comment #37
neclimdulthat wasn't need work... I miss the preview button.
Comment #38
kmox83 CreditAttribution: kmox83 commentedShould it be changed back to Reviewed & tested by the community?
Comment #39
neclimdulYes, sorry. Everything looks good.
Comment #40
sunmmm, this patch does not really resolve the problems outlined in the OP. :-/
To my knowledge, the Twig team also wants to get rid of
theme_links()
, because that is actually just an (item) list in reality, and it doesn't make sense to have a generic list template and another one for "a list of links".→ A new #type links should not only resolve the problems stated in the OP, but also incorporate the fact that each rendered child is just a list item; i.e., the #pre_render should
drupal_render()
each child element and then pass off the rendered children intotheme_item_list()
.Comment #41
thedavidmeister CreditAttribution: thedavidmeister commentedItem lists are a Twig template not a theme function now. Could we avoid touching drupal_render() in #pre_render if at all possible?
Comment #42
neclimdulThis patch actually abstracts the theme calls in all theses places into the #type so it could have a follow up to replace how its rendered or makes removing theme_links easier by having a central location. I still think this is RTBC.
Comment #43
thedavidmeister CreditAttribution: thedavidmeister commentedWell, we need some kind of consensus, but it sounds like either way we're due for an issue summary update.
Comment #44
star-szrAdding parent relationship.
Comment #45
lauriii