You are here

nice_menus.module in Nice Menus 5

File

nice_menus.module
View source
<?php

/*
 By Jake Gordon (jakeg)
 Module to enable CSS dropdoen and flyout menus.
 Modifications and help by Simon Rawson, Addison Berry and Chad Phillips.
*/

/**
 * Implementation of hook_help().
 */
function nice_menus_help($section) {
  switch ($section) {
    case 'admin/settings/modules#description':
      $output = t('Make drop down css/javascript menus for site navigation and admin menus.');
      break;
    case 'admin/settings/nice_menus':
      $output = t('<p>This is a simple module that enables the site to have drop down css/javascript menus for site navigation and admin navigation.</p><p>Remember to activate and configure the menus in !link</p>', array(
        '!link' => l('admin/build/block', 'admin/build/block'),
      ));
      break;
  }
  return $output;
}

/**
 * Implementation of hook_form_alter().
 */
function nice_menus_form_alter($form_id, &$form) {
  switch ($form_id) {
    case 'system_theme_settings':

      // This is a global setting, so only insert the field
      // on the global settings page.
      if (arg(4)) {
        return;
      }

      // Have to add a custom submit handler since this form doesn't use
      // the standard system submit handler.
      $form['#submit'] += array(
        'nice_menus_system_theme_settings_submit' => array(),
      );

      // Add global theme setting for a custom CSS file.
      $form['nice_menus_custom_css'] = array(
        '#type' => 'textfield',
        '#title' => t('Path to custom Nice Menus CSS file'),
        '#description' => t('To override the default Nice Menus CSS layout, enter the path to your custom CSS file.  It should be a relative path from the root of your Drupal install (e.g. sites/all/themes/example/mymenu.css).'),
        '#default_value' => variable_get('nice_menus_custom_css', ''),
        // Field appears below submit buttons without this -- yucky.
        '#weight' => 0,
      );
      break;
  }
}

/**
 * Records the nice menu custom CSS file per theme.
 */
function nice_menus_system_theme_settings_submit($form_id, $form_values) {
  variable_set('nice_menus_custom_css', $form_values['nice_menus_custom_css']);
}

/**
 * Implemention of hook_menu().
 */
function nice_menus_menu($may_cache) {
  if (!$may_cache) {

    // Add JavaScript, if enabled.
    if (variable_get('nice_menus_ie', 1) == 1) {
      drupal_add_js(drupal_get_path('module', 'nice_menus') . '/nice_menus.js');
    }

    // Add main CSS functionality.
    drupal_add_css(drupal_get_path('module', 'nice_menus') . '/nice_menus.css');

    // Add custom CSS layout if specified.
    if ($custom = variable_get('nice_menus_custom_css', '')) {
      drupal_add_css($custom);
    }
    else {
      drupal_add_css(drupal_get_path('module', 'nice_menus') . '/nice_menus_default.css');
    }
  }
  else {
    $items[] = array(
      'path' => 'admin/settings/nice_menus',
      'title' => t('Nice Menus'),
      'description' => t('Configure Nice Menus.'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'nice_menus_admin_settings',
      ),
      'access' => user_access('administer site configuration'),
      'type' => MENU_NORMAL_ITEM,
    );
  }
  return $items;
}

/**
 * Settings form as implemented by hook_menu
 */
function nice_menus_admin_settings() {
  $form['nice_menus_number'] = array(
    '#type' => 'select',
    '#title' => t('Number of Nice Menus'),
    '#description' => t('The total number of independent nice menus (blocks) you want.'),
    '#default_value' => variable_get('nice_menus_number', '2'),
    '#options' => drupal_map_assoc(array(
      0,
      1,
      2,
      3,
      4,
      5,
      6,
      7,
      8,
      9,
      10,
    )),
  );
  $form['nice_menus_ie'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable IE support'),
    '#description' => t('This will add necessary JavaScript for Nice Menus to work properly in Internet Explorer.'),
    '#default_value' => variable_get('nice_menus_ie', 1),
  );
  return system_settings_form($form);
}

/**
 * Implementation of hook_block().
 */
function nice_menus_block($op = 'list', $delta = 0, $edit = array()) {
  global $user;
  switch ($op) {
    case 'list':
      for ($i = 1; $i <= variable_get('nice_menus_number', '2'); $i++) {
        $blocks[$i]['info'] = variable_get('nice_menus_name_' . $i, 'Nice Menu ' . $i) . ' (Nice Menu)';
      }
      return $blocks;
      break;
    case 'configure':
      $form['nice_menus_name_' . $delta] = array(
        '#type' => 'textfield',
        '#title' => t('Menu Name'),
        '#default_value' => variable_get('nice_menus_name_' . $delta, 'Nice Menu ' . $delta),
      );
      $form['nice_menus_menu_' . $delta] = array(
        '#type' => 'select',
        '#title' => t('Source Menu Tree'),
        '#description' => t('The menu tree from which to show a nice menu.'),
        '#default_value' => variable_get('nice_menus_menu_' . $delta, '1'),
        '#options' => menu_parent_options(0, 0),
      );
      $form['nice_menus_type_' . $delta] = array(
        '#type' => 'select',
        '#title' => t('Menu Style'),
        '#description' => t('right: menu items are listed on top of each other and expand to the right') . '<br />' . t('left: menu items are listed on top of each other and expand to the left') . '<br />' . t('down: menu items are listed side by side and expand down'),
        '#default_value' => variable_get('nice_menus_type_' . $delta, 'right'),
        '#options' => drupal_map_assoc(array(
          'right',
          'left',
          'down',
        )),
      );
      return $form;
      break;
    case 'save':
      variable_set('nice_menus_name_' . $delta, $edit['nice_menus_name_' . $delta]);
      variable_set('nice_menus_menu_' . $delta, $edit['nice_menus_menu_' . $delta]);
      variable_set('nice_menus_type_' . $delta, $edit['nice_menus_type_' . $delta]);
      break;
    case 'view':

      // Build the nice menu for the block.
      $pid = variable_get('nice_menus_menu_' . $delta, '1');
      $direction = variable_get('nice_menus_type_' . $delta, 'right');
      if ($output = theme('nice_menu', $delta, $pid, $direction)) {
        $block['content'] = $output['content'];
        if (variable_get('nice_menus_type_' . $delta, 'right') == 'down') {
          $class = 'nice-menu-hide-title';
        }
        else {
          $class = 'nice-menu-show-title';
        }

        // If we're building the navigation block
        // use the same block title logic as menu module.
        if ($output['subject'] == t('Navigation') && $user->uid) {
          $subject = $user->name;
        }
        else {
          $subject = $output['subject'];
        }
        $block['subject'] = '<span class="' . $class . '">' . $subject . '</span>';
      }
      else {
        $block['content'] = false;
      }
      return $block;
      break;
  }
}

/**
 * Builds the inner portion of a nice menu.
 *
 * @param $pid
 *   The parent menu ID from which to build the items.
 * @param $menu
 *   Optional. A custom menu array to use for theming --
 *   it should have the same structure as that returned by menu_get_menu().
 * @return
 *   An HTML string of properly nested nice menu lists.
 */
function theme_nice_menu_tree($pid = 1, $menu = NULL) {
  $menu = isset($menu) ? $menu : menu_get_menu();
  $output['content'] = '';
  $output['subject'] = check_plain($menu['items'][$pid]['title']);
  if ($menu['visible'][$pid]['children']) {

    // Build class name based on menu path
    // e.g. to give each menu item individual style.
    foreach ($menu['visible'][$pid]['children'] as $mid) {

      // Strip funny symbols
      $clean_path = str_replace(array(
        'http://',
        'www',
        '<',
        '>',
        '&',
        '=',
        '?',
        ':',
      ), '', $menu['items'][$mid]['path']);

      // Convert slashes to dashes
      $clean_path = str_replace('/', '-', $clean_path);
      $path_class = 'menu-path-' . $clean_path;
      if (count($menu['visible'][$mid]['children']) > 0) {
        $output['content'] .= '<li id="menu-' . $mid . '" class="menuparent ' . $path_class . '">' . menu_item_link($mid);
        $output['content'] .= '<ul>';
        $tmp = theme('nice_menu_tree', $mid);
        $output['content'] .= $tmp['content'];
        $output['content'] .= "</ul>\n";
        $output['content'] .= "</li>\n";
      }
      else {
        $output['content'] .= '<li id="menu-' . $mid . '" class="' . $path_class . '">' . menu_item_link($mid) . '</li>' . "\n";
      }
    }
  }
  return $output;
}

/**
 * General theming function to allow any menu tree to be themed 
 * as a nice menu.
 *
 * @param $id
 *   The nice menu ID.
 * @param $pid
 *   The parent menu ID from which to build the nice menu
 * @param $direction
 *   Optional. The direction the menu expands. Default is 'right'.
 * @param $menu
 *   Optional. A custom menu array to use for theming --
 *   it should have the same structure
 *   as that returned by menu_get_menu(). Default is the standard menu tree.
 * @return
 *   An HTML string of nice menu links.
 */
function theme_nice_menu($id, $pid, $direction = 'right', $menu = NULL) {
  $output = array();
  if ($menu_tree = theme('nice_menu_tree', $pid, $menu)) {
    if ($menu_tree['content']) {
      $output['content'] = '<ul class="nice-menu nice-menu-' . $direction . '" id="nice-menu-' . $id . '">' . $menu_tree['content'] . '</ul>' . "\n";
      $output['subject'] = $menu_tree['subject'];
    }
  }
  return $output;
}

/**
 * Theme primary links as nice menus
 *
 * @param $direction
 *   Optional. The direction the menu expands. Default is 'down'.
 * @param $menu
 *   Optional. A custom menu array to use for theming --
 *   it should have the same structure
 *   as that returned by menu_get_menu(). Default is the standard menu tree.
 * @return
 *   An HTML string of nice menu primary links.
 */
function theme_nice_menu_primary_links($direction = 'down', $menu = NULL) {
  $pid = variable_get('menu_primary_menu', 0);
  $output = theme('nice_menu', 'primary', $pid, $direction, $menu);
  return $output['content'];
}

Functions

Namesort descending Description
nice_menus_admin_settings Settings form as implemented by hook_menu
nice_menus_block Implementation of hook_block().
nice_menus_form_alter Implementation of hook_form_alter().
nice_menus_help Implementation of hook_help().
nice_menus_menu Implemention of hook_menu().
nice_menus_system_theme_settings_submit Records the nice menu custom CSS file per theme.
theme_nice_menu General theming function to allow any menu tree to be themed as a nice menu.
theme_nice_menu_primary_links Theme primary links as nice menus
theme_nice_menu_tree Builds the inner portion of a nice menu.