You are here

hs_menu.module in Hierarchical Select 6.3

Same filename and directory in other branches
  1. 5.3 modules/hs_menu.module
  2. 7.3 modules/hs_menu.module

Implementation of the Hierarchical Select API for the Menu module.

File

modules/hs_menu.module
View source
<?php

/**
 * @file
 * Implementation of the Hierarchical Select API for the Menu module.
 */

//----------------------------------------------------------------------------

// Drupal core hooks.

/**
 * Implementation of hook_menu().
 */
function hs_menu_menu() {
  $items['admin/settings/hierarchical_select/menu'] = array(
    'title' => 'Menu',
    'description' => 'Hierarchical Select configuration for Menu',
    'access arguments' => array(
      'administer site configuration',
    ),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'hs_menu_admin_settings',
    ),
    'type' => MENU_LOCAL_TASK,
  );
  return $items;
}

/**
 * Implementation of hook_form_alter().
 */
function hs_menu_form_alter(&$form, $form_state, $form_id) {
  if (isset($form['type']['#value']) && $form['type']['#value'] . '_node_form' == $form_id && isset($form['menu']['parent']) && $form['menu']['#access']) {
    unset($form['menu']['parent']['#options']);
    $form['menu']['parent']['#type'] = 'hierarchical_select';
    _hs_menu_apply_config($form['menu']['parent'], NULL);
  }
  if ($form_id == 'menu_edit_item') {
    unset($form['menu']['parent']['#options']);
    $form['menu']['parent']['#type'] = 'hierarchical_select';
    _hs_menu_apply_config($form['menu']['parent'], array(
      $form['menu']['#item']['menu_name'],
      $form['menu']['#item']['mlid'],
    ));
  }
}

//----------------------------------------------------------------------------

// Menu callbacks.

/**
 * Form definition; admin settings.
 */
function hs_menu_admin_settings() {
  $form['hs_menu_resizable'] = array(
    '#type' => 'radios',
    '#title' => t('Resizable'),
    '#description' => t("When enabled, a handle appears below the Hierarchical Select to allow\n      the user to dynamically resize it. Double clicking will toggle between\n      the smallest and a sane 'big size'."),
    '#options' => array(
      0 => t('Disabled'),
      1 => t('Enabled'),
    ),
    '#default_value' => variable_get('hs_menu_resizable', 1),
  );
  return system_settings_form($form);
}

//----------------------------------------------------------------------------

// Hierarchical Select hooks.

/**
 * Implementation of hook_hierarchical_select_params().
 */
function hs_menu_hierarchical_select_params() {
  $params = array(
    'exclude',
  );
  return $params;
}

/**
 * Implementation of hook_hierarchical_select_root_level().
 */
function hs_menu_hierarchical_select_root_level($params) {
  $menus = array();
  $result = db_query("SELECT menu_name, title FROM {menu_custom} ORDER BY title");
  while ($menu = db_fetch_object($result)) {
    $menus[$menu->menu_name . ':0'] = $menu->title;
  }
  return $menus;
}

/**
 * Implementation of hook_hierarchical_select_children().
 */
function hs_menu_hierarchical_select_children($parent, $params) {
  $children = array();
  list($menu_name, $plid) = explode(':', $parent);
  $tree = menu_tree_all_data($menu_name, NULL);
  return _hs_menu_children($tree, $menu_name, $plid, $params['exclude']);
}

/**
 * Implementation of hook_hierarchical_select_lineage().
 */
function hs_menu_hierarchical_select_lineage($item, $params) {
  $lineage = array(
    $item,
  );
  list($menu_name, $mlid) = explode(':', $item);

  // If the initial mlid is zero, then this is the root level, so we don't
  // have to get the lineage.
  if ($mlid > 0) {

    // Prepend each parent mlid (i.e. plid) to the lineage.
    do {
      $plid = db_result(db_query("SELECT plid FROM {menu_links} WHERE mlid = %d", $mlid));
      array_unshift($lineage, "{$menu_name}:{$plid}");
      $mlid = $plid;
    } while ($plid > 0);
  }
  return $lineage;
}

/**
 * Implementation of hook_hierarchical_select_valid_item().
 */
function hs_menu_hierarchical_select_valid_item($item, $params) {
  $parts = explode(':', $item);
  $valid = TRUE;
  for ($i = 1; $i < count($parts); $i++) {
    $valid = $valid && is_numeric($parts[$i]);
  }

  // Ensure that this isn't the excluded menu link.
  $valid = $valid && $item != $params['exclude'][0] . $params['exclude'][1];
  return $valid;
}

/**
 * Implementation of hook_hierarchical_select_item_get_label().
 */
function hs_menu_hierarchical_select_item_get_label($item, $params) {
  static $labels = array();
  $parts = explode(':', $item);
  if (count($parts) == 1) {

    // Get the menu name.
    $menu_name = $parts[0];
    $labels[$item] = t(db_result(db_query("SELECT title FROM {menu_custom} WHERE menu_name = '%s'", $menu_name)));
  }
  else {

    // Get the menu link title.
    $mlid = end($parts);
    $menu_link = menu_link_load($mlid);
    $labels[$item] = $menu_link['title'];
  }
  return $labels[$item];
}

/**
 * Implementation of hook_hierarchical_select_implementation_info().
 */
function hs_menu_hierarchical_select_implementation_info() {
  return array(
    'hierarchy type' => t('Menu'),
    'entity type' => t('N/A'),
  );
}

//----------------------------------------------------------------------------

// Private functions.

/**
 * Recursive helper function for hs_menu_hierarchical_select_children().
 */
function _hs_menu_children($tree, $menu_name, $plid = 0, $exclude = FALSE) {
  $children = array();
  foreach ($tree as $data) {
    if ($data['link']['plid'] == $plid && $data['link']['hidden'] >= 0) {
      if ($exclude && $data['link']['menu_name'] === $exclude[0] && $data['link']['mlid'] == $exclude[1]) {
        continue;
      }
      $title = truncate_utf8($data['link']['title'], 30, TRUE, FALSE);
      if ($data['link']['hidden']) {
        $title .= ' (' . t('disabled') . ')';
      }
      $children[$menu_name . ':' . $data['link']['mlid']] = $title;
      if ($data['below']) {
        $children += _hs_menu_children($data['below'], $menu_name, $plid, $exclude);
      }
    }
    elseif ($data['below']) {
      $children += _hs_menu_children($data['below'], $menu_name, $plid, $exclude);
    }
  }
  return $children;
}

/**
 * Helper function to apply the HS config to a form item.
 */
function _hs_menu_apply_config(&$form, $exclude) {
  $form['#config'] = array(
    'module' => 'hs_menu',
    'params' => array(
      'exclude' => $exclude,
    ),
    'save_lineage' => 0,
    'enforce_deepest' => 0,
    'resizable' => variable_get('hs_menu_resizable', 1),
    'level_labels' => array(
      'status' => 0,
    ),
    'dropbox' => array(
      'status' => 0,
    ),
    'editability' => array(
      'status' => 0,
    ),
  );
}

Functions

Namesort descending Description
hs_menu_admin_settings Form definition; admin settings.
hs_menu_form_alter Implementation of hook_form_alter().
hs_menu_hierarchical_select_children Implementation of hook_hierarchical_select_children().
hs_menu_hierarchical_select_implementation_info Implementation of hook_hierarchical_select_implementation_info().
hs_menu_hierarchical_select_item_get_label Implementation of hook_hierarchical_select_item_get_label().
hs_menu_hierarchical_select_lineage Implementation of hook_hierarchical_select_lineage().
hs_menu_hierarchical_select_params Implementation of hook_hierarchical_select_params().
hs_menu_hierarchical_select_root_level Implementation of hook_hierarchical_select_root_level().
hs_menu_hierarchical_select_valid_item Implementation of hook_hierarchical_select_valid_item().
hs_menu_menu Implementation of hook_menu().
_hs_menu_apply_config Helper function to apply the HS config to a form item.
_hs_menu_children Recursive helper function for hs_menu_hierarchical_select_children().