Problem/Motivation

We have two very similar functions in Durpal theme_links and theme_item_list. Both of these functions 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.

The plan is to use theme_links() when a title and/or wrapper div is necessary, and theme_links() will call theme('item_list') to generate the list of items within. In turn, theme_item_list() will not contain a wrapper div or title anymore.

Proposed resolution

This means we will need to update core to use theme_links()) any time a title or wrapper div is necessary, and may need to

  • Clean up or code so we are not passing in 'title' => NULL, to theme_item_list() that will no longer accept a title
  • Move the titles from item_list into a #prefix on the render element
  • Remove other weirdness in our code to make markup consistent
  • Update tests to test for the new markup insted of old

Remaining tasks

There are eighty-three (83) instances of 'item_list' in core. Below is a list of *only* the ones that will need to be updated (since they require either a title or a surrounding div tag)

List of all instances of calls to theme('item_list') or render arrays with '#theme' => 'item_list' where a title or wrapper div is warranted

Location of item_list call Call theme('links')? why Code changes (this patch)
core/authorize.php YES has title replace item_list with links
core/modules/field/lib/Drupal/field/Plugin/views/field/Field.php NO Remove 'title' => NULL,
core/includes/theme.maintenance.inc NO not links move heading into #prefix on render element
core/modules/node/node.module YES has title replace item_list with links
core/modules/toolbar/tests/modules/toolbar_test/toolbar_test.module YES has title replace item_list with links
core/modules/views/lib/Drupal/views/Plugin/views/display/DisplayPluginBase.php NO $form['analysis'] needs to be updated to add the form-item class as attributes, no extra div plz
core/modules/views/views.theme.inc NO Remove '#title' => NULL,
core/update.php NO not links move title into #prefix on render element

User interface changes

None.

API changes

None.

#311011: Replace links.html.twig with item-list--links.html.twig
#1842140: Remove title and wrapper div from item-list.html.twig

CommentFileSizeAuthor
#70 interdiff.txt737 bytesManuel Garcia
#70 replace_calls_to-2032645-70.patch8.94 KBManuel Garcia
#67 replace_calls_to-2032645-67.patch8.99 KBjoelpittet
#67 interdiff.txt3.3 KBjoelpittet
#61 2032645-replacing_item_list_with_links-60.patch9.02 KBpiyuesh23
#58 2032645-replacing_item_list_with_links-58.patch9.32 KBpiyuesh23
#56 interdiff-2032645-45-54.txt2.15 KBCyberschorsch
#54 interdiff-2032645-45-54.txt5.44 KBCyberschorsch
#54 2032645-54.patch5.44 KBCyberschorsch
#45 reroll-replace_calls_to-2032645-45.patch6.57 KBmarieke_h
#39 toolbar-before.png30.87 KBrteijeiro
#39 toolbar-after.png31.48 KBrteijeiro
#39 interdiff.txt605 bytesrteijeiro
#39 drupal-item_list_to_links-2032645-39.patch6.76 KBrteijeiro
#38 interdif-2032645-35-37.txt5.55 KBcutesquirrel
#38 drupal-item_list_to_links-2032645-37.patch6.75 KBcutesquirrel
#35 drupal-item_list_to_links-2032645-35.patch4.84 KBManuel Garcia
#32 drupal-item_list_to_links-2032645-32.patch4.69 KBSumeetJaggi
#30 2032645_30.patch4.7 KBravi.khetri
#29 drupal-item_list_to_links-2032645-29.patch5.42 KBankitgarg
#27 drupal-item_list_to_links-2032645-27.patch5.47 KBgbisht
#24 drupal-item_list_to_links-2032645-24.patch5.45 KBadci_contributor
#17 drupal-item_list_to_links-2032645-15.patch6.18 KBforbesgraham
#14 drupal-item_list_to_links-2032645-14.patch7.78 KBjenlampton
#14 interdiff.txt2.37 KBjenlampton
#10 theme-item-list-to-links-2032645-10.patch8.93 KBpplantinga
#6 theme-item-list-to-links-2032645-6.patch8.05 KBpplantinga
#4 theme-item-list-to-links-2032645-4.patch2.94 KBpplantinga
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jenlampton’s picture

Issue summary: View changes

things to change

jenlampton’s picture

Title: [meta] Replace calls to theme('item_list') with calls to theme('links') (when a heading or wrapper div is warranted) » Replace calls to theme('item_list') with calls to theme('links') (when a heading or wrapper div is warranted)

Since there are only 5 instances of theme calls that need to be changed from 'item_list' to 'links' this does not need to be a meta issue, it can all be done in one patch.

jenlampton’s picture

Issue summary: View changes

cleanup

jenlampton’s picture

also, tagging

jenlampton’s picture

Title: Replace calls to theme('item_list') with calls to theme('links') (when a heading or wrapper div is warranted) » Replace calls to theme('item_list') with calls to theme('links') for links, when a heading or wrapper div is warranted

clarification :)

jenlampton’s picture

Issue summary: View changes

update

jenlampton’s picture

Issue summary: View changes

cleanup

jenlampton’s picture

Issue summary: View changes

cleanup

pplantinga’s picture

Status: Active » Needs review
FileSize
2.94 KB

I'm guessing a separate issue should be filed for converting the calls to theme() to drupal_render()?

Here's a possible patch.

A quick question though, the css will be converted and '#title' be removed with #1842140: Remove title and wrapper div from item-list.html.twig, right?

Status: Needs review » Needs work

The last submitted patch, theme-item-list-to-links-2032645-4.patch, failed testing.

pplantinga’s picture

Status: Needs work » Needs review
FileSize
8.05 KB

Okay... let's give this another shot.

Status: Needs review » Needs work
Issue tags: -Novice, -theme system cleanup, -Theme Component Library

The last submitted patch, theme-item-list-to-links-2032645-6.patch, failed testing.

pplantinga’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work
Issue tags: +Novice, +theme system cleanup, +Theme Component Library

The last submitted patch, theme-item-list-to-links-2032645-6.patch, failed testing.

pplantinga’s picture

Apparently we are now escaping a string that we weren't before, and that was throwing off a test.

Here's the version with the test updated.

pplantinga’s picture

Status: Needs work » Needs review
pplantinga’s picture

Issue summary: View changes

remove pager

steveoliver’s picture

+++ b/core/modules/toolbar/tests/modules/toolbar_test/toolbar_test.module
@@ -29,13 +29,17 @@ function toolbar_test_toolbar() {
+        '#heading' => array(
+          'text' => t('Test tray'),
+          'level' => 'h2',
+          'class' => array('visually-hidden'),
         ),
-        '#prefix' => '<h2 class="visually-hidden">' . t('Test tray') . '</h2>',
         '#attributes' => array(
           'class' => array('menu'),

class as a top level argument, rather than being #attributes, was the only thing that jumped out of me when reviewing this diff.

As a follow-up to this issue, since there already is a @todo in core for it(theme_links(), theme.inc:1706), #header['class'] should be replaced with #header['attributes']['class'].

Re: #4, I do believe the title, wrapper, CSS are supposed to happen in the other issue(s).

jenlampton: I'm not totally understanding the proposed resolution, but it seems the scope of this issue is fully addressed by this latest patch in #10.

If so, +1 to RTBC.

steveoliver’s picture

Issue summary: View changes

up

jenlampton’s picture

Issue summary: View changes

up

jenlampton’s picture

Issue summary: View changes

updated

jenlampton’s picture

@steveoliver the basic idea is to replace theme_item_list, which currently outputs an unnecesary div and title, with just an item list.

There were instances in core where we need the ability to have a div and a title added around an item list, so I went through core and identified all the places where that happens. Most of the time it's actually when we're generating a list of links, and as it turns out, we have a theme function for that: theme_links.

The scope of *this* issue is to identify all the calls to theme_item_list where we should be calling theme_links instead, and change them.

Once this is done, we can then work on #1842140: Remove title and wrapper div from item-list.html.twig without breaking tests.

jenlampton’s picture

Issue summary: View changes

update

jenlampton’s picture

Issue summary: View changes

remove unneccary rows

jenlampton’s picture

Rerolled.

I updated the summary to indicate which things should happen in this issue, and which in the other. Hopefully that will help clarify.

I'm going to leave the item_list test with a title in here, and we can remove that in #1842140: Remove title and wrapper div from item-list.html.twig

I also don't know if the word 'module' is ever translated, but just in case I wrapped the heading for update.php in a new t().

jenlampton’s picture

Issue summary: View changes

list

areke’s picture

Issue summary: View changes
Status: Needs review » Needs work
Issue tags: +Needs reroll

It looks like after 6 months of sitting here, the code needs to be re-rolled.

forbesgraham’s picture

Assigned: Unassigned » forbesgraham
forbesgraham’s picture

Re-rolling patch from comment 14.

forbesgraham’s picture

Status: Needs work » Needs review
pplantinga’s picture

Status: Needs review » Reviewed & tested by the community

Looks good to me.

chx’s picture

Issue tags: -Needs reroll
webchick’s picture

Status: Reviewed & tested by the community » Needs review

This all looks like a great clean-up! One quick thing...

+++ b/core/modules/statistics/lib/Drupal/statistics/Tests/StatisticsReportsTest.php
@@ -51,7 +51,7 @@ function testPopularContentBlock() {
-    $this->assertText("Today's", "Found today's popular content.");
+    $this->assertText(check_plain("Today's"), "Found today's popular content.");

That's a bit weird and afaik should not be necessary? Or at least I can't figure out why it would've passed before and not now.

pplantinga’s picture

Since this is part of a title that used to be just spit out as plain HTML, it wasn't checked to see if it was html-ready.

Old output: Today's

New output: Today&#039;s

I think the new output is better HTML, but correct me if I'm wrong...

jhedstrom’s picture

Status: Needs review » Needs work
Issue tags: +Needs reroll
adci_contributor’s picture

Trying to reroll

adci_contributor’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 24: drupal-item_list_to_links-2032645-24.patch, failed testing.

gbisht’s picture

Status: Needs work » Needs review
Issue tags: +SprintWeekend2015
FileSize
5.47 KB

Patch rerolled!

Status: Needs review » Needs work

The last submitted patch, 27: drupal-item_list_to_links-2032645-27.patch, failed testing.

ankitgarg’s picture

ravi.khetri’s picture

Status: Needs work » Needs review
FileSize
4.7 KB

Rerolled.

Status: Needs review » Needs work

The last submitted patch, 30: 2032645_30.patch, failed testing.

SumeetJaggi’s picture

Status: Needs work » Needs review
FileSize
4.69 KB

Re Rolled!

Status: Needs review » Needs work

The last submitted patch, 32: drupal-item_list_to_links-2032645-32.patch, failed testing.

Manuel Garcia’s picture

Issue tags: -Needs reroll

Patch applies cleanly.

Manuel Garcia’s picture

Status: Needs work » Needs review
FileSize
4.84 KB

Drupal\statistics\Tests\StatisticsReportsTest was failing due to fatal error, since the previous patch was calling check_plain, which is now String::checkPlain().

Plenty of tests to fix stil though.

Status: Needs review » Needs work

The last submitted patch, 35: drupal-item_list_to_links-2032645-35.patch, failed testing.

cutesquirrel’s picture

Assigned: forbesgraham » cutesquirrel
cutesquirrel’s picture

Status: Needs work » Needs review
FileSize
6.75 KB
5.55 KB

Here is the fixed #35 patch : the theme_links waited for an "url" key and not a "href" key.
I've also fixed the title which display the comment count on each popular node link.

rteijeiro’s picture

Issue summary: View changes
Status: Needs review » Reviewed & tested by the community
FileSize
6.76 KB
605 bytes
31.48 KB
30.87 KB

Fixed just a couple of nitpicks. It's RTBC for me.

Toolbar BEFORE

Toolbar AFTER

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 39: drupal-item_list_to_links-2032645-39.patch, failed testing.

akalata’s picture

Issue tags: +Needs reroll
akalata’s picture

Assigned: cutesquirrel » Unassigned
marieke_h’s picture

Assigned: Unassigned » marieke_h

I am at DrupalCon Barcelona mentored sprint. And will try to do this reroll (together with jnnck).
Documentation for reroll https://www.drupal.org/patch/reroll

marieke_h’s picture

We rerolled the patch from #39.
Let's see what the testbot says.

marieke_h’s picture

Status: Needs work » Needs review

The last submitted patch, 29: drupal-item_list_to_links-2032645-29.patch, failed testing.

marieke_h’s picture

Assigned: marieke_h » Unassigned
Cyberschorsch’s picture

I am reviewing this issue at dc barcelona

Status: Needs review » Needs work

The last submitted patch, 45: reroll-replace_calls_to-2032645-45.patch, failed testing.

The last submitted patch, 29: drupal-item_list_to_links-2032645-29.patch, failed testing.

Cyberschorsch’s picture

Assigned: Unassigned » Cyberschorsch

The last submitted patch, 45: reroll-replace_calls_to-2032645-45.patch, failed testing.

Cyberschorsch’s picture

Status: Needs work » Needs review
FileSize
5.44 KB
5.44 KB

I removed a still existing '#title' => NULL and removed the changes in the StatisticReportsTest which breaks the current tests.
As webchick mentioned in #21, I don't think we have to checkplain there.

If we look in the list in the issue summary, I am not sure what todo about the #prefix elements...

Cyberschorsch’s picture

Assigned: Cyberschorsch » Unassigned
Cyberschorsch’s picture

FileSize
2.15 KB

This is the correct interdiff

akalata’s picture

Status: Needs review » Needs work
Issue tags: -Needs reroll +Needs issue summary update, +Barcelona2015
  1. +++ b/core/authorize.php
    @@ -146,7 +146,10 @@ function authorize_access_allowed(Request $request) {
           '#theme' => 'item_list',
    

    Shouldn't this be changed to '#theme' => 'links'?

  2. +++ b/core/modules/statistics/src/Tests/StatisticsTestBase.php
    @@ -19,7 +19,7 @@
    -  public static $modules = array('node', 'block', 'ban', 'statistics');
    +  public static $modules = array('node', 'block', 'ban', 'statistics', 'comment');
    
    +++ b/core/modules/statistics/statistics.module
    @@ -105,6 +105,11 @@ function statistics_title_list($dbfield, $dbrows) {
    +    if (Drupal::moduleHandler()->moduleExists('comment')) {
    +      $query->leftJoin('comment_entity_statistics', 'ces', 'n.nid = ces.entity_id');
    +      $query->fields('ces', array('comment_count'));
    +    }
    

    These changes were introduced in #38, but I'm not sure they're in scope of this issue?

  3. The change to core/includes/theme.maintenance.inc described in the issue summary has not been included in this patch.
  4. The change to core/modules/views/lib/Drupal/views/Plugin/views/display/DisplayPluginBase.php may no longer be necessary, but we may need to investigate where that was changed and update the issue summary accordingly.
  5. The change to core/modules/views/views.theme.inc may no longer be necessary, but we may need to investigate where that was changed and update the issue summary accordingly.
  6. The file core/update.phpno longer exists in HEAD, but we may need to investigate where that was changed and update the issue summary accordingly.
piyuesh23’s picture

Status: Needs work » Needs review
FileSize
9.32 KB

Handled the comments above. 8.0.x HEAD has a few other instances for theme_item_list with #title as well.

Uploading the updated patch here.

Status: Needs review » Needs work

The last submitted patch, 58: 2032645-replacing_item_list_with_links-58.patch, failed testing.

The last submitted patch, 58: 2032645-replacing_item_list_with_links-58.patch, failed testing.

piyuesh23’s picture

Status: Needs work » Needs review
FileSize
9.02 KB

Patch created with core folder as the base. Uploading the correct patch again.

Status: Needs review » Needs work

The last submitted patch, 61: 2032645-replacing_item_list_with_links-60.patch, failed testing.

The last submitted patch, 61: 2032645-replacing_item_list_with_links-60.patch, failed testing.

Manuel Garcia’s picture

Status: Needs work » Needs review

It's great to see progress on this =)

+++ b/core/authorize.php
@@ -146,7 +146,10 @@ function authorize_access_allowed(Request $request) {
+    $heading = array(
+      'text' => t('Next steps'),
+      'level' => 'h3',
+    );

+++ b/core/modules/node/node.module
@@ -187,16 +187,37 @@ function node_entity_view_display_alter(EntityViewDisplayInterface $display, $co
+  $links = array();
...
+    ) : array();
+    $links[] = array(
+      'title' => $row->title,
+      'url' => new Url('entity.node.canonical', ['node' => $row->nid]),
+      'attributes' => $options,
+    );
...
+    $heading = array(
+      'text' => $title,
+      'level' => 'h3',
+    );
+    return array(
+      '#theme' => 'links__node',
+      '#links' => $links,
+      '#heading' => $heading
+    );

Let's do these in short array syntax.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

joelpittet’s picture

Version: 8.2.x-dev » 8.3.x-dev
Issue tags: +Dublin2016
FileSize
3.3 KB
8.99 KB

Did the array() that I could, removed the return that was left in there and this should be good for a review.

Status: Needs review » Needs work

The last submitted patch, 67: replace_calls_to-2032645-67.patch, failed testing.

Manuel Garcia’s picture

+++ b/core/modules/node/node.module
@@ -186,19 +186,37 @@ function node_entity_view_display_alter(EntityViewDisplayInterface $display, $co
+  if ($has_rows) {
+    $heading = [
+      'text' => $title,
+      'level' => 'h3',
+    ];
+    return [
+      '#theme' => 'links__node',
+      '#links' => $links,
+      '#heading' => $heading,
+    ];
+  }
+  else {
+    return FALSE;

Bit of a non-issue, but we could avoid using if else here by returning FALSE early if !$has_rows... makes for easier reading

Other than this, interdiff and patch looks good to me. Good catch @joelpittet on removing that leftover return btw.

Next step, fix the failing tests :)

Manuel Garcia’s picture

Addressing #69

Status: Needs review » Needs work

The last submitted patch, 70: replace_calls_to-2032645-70.patch, failed testing.

Manuel Garcia’s picture

Status: Needs work » Postponed
Issue tags: +Needs subsystem maintainer review

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

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

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

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.

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.

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.