hs_menu.module

Tracking 5.x-3.x branch
  1. drupal
    1. 5 contributions/hierarchical_select/modules/hs_menu.module
    2. 6 contributions/hierarchical_select/modules/hs_menu.module

Implementation of the Hierarchical Select API for the Menu module.

Functions & methods

NameDescription
hs_menu_admin_settingsForm definition; admin settings.
hs_menu_form_alterImplementation of hook_form_alter().
hs_menu_hierarchical_select_childrenImplementation of hook_hierarchical_select_children().
hs_menu_hierarchical_select_implementation_infoImplementation of hook_hierarchical_select_implementation_info().
hs_menu_hierarchical_select_item_get_labelImplementation of hook_hierarchical_select_item_get_label().
hs_menu_hierarchical_select_lineageImplementation of hook_hierarchical_select_lineage().
hs_menu_hierarchical_select_paramsImplementation of hook_hierarchical_select_params().
hs_menu_hierarchical_select_root_levelImplementation of hook_hierarchical_select_root_level().
hs_menu_hierarchical_select_valid_itemImplementation of hook_hierarchical_select_valid_item().
hs_menu_menuImplementation of hook_menu().
_hs_menu_apply

File

View source
  1. <?php
  2. /**
  3. * @file
  4. * Implementation of the Hierarchical Select API for the Menu module.
  5. */
  6. //----------------------------------------------------------------------------
  7. // Drupal core hooks.
  8. /**
  9. * Implementation of hook_menu().
  10. */
  11. function hs_menu_menu($may_cache) {
  12. $items = array();
  13. if ($may_cache) {
  14. $items[] = array(
  15. 'path' => 'admin/settings/hierarchical_select/menu',
  16. 'title' => t('Menu'),
  17. 'description' => t('Hierarchical Select configuration for Menu'),
  18. 'callback' => 'drupal_get_form',
  19. 'callback arguments' => array('hs_menu_admin_settings'),
  20. 'type' => MENU_LOCAL_TASK,
  21. );
  22. }
  23. return $items;
  24. }
  25. /**
  26. * Implementation of hook_form_alter().
  27. */
  28. function hs_menu_form_alter($form_id, &$form) {
  29. if (isset($form['type']) && $form['type']['#value'] .'_node_form' == $form_id) {
  30. // Content authoring form settings: "Restrict parent items to" setting.
  31. $parent_mid = variable_get('menu_parent_items', 0);
  32. _hs_menu_apply($form['menu']['pid'], NULL, $parent_mid);
  33. }
  34. if ($form_id == 'menu_edit_item_form') {
  35. $mid = (isset($form['mid'])) ? $form['mid']['#value'] : NULL;
  36. _hs_menu_apply($form['pid'], $mid, NULL);
  37. }
  38. }
  39. //----------------------------------------------------------------------------
  40. // Menu callbacks.
  41. /**
  42. * Form definition; admin settings.
  43. */
  44. function hs_menu_admin_settings() {
  45. $form['hs_menu_resizable'] = array(
  46. '#type' => 'radios',
  47. '#title' => t('Resizable'),
  48. '#description' => t(
  49. "When enabled, a handle appears below the Hierarchical Select to allow
  50. the user to dynamically resize it. Double clicking will toggle between
  51. the smallest and a sane 'big size'."
  52. ),
  53. '#options' => array(
  54. 0 => t('Disabled'),
  55. 1 => t('Enabled'),
  56. ),
  57. '#default_value' => variable_get('hs_menu_resizable', 1),
  58. );
  59. return system_settings_form($form);
  60. }
  61. //----------------------------------------------------------------------------
  62. // Hierarchical Select hooks.
  63. /**
  64. * Implementation of hook_hierarchical_select_params().
  65. */
  66. function hs_menu_hierarchical_select_params() {
  67. $params = array(
  68. 'mid', // The mid of the given item. This item and its children will be excluded from the hierarchy.
  69. 'parent_mid', // The parent mid, if any. 0 for the entire hierarchy.
  70. );
  71. return $params;
  72. }
  73. /**
  74. * Implementation of hook_hierarchical_select_root_level().
  75. */
  76. function hs_menu_hierarchical_select_root_level($params) {
  77. return hs_menu_hierarchical_select_children(0, $params);
  78. }
  79. /**
  80. * Implementation of hook_hierarchical_select_children().
  81. */
  82. function hs_menu_hierarchical_select_children($parent, $params) {
  83. $children = array();
  84. $mid = $params['mid'];
  85. $pid = $parent;
  86. $parent_mid = $params['parent_mid'];
  87. if (!($parent_item = menu_get_item($pid))) {
  88. return $children;
  89. }
  90. // Stop processing if there aren't any children.
  91. else if (!isset($parent_item['children'])) {
  92. return $children;
  93. }
  94. foreach ($parent_item['children'] as $child_mid) {
  95. // Don't include the given item in the hierarchy!
  96. if ($child_mid == $params['mid']) {
  97. continue;
  98. }
  99. $child = menu_get_item($child_mid);
  100. if ($child['type'] & (MENU_MODIFIABLE_BY_ADMIN | MENU_IS_ROOT)) {
  101. $title = $child['title'];
  102. if (!($child['type'] & MENU_VISIBLE_IN_TREE)) {
  103. $title .= ' ('. t('disabled') .')';
  104. }
  105. $children[$child_mid] = $title;
  106. }
  107. }
  108. // If this is the root level, it might contain the menu to which menu
  109. // items are restricted. If it does contain it, remove all other menus.
  110. if (isset($children[$parent_mid])) {
  111. foreach ($children as $child_mid => $child) {
  112. if ($child_mid != $parent_mid) {
  113. unset($children[$child_mid]);
  114. }
  115. }
  116. }
  117. return $children;
  118. }
  119. /**
  120. * Implementation of hook_hierarchical_select_lineage().
  121. */
  122. function hs_menu_hierarchical_select_lineage($item, $params) {
  123. $lineage = array($item);
  124. while (TRUE) {
  125. $pid = db_result(db_query("SELECT pid FROM {menu} WHERE mid = %d", $item));
  126. // 0 is the root menu item, so if $pid == 0, the lineage is complete!
  127. if ($pid == 0) {
  128. break;
  129. }
  130. array_unshift($lineage, $pid);
  131. $item = $pid;
  132. }
  133. return $lineage;
  134. }
  135. /**
  136. * Implementation of hook_hierarchical_select_valid_item().
  137. */
  138. function hs_menu_hierarchical_select_valid_item($item, $params) {
  139. if (!is_numeric($item) || $item < 1) {
  140. return FALSE;
  141. }
  142. $type = db_result(db_query("SELECT type FROM {menu} WHERE mid = %d", $item));
  143. // mid 1 corresponds to the hardcoded "Navigation" menu.
  144. return (($item == 1) || ($type & (MENU_MODIFIABLE_BY_ADMIN | MENU_IS_ROOT)));
  145. }
  146. /**
  147. * Implementation of hook_hierarchical_select_item_get_label().
  148. */
  149. function hs_menu_hierarchical_select_item_get_label($item, $params) {
  150. static $labels = array();
  151. if (!isset($labels[$item])) {
  152. $labels[$item] = t(db_result(db_query("SELECT title FROM {menu} WHERE mid = %d", $item)));
  153. }
  154. return $labels[$item];
  155. }
  156. /**
  157. * Implementation of hook_hierarchical_select_implementation_info().
  158. */
  159. function hs_menu_hierarchical_select_implementation_info() {
  160. return array(
  161. 'hierarchy type' => t('Menu'),
  162. 'entity type' => t('N/A'),
  163. );
  164. }
  165. //----------------------------------------------------------------------------
  166. // Private functions.
  167. function _hs_menu_apply(&$form, $mid, $parent_mid) {
  168. unset($form['#options']);
  169. $form['#type'] = 'hierarchical_select';
  170. $form['#config'] = array(
  171. 'module' => 'hs_menu',
  172. 'params' => array(
  173. 'mid' => $mid,
  174. 'parent_mid' => $parent_mid,
  175. ),
  176. 'save_lineage' => 0,
  177. 'enforce_deepest' => 0,
  178. 'resizable' => variable_get('hs_menu_resizable', 1),
  179. 'level_labels' => array(
  180. 'status' => 0,
  181. ),
  182. 'dropbox' => array(
  183. 'status' => 0,
  184. ),
  185. 'editability' => array(
  186. 'status' => 0,
  187. ),
  188. );
  189. }