theme_links should return list of links

m3avrck - May 23, 2006 - 19:13
Project:Drupal
Version:x.y.z
Component:theme system
Category:feature request
Priority:normal
Assigned:Unassigned
Status:closed
Description

theme_links() should return a list of links, not a string seperated by |. Almost all themers override this function to produce a list of links, this seems like it is backwards. Let's output a list of links and those that don't want the list, can override the function. This equals a lot less work for themers.

New id and class hooks too.

AttachmentSizeStatusTest resultOperations
theme_2.patch7.59 KBIgnoredNoneNone

#1

Steve McKenzie - May 23, 2006 - 19:16

this is extremely needed.

I had this concern the other day.

+1 on the patch :)

#2

webchick - May 23, 2006 - 19:20

+1 for the concept. I'll try and test this later today.

#3

m3avrck - May 26, 2006 - 16:43
Status:needs work» needs review

Updated patch, fixed a few more issues.

Cleaning and well tested now, please review!

AttachmentSizeStatusTest resultOperations
links_8.patch8.67 KBIgnoredNoneNone

#4

Dries - May 26, 2006 - 18:15

I leave it up to other themers to decide whether this is useful or not. :)
Do we need to escape id and class or are these assumed to be safe?

#5

m3avrck - May 27, 2006 - 15:51

Hmm, I'm not sure if that is necessary. The only place this function is called is directly from core (with no attributes passed in) or from a theme file with PHP.

Not sure if escaping is needed at all...

#6

killes@www.drop.org - May 27, 2006 - 21:13
Version:4.7.0» x.y.z
Category:task» feature request

looks like a feature to me. :p

#7

webchick - May 29, 2006 - 03:24

Code looks good. I tested all four core themes and didn't notice any major drama apart from some subtle differences:

1. The menu gets indented slightly
2. The separator bar looks slightly different, because it is a border and not an actual "|"

I've attached a screenshot to show an example of the differences. They don't bother me personally, but figured I would post it for themers to review.

My only question is does it make sense to take this part:

return '

  • '. implode('
  • ', $output) .'

';

And put it in its own theme function? theme_links_list, for example? theme_links seems like an awful lot of code to copy and paste in order to override the link output in your theme. And yes, I realize this patch didn't touch this other code, but figured I'd bring it up since I'm looking at it. ;)

AttachmentSizeStatusTest resultOperations
theme_links.bmp243.05 KBIgnoredNoneNone

#8

webchick - May 29, 2006 - 03:25

Bah.

This part:

<?php
return '<ul'. $id . $class .'><li class="first">'. implode('</li><li>', $output) .'</li></ul>';
?>

#9

webchick - May 29, 2006 - 04:00
Status:needs review» needs work

Hm.. on closer review, more work is needed. For example, what used to look like:

reply | edit | delete

at the end of comments now looks like:

* reply
* edit
* delete

...in Chameleon anyway. Probably other themes as well.

A grep for theme('links' reveals the following functions which should be checked:

theme_node
theme_poll_results
aggregator_page_sources
aggregator_page_categories
theme_comment

#10

m3avrck - May 29, 2006 - 15:57
Status:needs work» needs review

Agreed, there will be some minor discrepencies by switching from text | to CSS borders, but that is about it.

I did a double check and all theme_links() are coming out correctly.

Webchick, did you try the latest patch I posted? I fixed a few more I missed. Also make sure to reload your CSS, as it is probably caching an out of date version :-)

#11

drumm - June 12, 2006 - 00:37
Status:needs review» needs work

I was just thinking of writing this myself.

- I think we should have a standard class added to all <ul> output by this function so we don't have to make such a large css selector which only catches the standard places.
- I'm guessing this won't work since theme_item_list isn't in too good of shape (it would put some extra HTML cruft around the <ul>), but it /might/ be a good idea to merge them since its all lists.
- I haven't tried, but I don't think this will apply since there was a round of changing '0em' to '0'.

#12

m3avrck - June 12, 2006 - 19:06
Status:needs work» needs review

Updated patch against HEAD.

Neil, I'm not sure what you mean by adding a standard class to the output, this seems more confusing IMO, but maybe I fail to see something.

AttachmentSizeStatusTest resultOperations
links_9.patch8.5 KBIgnoredNoneNone

#13

webchick - June 12, 2006 - 19:11

m3avrck: I think he means something like class="link-list" so we can differentiate a list of links generated from theme_links from a list of links that might happen to be present in page content or whatever that someone posted.

#14

Dries - June 23, 2006 - 18:26

I'm cool with this but am not commiting this yet -- let's figure out Neil's comment first.

#15

moshe weitzman - July 27, 2006 - 04:43

something like this went in with link_alter() patch? if so, please close the issue.

#16

m3avrck - July 30, 2006 - 00:21

No, this patch makes the default behavior for theme_links() to return a UL of links, instead of a string.

#17

Steve McKenzie - August 18, 2006 - 20:03
Status:needs review» reviewed & tested by the community

good to go.

#18

drumm - August 23, 2006 - 05:11
Status:reviewed & tested by the community» needs work

.primary-links li, .secondary-links li, .links li, .taxonomy li

This sort of thing is sprinkled all over. Any additional modules need to add CSS to make thier links display like other links. Themes have to think about which modules they will define styles for if they would like to override the look of all links.

There needs to be a consistent class provided by theme_links() that can be used to define simple CSS rules.

#19

m3avrck - August 24, 2006 - 06:46
Status:needs work» needs review

Here's an entirely new patch that really consolidates things.

theme_links() is much improved to be of perfect (well I hope) use for themers... the same logic is being used to generate the menu system on mtv.co.uk .. which I wrote (yes that is a Drupal menu!)

Otherwise, themes should look exactly the same, but they will be more semantically correct.

Docs will be updated when patch goes in.

AttachmentSizeStatusTest resultOperations
links_10.patch12.93 KBIgnoredNoneNone

#20

moshe weitzman - August 24, 2006 - 16:11

this looks cool. maybe though this function should build a links array and send to theme('item_list')? seems like there is some overlap now.

#21

m3avrck - August 25, 2006 - 05:46

Hmm, while there is some overlap I'm not sure if they would go as well together. theme_links() is looking for an array of links setup in a correct fashion--different than theme_item_list()

I do agree there can be some cleanup, but I'm not sure if they can be merged. They seem to offer different uses of lists--one for actual navigation with special CSS classes for funky menus (like mtv.co.uk ... it's using this ;-)) and then other for just regular ol' lists.

#22

moshe weitzman - August 25, 2006 - 12:26

i wasn't suggesting merging functions, just suggesting that theme_links() do some preprocessing and then call theme('item_links'). that may or may not be a good idea, as you say.

#23

m3avrck - August 29, 2006 - 03:17

Updated for HEAD.

Moshe, yeah I don't think sending any data to theme_item_list() would help. During the loop for theme_links(), we process all of the l() and add in some attributes. We could do this and put it into an array $data and pass it to theme_item_list() but that seems like overkill.

This patch has been set to RTBC before and it's still the same--but I've condensed it even more based on Neil's feedback. I think it's ready to go finally (this will be a *huge* boon to themers).

AttachmentSizeStatusTest resultOperations
links_11.patch12.93 KBIgnoredNoneNone

#24

quicksketch - August 29, 2006 - 05:35
Status:needs review» reviewed & tested by the community

First of all, this functionality really needs to get into core. To follow up on moshe's comments, I thought I'd weigh the benefits of using theme_item_list() in theme_links(). The plus side of using theme_item_list is that we're using less lines of code. I wrote up a modified verion of this patch which was 10-15 lines shorter by calling item_list().

After writing the patch, I found a few pit-falls with this approach.
* item_list isn't as detailed, for instance it doesn't include classes for each <li> and the 'first' and 'last' classifications
* even with improving item_list to include this information, the list is still wrapped in an unnecessary <div class="item-class"> (which breaks all the current themes, with no easy solution)
* and finally, as m3avrk pointed out to me, we're recursing through the array twice, instead of just once

So all in all, that seems like a lot of problems just to avoid 10 lines of code. I give the current approach a thumbs-up.

#25

rickvug - August 29, 2006 - 08:07

I leave it up to other themers to decide whether this is useful or not.

It's needed: this is the way to do this. This is semantically correct html (for those of us who care!).

#26

eaton - August 30, 2006 - 00:13

Having looked over it, I would agree, yes. This is indeed the way it should be done by default. This is the kind of code that folks implementing advanced themes have to copy and paste in every time.

+1. Yummy.

#27

drumm - August 30, 2006 - 07:37
Status:reviewed & tested by the community» fixed

Committed to HEAD.

#28

m3avrck - August 30, 2006 - 18:12

#29

Anonymous - September 13, 2006 - 18:16
Status:fixed» closed
 
 

Drupal is a registered trademark of Dries Buytaert.