Problem/Motivation
We have two very similar templates in Drupal links.html.twig and item-list.html.twig. Both of these templates loop through a set of items, and create a HTML list. This creates twice as much code to maintain, and two separate, nearly identical, things to keep up to date.
Proposed resolution
Let's remove links.html.twig, add item-list--links.html.twig and call theme('item_list__links') to generate link lists. This implies adding the heading tag functionality to item-list.html.twig and converting the template to handle keyed item lists, and turning those keys like links.html.twig currently does
Remaining tasks
- #2032645: Replace calls to theme('item_list') with calls to theme('links') for links, when a heading or wrapper div is warranted
- #1842140: Remove title and wrapper div from item-list.html.twig
- #939462: Specific preprocess functions for theme hook suggestions are not invoked
- Replace links.html.twig with theme('item_list__links') to generate the link list items throughout core (this issue)
User interface changes
None.
API changes
TBD
Related Issues
Postponed by: #939462: Specific preprocess functions for theme hook suggestions are not invoked
#1813426: [meta] Consolidate all item list templates and add theme_hook_suggestions
#1804614: [meta] Consolidate theme functions and properly use theme suggestions in core
#1939064: Convert theme_links() to Twig
Original report by @RobLoach
There's a function available to create an HTML list of items (theme_item_list), and there's a function to create an HTML list of links (theme_links). Wouldn't it make sense to make the function that creates a list of links take advantage of the function that creates the HTML list of items?
This patch makes theme_links use theme_item_list to create the list of links.
Comment | File | Size | Author |
---|
Comments
Comment #1
RobLoachWhoops, overlapping $attributes...... Fixed.
Comment #2
keith.smith CreditAttribution: keith.smith commented(not worth changing status, but if you reroll, there's a comment in there with no ending period.)
Comment #3
RobLoachAh, yeah... I'm constantly impressed by your good eye, Keith.
Comment #4
RobLoachComment #5
Anonymous (not verified) CreditAttribution: Anonymous commentedThe last submitted patch failed testing.
Comment #6
Todd Nienkerk CreditAttribution: Todd Nienkerk commented+1. This would be very, very useful. At the very least, a conversion function would be tremendously helpful -- i.e., something that can convert the
$links
normally sent totheme_links()
into somethingtheme_item_list()
can understand.Comment #7
Todd Nienkerk CreditAttribution: Todd Nienkerk commentedHere's my initial stab at at converting links to an item list:
Comment #8
sun.core CreditAttribution: sun.core commentedComment #9
sunNot sure whether this still makes sense when considering HTML5's <navigation> element? (http://www.alistapart.com/articles/previewofhtml5)
Comment #10
JacineYes plz!!! :)
Comment #11
sunThere's a non-obvious difference between links and item_list: links use the passed-in array keys as class names for the LIs.
Comment #12
Fabianx CreditAttribution: Fabianx commentedAdding tags from closed duplicate #1777326: Replace theme_links() with theme_item_list().
Comment #13
Fabianx CreditAttribution: Fabianx commentedComment #14
sunCopying over an essential comment from #1777326: Replace theme_links() with theme_item_list():
Comment #15
tim.plunkettAnother related issue: #891112: Replace theme_item_list()'s 'data' items with render elements
Comment #15.0
jenlamptonupdated summary
Comment #15.1
jenlamptonlink
Comment #16
jenlamptonComment #17
jenlamptonComment #17.0
jenlamptonlink
Comment #19
drupalninja99 CreditAttribution: drupalninja99 commentedWell one thing, is that now we are going to need to update several automated tests to compare the rendered links with
<div class="item-list">
. That div is present in theme_item_list but I suppose not present in theme_links. Is that okay?Comment #20
drupalninja99 CreditAttribution: drupalninja99 commented@Jen, I have updated 3 automated tests to include the wrapper div that theme_item_list adds. I bet that will fix some of these fails.
Comment #22
johnnydarkko CreditAttribution: johnnydarkko commentedWorking alongside dpintats #1842140: Remove title and wrapper div from item-list.html.twig
Comment #23
drupalninja99 CreditAttribution: drupalninja99 commentedOh okay, well then my patch #20 doesn't make sense if the wrapper markup is being removed.
Comment #24
steveoliver CreditAttribution: steveoliver commented#23: Yes.
Besides the issue with this wrapper (which I think we can safely keep in the test until the other issue removes the wrapper), @johnnydarkko and I found out the cause of [all?] the failing tests--the missing key class on list items previously set by
theme_links
in theme.inc:1723:Expecting we want to maintain this key class, the solution to the issue is to implement a preprocess function for adding the class for links lists:
And of course this preprocess won't get picked up until one or both of these issues are resolved:
#939462: Specific preprocess functions for theme hook suggestions are not invoked and/or #1751194: Introduce hook_theme_suggestions[_HOOK]() and hook_theme_suggestions[_HOOK]_alter()
In the interim, I believe we should be able to get the preprocess function firing by explicitly adding the suggestion to
drupal_common_theme()
:So, in summary, to move forward on this, I think:
1. Leave the wrapper in tests until #1842140: Remove title and wrapper div from item-list.html.twig is fixed and/or RTBC.
2. Implement preprocess for item_list__links to add key class back to each item in 'items'.
Comment #25
markhalliwell#1751194: Introduce hook_theme_suggestions[_HOOK]() and hook_theme_suggestions[_HOOK]_alter() is now in core.
Once #2035055: Introduce hook_theme_prepare[_alter]() and remove hook_preprocess_HOOK() makes it in,
hook_theme_prepare_item_list__links()
can become a reality.Comment #26
markhalliwellIn reality though, we should simply replace any calls to
theme('links')
or'#theme' => 'links'
with simplyitem_list__links
and just remove this completely unnecessary theme implementation. We'd still keep the suggestion prepare hook though.Comment #26.0
markhalliwelltwig
Comment #27
mitokens CreditAttribution: mitokens as a volunteer commentedComment #28
akalata CreditAttribution: akalata commentedWith #1939064: Convert theme_links() to Twig in head, theme_links() no longer exists. How is this issue affected?
Comment #29
jwilson3We now have two templates:
links.html.twig
anditem-list.html.twig
and although they both produce very similar HTML code, the "template logic" in them is quite different so I would be inclined to either:Option A:
Close this as works as designed.
Option B:
Eat our own dog food: remove links.html.twig and implement a real template suggestion in core:
item-list--links.html.twig
.We need to account for all of the differences from links.html.twig including:
Comment #30
markhalliwellThis really postponed on this related issue, ultimately. Now that it's in, "Option B" is actually a reality and the original intent of this issue. It still needs to happen... at some point.
However, given how close to RC we are, maybe it should be postponed for 8.1.x or 9.x? There is not really a good/current policy towards BC breakages for themes/templates/CSS in core.
Comment #31
jwilson3I identified a couple questions/clarifications while cleaning up the issue summary. The other issue #1842140: Remove title and wrapper div from item-list.html.twig states:
However, a wrapper div and title have nothing to do with whether a list has links or not, so this could certainly be improved. Assuming that issue is not blocked/postponed and will get in before this one, then we should really figure out and discuss a strategy here that improves/clarifies DX and TX at the same time.
1. We should improve the template so wrapper/title is independent of links. For this, I can see two options (don't know which is better, but am leaning towards the latter):
OR
2. A list of links is semantically-speaking, just "navigation". So we could use semantic
<nav>
wrapper and<hX>
for title tag, with aria landmarks (a la pager.html.twig) and then either leverage link-list--wrapped.html.twig or implement another template link-list--navigation.html.twig that hardcodes these tags into a template that a themer could easily target and override if desired.Comment #32
andypostlinks.html.twig
is a part of stable themeComment #33
catchMoving back to 8.2.x
We can't actually remove theme hooks/templates in minor releases, but we can remove usages and deprecate them.
Comment #42
catchThere's a patch here.
Comment #48
andypostit still blocked on #1842140: Remove title and wrapper div from item-list.html.twig because markup between
core/modules/system/templates/links.html.twig
andcore/modules/system/templates/item-list.html.twig
is not possible to make equal