Add absolute link menu item to user menu in Drupal

First of all, user menu (local tasks) in Drupal cannot be edited via admin. To add items to the menu you need to create new module and implement hook_menu() in it.
The following example shows how to do it in Drupal 6:

function yourmodule_menu() {
  $items['http://some.url'] = array(
    'tab_root'	=> 'user',
    'tab_parent'	=> 'user',
    'title' => 'External URL',
    'page callback' => 'user_page',
    'access callback' => 'user_is_anonymous',
    'type' => MENU_LOCAL_TASK,
    'weight'	=> 5
  return $items;

Change ‘access callback’ to whatever function (or constant) you want, but don’t forget to set ‘page callback’. It is being checked when building menu structure even if we don’t have any page callback actually.

Thursday, May 21st, 2009

Theming language selector in Drupal

Unfortunately Drupal (as in version 6.5) doesn’t allow easy theming for language selector. However it’s still possible with theme function overriding. Look at the code.

function yourtheme_links($links, $attributes = array('class' => 'links')) {
	list(,$first_link) = each($links);
	if (($first_link['attributes']['class'] == 'language-link')) {
		global $language;
		$new_links = array();
		$num_links = count($links);
		$i = 0;
		foreach ($links as $link) {
			$i ++;
			$link['title'] = '<span class="bg">' . $link['title'] . '</span>';
			$link['html']	= true;
			if ($link['language']->language == $language->language)
				$link['attributes']['class'] = 'selected';
			$new_links[] = $link;
			if ($i < $num_links)
				$new_links[] = array(
					'title'	=> '|',
					'attributes'	=> array('class'	=> 'separator')
		$links = $new_links;
	return theme_links($links, $attributes);

It adds ‘selected’ class to the current language (which I think logical) and adds separator element between language links. You can customize them as you wish.

Thursday, November 13th, 2008