15th February 2011
I’ve had to develop a couple of Drupal sites recently where I needed to grab the array of Primary Links and do something bespoke with it. Drupal provides functions for this, but they return so much data it can be difficult to work with it, particularly when you have large menus.
The $primary_links variable returns a huge amount of data yet doesn’t include the children of menu items, even if the “expanded” property is set to true.
The function menu_primary_links() provides the right level of data - the title and link, but also doesn’t include a menu item’s children.
The way to get the full primary links data is to call the function: menu_tree_page_data(‘primary-links’). This returns all required data, but again contains a lot of other unnecessary data.
So I wrote this little recursive function to clean the array so it only contains the data I need:
function clean_navigation($links) {
$result = array();
foreach($links as $id => $item) {
$new_item = array('title' => $item['link']['title'], 'link_path' => $item['link']['link_path'], 'href' => $item['link']['href']);
if ($item['below']) {
$new_item['below'] = clean_navigation($item['below']);
}
$result[] = $new_item;
}
return $result;
}
The above function can sit in your template.php file, and can be called from anywhere. I put the following line in my page.tpl.php:
$nav = clean_navigation(menu_tree_page_data('primary-links'));
So finally the $nav variable contains a nice simple array containing only the required information:
[0]=>
array(3) {
["title"]=>
string(4) "Home"
["link_path"]=>
string(7) "<front>"
["href"]=>
string(7) "<front>"
}
[1]=>
array(4) {
["title"]=>
string(7) "Finance"
["link_path"]=>
string(7) "node/11"
["href"]=>
string(7) "node/11"
["below"]=>
array(1) {
[0]=>
array(3) {
["title"]=>
string(6) "Events"
["link_path"]=>
string(14) "finance/events"
["href"]=>
string(14) "finance/events"
}
}
}
and so on...
I often develop sites on my local machine before committing and transferring to a staging/live server, so during the development phase most of my sites reside on a URL similar to http://localhost:8800. …
17th May 2011
read more »I was just developing a Block View in Drupal for a freelance project, where the markup on the first row was different to the subsequent rows. I needed to change the markup to display an image and teaser, while the subsequent rows just has the title (which links to the node) and published date. …
13th April 2011
read more »Being able to sort a view using URL parameters seems like the most basic functionality, but it seems this is not possible using using views without some kind of module. I have to say Symphony handles this much better than Drupal. …
10th March 2011
read more »I often like to use template files to alter the HTML of a view to cut down on the masses of redundant markup that Drupal creates. But I just came across a problem where I was unable to get the file path of an image in my content type, despite the fact that the image was output in the view settings. …
8th March 2011
read more »