You are here

function menu_token_translated_menu_link_alter in Menu Token 7

Same name and namespace in other branches
  1. 6 menu_token.module \menu_token_translated_menu_link_alter()

Implements hook_translated_menu_link_alter().

File

./menu_token.module, line 102
Main module file for the Menu Token module.

Code

function menu_token_translated_menu_link_alter(&$item, $map) {
  global $menu_admin;

  // Check whether we should replace the path.
  if (empty($menu_admin) && isset($item['options']['menu_token_link_path'])) {
    if (_menu_token_is_called_from_features()) {
      return;
    }
    $info = token_get_info();
    $data = array();
    $token_entity_mapping = token_get_entity_mapping();

    // Load data objects used when replacing link.
    if (isset($item['options']['menu_token_data'])) {
      foreach ($item['options']['menu_token_data'] as $type => $values) {
        if (!empty($info['types'][$type]) && ($handler = menu_token_get_handler($values['plugin']))) {
          $values['options']['_type'] = $token_entity_mapping[$type];
          if ($object = $handler
            ->object_load($values['options'])) {
            $data[$type] = $object;
          }
        }
      }
    }
    $options['title_localize'] = !empty($item['options']['menu_token_options']['title_localize']) ? TRUE : FALSE;
    $options['clear'] = !empty($item['options']['menu_token_options']['clear']) ? TRUE : FALSE;
    $options['sanitize'] = FALSE;

    // Store the UUID link path.
    $item['options']['menu_token_link_uuid'] = $item['link_path'];
    $original_title = $item['title'];

    // If item is generated by admin menu module, tokens should not be replaced
    // and indicator that tokens are used should be shown.
    $item['title'] = token_replace($item['title'], $data, $options);
    $url = token_replace($item['options']['menu_token_link_path'], $data, $options);

    // Localize resulting title (for security, only when it has no tokens)
    if ($options['title_localize'] && $original_title == $item['title']) {
      $item['title'] = t(filter_xss($item['title']));
    }

    // Make sure aliases are proccessed correctly
    $url = trim($url, '/');
    $url = drupal_get_normal_path($url);

    // 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 (url_is_external($url)) {
      $item['href'] = $item['link_path'] = $url;
      return;
    }

    // Split url into parts and save in proper format.
    $url_parts = parse_url($url);
    $url = !empty($url_parts['path']) ? $url_parts['path'] : '';
    $item['href'] = $item['link_path'] = $item['router_path'] = $url;
    if (isset($url_parts['query'])) {
      $query = drupal_get_query_array($url_parts['query']);
      $item['localized_options']['query'] = $item['options']['query'] = $query;
    }
    if (isset($url_parts['fragment'])) {
      $item['localized_options']['fragment'] = $item['options']['fragment'] = $url_parts['fragment'];
    }
    if (!isset($item['localized_options'])) {
      $item['localized_options'] = array();
    }
    if ($url == '<front>') {
      $url = drupal_get_normal_path(variable_get('site_frontpage', 'node'));
    }

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