Okay, so I have searched through drupal and found this discussion come up all over the place, but with no solid answer.

I am looking to change the order of $links.
So A,B,C,D to B,A,C,D.
For example, at the bottom of a node or blog I may have "comments, related items, something, whatever".
But I want it to be "related items, comments, something, whatever".

So I have come to gather that this can be done through making a custom theme_links and/or using hook_link_alter.

There are examples of the code used for this, however, they all fail give a guide or to explain how to write the php and logic to actually re-order the links.

I'm sure many would appreciate an explanation for how to do this. Thanks.

Just a few of the code/examples I found on this:
http://drupal.org/node/44708
http://api.drupal.org/api/HEAD/function/theme_links
http://drupal.org/node/44435

Comments

summit’s picture

Hi,

Did you allready try http://drupal.org/project/nodeorder ?

Greetings,
Martijn

Anonymous’s picture

I haven't, but installing 4 modules for such a small thing seems like a bit much. I'd much rather learn the little bit of drupal/php code needed for the change than to try incorporating 4 more modules to an already heavily moduled site. Thanks for the suggestion though.

So yea, if anyone knows the logic and could give an example, that'd be appreciated :)

nevets’s picture

The first part is where to re-order the links and is pretty straight foward. The second part is the one that could be more challanging, that is ordering the links. Here part of the question is how you want the links re-order. Some examples would be module (the default), alphabetically and administrator picked. Alphabetically can be completely by overrideing theme_links() or possible the link_alter hook. Being able to pick the order would required some form of administrative code to allow setting the links order plus an overriden version of theme_links() to "honor" the settings

Anonymous’s picture

Perhaps I'm asking the wrong question. I understand how everything is being over-ridden and so forth, but not how to actually code the re-ordering.

I can't seem to find any examples on this.

nevets’s picture

If you want them alphabetically that is pretty straightforward, if you want them in a fixed order (i.e. the code determines the order) that is also pretty straightforward. On the other end if you want to be able to choose the order with an interface that would be more work. So how do you want them re-ordered?

Anonymous’s picture

I just want to be able to code a Fixed order (no interface needed). I actually want to do exactly as my example is in the first post.

nevets’s picture

For this to work you need to know the index of the links you want to force the order on. There is a line of code at the start of the function commented out that you can remove the comment from to see the link indexes. This code goes in your themes template.php file. If your theme does not have such a file you will need to create it, in this the file needs to start with <?php.

This may not be best practices since use the links_alter hook might be better but requires an array copy and would need to be implemented in a module. This version on the other hand directly calls theme_links at the end.

function phptemplate_links($links = array(), $delimiter = ' | ') {
  /**
* catches the theme_links function
*/
	// Uncomment the following line to see the links and the indexes
	// print '<pre>' . print_r($links, TRUE) . '</pre>';
	
	$ordered_links = array();
	
	// Indexes of links we want to force order for 
	// Links accounted for here will be in order in this array
	
	$in_order = array('taxonomy_term_1', 'taxonomy_term_2');
	
	// Move links we care about to $ordered_links array
	//   Will be added in order index is found in $in_order array
	
	foreach ( $in_order as $index ) {
		// Make sure the link exists
		if( isset($links[$index]) ) {
			$ordered_links[] = $links[$index];
			unset($links[$index]);
		}
	}
	
	// Add any links not accounted for at end
	
	foreach ( $links as $link ) {
		$ordered_links[] = $link;
	}

	// Not really a best practice but it avoid copying
	//	the code in theme_links
	
	return theme_links($ordered_links, $delimiter);
}
Anonymous’s picture

Thanks, this is just the type of thing I was looking for and I can actually understand what each part is doing.

About the return theme_links though. This isn't really needed unless you want an unordered list, correct? I currently just want a strait line printout with commas inbetween.
To do this, I changed:
return theme_links($ordered_links, $delimiter);
to
return $ordered_links
This would just be returning and array now, but I forward that to a different link theming function I already have and it prints in a line with commas (in the order created through this of course). Oh and obviously making that change makes the second parameter in this function useless...so I took that out.

Thanks again.

nevets’s picture

You could also replace

return theme_links($ordered_links, $delimiter);

with

return implode(', ', $ordered_links);

which would remove the need to call yet another theme function.

tille’s picture

..had a similar problem, finally found a way to not only re-order the links, but to place them wherever.. - ..and posted it in this thread:

http://drupal.org/node/115920#comment-273091

greetings..

___________________________
my pictures: www.bilderbook.org

___________________________

scalp’s picture

I realize this is an old post so I don't know if anyone's still listening, but I'm trying to use the above for Drupal 6 and it doesn't seem to be doing anything. I can't seem to find the indexes. Where should they be showing up after uncommenting the line in the code? I've checked the page source, both with it commented and without, and they seem to be exactly the same.

NoRandom’s picture

Here is a solution wich works for Drupal 6 (and also for 5 with little modification): http://drupal.org/node/115920#comment-2183468