You are here

menu_token.module in Menu Token 6

File

menu_token.module
View source
<?php

module_load_include('inc', 'menu_token');
module_load_include('inc', 'menu_token', 'menu_token.admin');

/**
 * Implementation of hook_translated_menu_link().
 */
function menu_token_translated_menu_link_alter(&$item, $map) {
  $mlid = $item['mlid'];
  $menu_token_item = menu_token_get($mlid);

  // Check whether we should replace the path.
  if (isset($menu_token_item)) {

    // If item is generated by admin menu module, tokens should not be replaced and
    // indicator that tokens are used should be shown.
    if (_menu_token_menu_admin($item)) {
      $item['title'] .= theme('menu_token_uses_tokens');
      $item['localized_options']['html'] = TRUE;
    }
    else {

      // Replace with tokens.
      $item['title'] = token_replace($item['title'], 'global');
      $item['link_path'] = token_replace($menu_token_item['link_path'], 'global');
      $item['href'] = $item['link_path'];

      // Override active trail if showing front page but translated link is not to front page.
      // NOTE: This relies on any parent of a tokenised menu item having "option" flag "alter" set,
      // which is most easily achieved by setting it to use token translation but not specifying a token.
      // Otherwise parent does not get processed through this function and because its untranslated child has
      // an href of <front>, the menu system thinks it is part of the active trail to the front page.
      if (drupal_is_front_page() && $item['href'] != drupal_get_normal_path(variable_get('site_frontpage', 'node'))) {
        $item['in_active_trail'] = FALSE;
      }

      // Check whether path is external.
      if (menu_path_is_external($item['link_path'])) {
        return;
      }

      // Load menu_item and check access.
      if ($menu_item = menu_get_item($item['link_path'])) {
        $item['access'] = $menu_item['access'];
        return;
      }
      $item['access'] = FALSE;
    }
  }
}

/**
 * Implementation of hook_theme().
 */
function menu_token_theme() {
  return array(
    'menu_token_uses_tokens' => array(),
  );
}

/**
 * Theming "uses tokens" label.
 */
function theme_menu_token_uses_tokens() {
  drupal_add_css(drupal_get_path('module', 'menu_token') . '/menu_token.css');
  return '<span class="menu_token_uses_tokens">' . t(' uses tokens') . '</span>';
}

Functions

Namesort descending Description
menu_token_theme Implementation of hook_theme().
menu_token_translated_menu_link_alter Implementation of hook_translated_menu_link().
theme_menu_token_uses_tokens Theming "uses tokens" label.