views:

249

answers:

2

I have seen a few functions that allow one to attach classes/ID's to menu items; unfortunately, none of them apply to my use case. What I want to do is assign a class/ID to any menu item based on the content type of the linked item.

For example, I have a content type called "Internal" which is only displayed if the viewer is within the accepted IP range (my way of having intranet pages without having a separate site). I want to style these links differently.

On the same lines, I would like to be able to style external links in the menu as well.

Note, I am aware of the Menu Attributes module (http://drupal.org/project/menu_attributes), which does allow me to assign my own ID's to the menu items, but I want something automated as I have well over 100 content editors on the site. Something like this would ideally be an automated process.

A: 

You could load a different theme in the configuration directory. You'd probably need a host name for your IP range.

sites/default/themes/my_theme
sites/intranet.tld/themes/my_theme

You'd avoid duplication by symbolic linking the theme and over writing the html/css you need.

See the documentation in settings.php

That's just one of many ways...

Rimian
...many ways of doing what? Why would I want to load a different theme? I am looking to customize the menu functions not how to set up a sub site.
Marc
A: 

You can add the class in theme_menu_item or theme_menu_item_link (depending on if you want it on the li or the a). Then you need some method of figuring out the content type of a given menu item. Something like node_load(array('path' => $link['href'])) should do the trick (untested).

Scott Reynen
I had considered using node_load(), but I have no idea how to leverage it to get the $node->type. The way you have it assumes that the path is based on the content type, which it often is on a Drupal site. Unfortunately, that isn't the case here for me. The paths are determined by the content owners.Ideally, I would like something along the lines of<?php $ctype = $node->type; // anything else return '<li class="'. $class. $ctype. '">'. $link. $menu. "</li>\n";?>I'm just not sure how to bring the node object into the theme_menu_item function.
Marc
My previous untested code doesn't actually work.The paths are stored in the url_alias table, so you can query that table to get the node ID, use that ID to load the node, and then get the type from $node->type. This is still untested code, but the individual parts definitely work:$path = db_fetch_object(db_query("SELECT src FROM url_alias WHERE dst = '%s'", $link['href']));$path_parts = explode('/', $path->src);$node = node_load($path_parts[1]);
Scott Reynen

related questions