menu_tree.inc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. /**
  3. * @file
  4. * Provides ctools integration for "Menu block" trees.
  5. *
  6. * "Menu block" trees operate with no context at all. They are basically the
  7. * same as a 'custom content' block, but not even that sophisticated.
  8. */
  9. /**
  10. * Plugins are described by creating a $plugin array which will be used
  11. * by the system that includes this file.
  12. */
  13. $plugin = array(
  14. // And this is just the administrative title.
  15. // All our callbacks are named according to the standard pattern and can be deduced.
  16. 'title' => t('Menu tree'),
  17. );
  18. /**
  19. * Supplies a list of menu tree content sub-types.
  20. */
  21. function menu_block_menu_tree_content_type_content_types() {
  22. $common_plugin = array(
  23. 'description' => t('A configurable tree provided by Menu block.'),
  24. 'icon' => 'icon_contrib_menu.png',
  25. 'category' => array(t('Menus'), -9),
  26. // The default context.
  27. 'defaults' => menu_block_get_config(),
  28. // JavaScript and CSS for the config form.
  29. 'js' => array(drupal_get_path('module', 'menu_block') . '/menu-block.js'),
  30. 'css' => array(drupal_get_path('module', 'menu_block') . '/menu-block-admin.css'),
  31. );
  32. unset($common_plugin['defaults']['delta']);
  33. $menus = menu_block_get_all_menus();
  34. $items = array();
  35. foreach ($menus as $menu_name => $title) {
  36. $items[$menu_name] = $common_plugin;
  37. $items[$menu_name]['title'] = t('%menu menu tree', array('%menu' => $title));
  38. $items[$menu_name]['defaults']['menu_name'] = $menu_name;
  39. $items[$menu_name]['menu_title'] = $title;
  40. // Custom icons/titles for some menus.
  41. switch ($menu_name) {
  42. case MENU_TREE__CURRENT_PAGE_MENU:
  43. $items[$menu_name]['title'] = t('menu tree of %menu', array('%menu' => 'the menu selected by the page'));
  44. break;
  45. case 'main-menu':
  46. case 'secondary-menu':
  47. $items[$menu_name]['icon'] = 'icon_contrib_main_menu.png';
  48. break;
  49. case 'management':
  50. $items[$menu_name]['icon'] = 'icon_contrib_management.png';
  51. break;
  52. }
  53. if (strpos($menu_name, 'book-toc-') === 0) {
  54. $items[$menu_name]['icon'] = 'icon_contrib_booknavigation.png';
  55. }
  56. }
  57. return $items;
  58. }
  59. /**
  60. * Renders a menu_tree content type based on the delta supplied in the configuration.
  61. *
  62. * @param $subtype
  63. * @param $conf
  64. * Configuration as done at admin time.
  65. * @param $args
  66. * @param $context
  67. * Context; in this case we don't have any.
  68. * @return
  69. * object An object with at least title and content members.
  70. */
  71. function menu_block_menu_tree_content_type_render($subtype, $conf, $args, $context) {
  72. // Ensure the delta is unique.
  73. $ids = &drupal_static(__FUNCTION__, array());
  74. if (empty($ids[$conf['menu_name']])) {
  75. $ids[$conf['menu_name']] = 0;
  76. }
  77. $delta = ++$ids[$conf['menu_name']];
  78. $conf['delta'] = 'ctools-' . $conf['menu_name'] . '-' . $delta;
  79. $tree = menu_tree_build($conf);
  80. $block = new stdClass();
  81. $block->subtype = $conf['menu_name'];
  82. $block->title = $tree['subject'];
  83. $block->title_array = $tree['subject_array'];
  84. $block->content = $tree['content'];
  85. return $block;
  86. }
  87. /**
  88. * 'Edit form' callback for the content type.
  89. */
  90. function menu_block_menu_tree_content_type_edit_form($form, &$form_state) {
  91. $conf = $form_state['conf'];
  92. // Load the standard config form.
  93. module_load_include('inc', 'menu_block', 'menu_block.admin');
  94. // Create a pseudo form state.
  95. $sub_form_state = array('values' => $conf);
  96. $form += menu_block_configure_form($form, $sub_form_state);
  97. // Hide the menu selector.
  98. $form['menu_name']['#type'] = 'hidden';
  99. // Set the options to a simple list of menu links for the configured menu.
  100. $menus = menu_block_get_all_menus();
  101. $form['parent_mlid']['#options'] = menu_parent_options(array($conf['menu_name'] => $menus[$conf['menu_name']]), array('mlid' => 0));
  102. // Hide the Parent item option for the special "active" menu.
  103. if ($conf['menu_name'] == MENU_TREE__CURRENT_PAGE_MENU) {
  104. $form['parent_mlid']['#type'] = 'hidden';
  105. }
  106. // Remove CSS class hooks for jQuery script on parent select.
  107. unset($form['parent_mlid']['#attributes']);
  108. return $form;
  109. }
  110. /**
  111. * Submit callback for content type editing form.
  112. */
  113. function menu_block_menu_tree_content_type_edit_form_submit(&$form, &$form_state) {
  114. foreach (array_keys($form_state['subtype']['defaults']) as $key) {
  115. if (!empty($form_state['values'][$key])) {
  116. $form_state['conf'][$key] = $form_state['values'][$key];
  117. }
  118. }
  119. }
  120. /**
  121. * Return the tree's title with an admin-sensitive prefix.
  122. */
  123. function menu_block_menu_tree_content_type_admin_title($subtype, $conf, $context = NULL) {
  124. if (!empty($conf['admin_title'])) {
  125. $output = filter_xss_admin($conf['admin_title']);
  126. }
  127. else {
  128. // Build the menu tree.
  129. module_load_include('inc', 'menu_block', 'menu_block.admin');
  130. $output = _menu_block_format_title($conf);
  131. }
  132. return $output;
  133. }
  134. /**
  135. * Callback to provide administrative info (the preview in panels when building a panel).
  136. */
  137. function menu_block_menu_tree_content_type_admin_info($subtype, $conf, $context = NULL) {
  138. // Ensure the delta is unique.
  139. $ids = &drupal_static(__FUNCTION__, array());
  140. if (empty($ids[$conf['menu_name']])) {
  141. $ids[$conf['menu_name']] = 0;
  142. }
  143. $delta = ++$ids[$conf['menu_name']];
  144. $conf['delta'] = 'ctools-' . $conf['menu_name'] . '-' . $delta;
  145. // Force the title to not be a link.
  146. $conf['title_link'] = 0;
  147. $tree = menu_tree_build($conf);
  148. $block = new stdClass();
  149. $block->subtype = $conf['menu_name'];
  150. $block->title = $tree['subject'];
  151. $block->content = $tree['content'];
  152. return $block;
  153. }