You are here

special_menu_items.module in Special menu items 7.2

The main file for the Special menu items module.

Module to enable placeholder or separator menu items.Placeholder is a menu item which is actually not a link. Something like this is useful with drop down menus where we want to have a parent link which is actually not linking to a page but which is just acting as a parent and grouping some children below it. A separator menu item is something like "-------" which is also not linking anywhere but merely a mean to structure menus.

Written by Tamir Al Zoubi and Karim Djelid

File

special_menu_items.module
View source
<?php

/**
 * @file
 * The main file for the Special menu items module.
 *
 * Module to enable placeholder or separator menu items.Placeholder is a menu
 *  item which is actually not a link. Something like this is useful with drop
 *  down menus where we want to have a parent link which is actually not linking
 *  to a page but which is just acting as a parent and grouping some children
 *  below it. A separator menu item is something like "-------" which is also
 *  not linking anywhere but merely a mean to structure menus.
 *
 *  Written by Tamir Al Zoubi and Karim Djelid
 *  - Servit Open Source Solutions - www.servit.ch.
 */

/**
 * Implements hook_menu().
 */
function special_menu_items_menu() {
  $items['<nolink>'] = array(
    'page callback' => 'special_menu_items_page_callback',
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );
  $items['<separator>'] = array(
    'page callback' => 'special_menu_items_page_callback',
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );
  $items['admin/config/system/special_menu_items'] = array(
    'title' => 'Special Menu Items',
    'description' => 'Configure Special Menu Items.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'special_menu_items_admin_settings_form',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}

/**
 * Page callback for special menu links.
 *
 * Simply returns MENU_NOT_FOUND. We can't use drupal_not_found() as page
 * callback since this would lead to calling drupal_deliver_page() twice.
 *
 * @return int
 *   The MENU_NOT_FOUND status code.
 */
function special_menu_items_page_callback() {
  return MENU_NOT_FOUND;
}

/**
 * Override of theme_link().
 *
 * This function will render link if it is "nolink" or "separator".
 * Otherwise it will call originally overwritten menu_item_link function.
 */
function special_menu_items_link(array $variables) {
  if (in_array($variables['path'], array(
    '<nolink>',
    '<separator>',
  ))) {
    switch ($variables['path']) {
      case '<nolink>':
        $tag = variable_get('special_menu_items_nolink_tag', '<span>');
        $title = $variables['options']['html'] ? $variables['text'] : check_plain($variables['text']);
        $variables['options']['attributes']['class'][] = 'nolink';
        $variables['options']['attributes']['tabindex'][] = '0';
        break;
      case '<separator>':
        $tag = variable_get('special_menu_items_separator_tag', '<span>');
        $title = variable_get('special_menu_items_separator_value', '<hr>');
        $variables['options']['attributes']['class'][] = 'separator';
        break;
    }
    $attributes = drupal_attributes($variables['options']['attributes']);
    if ($tag != '<a>') {

      // <a> tags can have these but a <span> cannot, so we remove them.
      foreach (array(
        'accesskey',
        'target',
        'rel',
        'name',
      ) as $attribute) {
        $attributes = preg_replace("/ {$attribute}=\".*\"/i", "", $attributes);
      }
    }
    return special_menu_items_render_menu_item($tag, $title, $attributes);
  }

  // Call the original theme function for normal menu link.
  $theme_registry = theme_get_registry();
  $function = $theme_registry['special_menu_items_link_default']['function'];
  return $function($variables);
}

/**
 * Returns menu item rendered.
 */
function special_menu_items_render_menu_item($tag, $value, $attrs = array()) {

  // $attrs may be a string already or an array.
  if (is_array($attrs)) {
    $attrs = drupal_attributes($attrs);
  }
  $length = strlen($tag);
  if ($tag[0] == '<' && $tag[$length - 1] == '>') {
    $tag = substr($tag, 1, $length - 2);
  }
  $closingtag = explode(' ', $tag, 2);
  $closingtag = '</' . $closingtag[0] . '>';
  $tag = '<' . $tag . $attrs . '>';
  return $tag . $value . $closingtag;
}

/**
 * Implements hook_theme_registry_alter().
 *
 * We replace theme_menu_item_link with our own function.
 */
function special_menu_items_theme_registry_alter(&$registry) {

  // Save previous value from registry in case another theme overwrites
  // menu_item_link.
  $registry['special_menu_items_link_default'] = $registry['link'];
  $registry['link']['function'] = 'special_menu_items_link';
}

/**
 * Implements hook_form_FROM_ID_alter().
 *
 * Description changed, added nolink and separator as path types.
 */
function special_menu_items_form_menu_edit_item_alter(&$form, &$form_state) {

  // Some menu items have a pre-defined path which cannot be modified hence no
  // default_value.
  if (isset($form['link_path']['#default_value'])) {
    $default_value = $form['link_path']['#default_value'];
    if (preg_match('/^<nolink>\\/[0-9]+$/', $default_value)) {
      $default_value = '<nolink>';
    }
    elseif (preg_match('/^<separator>\\/[0-9]+$/', $default_value)) {
      $default_value = '<separator>';
    }
    $form['link_path']['#default_value'] = $default_value;
    $form['link_path']['#description'] .= ' ' . t('Enter "%nolink" to generate non-linkable item, enter "%separator" to generate separator item.', array(
      '%nolink' => '<nolink>',
      '%separator' => '<separator>',
    ));
  }
}

/**
 * Special Menu Items admin settings form.
 *
 * @return array
 *   The settings form used by Special Menu Items.
 */
function special_menu_items_admin_settings_form() {
  $form['special_menu_items_nolink_tag'] = array(
    '#type' => 'textfield',
    '#title' => t('HTML tag for "nolink"'),
    '#description' => t('By default, Special Menu Items will use a span tag for the nolink menu item. Here you can specify your own tag.'),
    '#default_value' => variable_get('special_menu_items_nolink_tag', '<span>'),
  );
  $form['special_menu_items_separator_tag'] = array(
    '#type' => 'textfield',
    '#title' => t('HTML tag for "separator"'),
    '#description' => t('By default, Special Menu Items will use a span tag for the separator menu item. Here you can specify your own tag.'),
    '#default_value' => variable_get('special_menu_items_separator_tag', '<span>'),
  );
  $form['special_menu_items_separator_value'] = array(
    '#type' => 'textfield',
    '#title' => t('Value to be displayed for the "separator"'),
    '#description' => t('By default, Special Menu Items will use a "&lt;hr&gt;" value for the separator. You can specify your own value for the separator.'),
    '#default_value' => variable_get('special_menu_items_separator_value', '<hr>'),
  );
  return system_settings_form($form);
}

/**
 * Implements hook_menu_link_update()
 */

/*
function special_menu_items_menu_link_update($link) {
// do all links in db
global $db_type;
if ($db_type == 'pgsql') {
db_query("UPDATE {menu_links} SET link_path=link_path||'/'||mlid
WHERE (link_path='<nolink>' OR link_path='<separator>') AND hidden != -1");
}
else {
db_query("UPDATE {menu_links} SET link_path=CONCAT(CONCAT(link_path,'/'),mlid)
WHERE (link_path='<nolink>' OR link_path='<separator>') AND hidden!=-1");
}
}
*
*/

Functions

Namesort descending Description
special_menu_items_admin_settings_form Special Menu Items admin settings form.
special_menu_items_form_menu_edit_item_alter Implements hook_form_FROM_ID_alter().
special_menu_items_link Override of theme_link().
special_menu_items_menu Implements hook_menu().
special_menu_items_page_callback Page callback for special menu links.
special_menu_items_render_menu_item Returns menu item rendered.
special_menu_items_theme_registry_alter Implements hook_theme_registry_alter().