menu.inc 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. * @file
  4. * General menu helper functions.
  5. */
  6. /**
  7. * Dynamically add a tab to the current path.
  8. *
  9. * This function is a simplified interface for adding tabs to the current path.
  10. * Some considerations when doing this:
  11. *
  12. * - First, if there is only 1 tab, Drupal will not show it. Therefore, if
  13. * you are only adding one tab, you should find a way to make sure there is
  14. * already tab, or instead add 2.
  15. * - Second, the caller is responsible for providing access control to these
  16. * links.
  17. *
  18. * @param $link
  19. * An array describing this link. It must contain:
  20. * - 'title': The printed title of the link.
  21. * - 'href': The path of the link. This is an argument to l() so it has all
  22. * of those features and limitations.
  23. * - 'options': Any options that go to l, including query, fragment and html
  24. * options necessary.
  25. * - 'weight': The weight to use in ordering the tabs.
  26. * - 'type': Optional. If set to MENU_DEFAULT_LOCAL_TASK this can be used to
  27. * add a fake 'default' local task, which is useful if you have to add
  28. * tabs to a page that has none.
  29. */
  30. function ctools_menu_add_tab($link = NULL) {
  31. $links = &drupal_static(__FUNCTION__, array());
  32. if (isset($link)) {
  33. $links[$link['href']] = $link;
  34. }
  35. return $links;
  36. }
  37. /**
  38. * Re-sort menu items after we have modified them.
  39. */
  40. function ctools_menu_sort($a, $b) {
  41. $a_weight = (is_array($a) && isset($a['#link']['weight'])) ? $a['#link']['weight'] : 0;
  42. $b_weight = (is_array($b) && isset($b['#link']['weight'])) ? $b['#link']['weight'] : 0;
  43. if ($a_weight == $b_weight) {
  44. $a_title = (is_array($a) && isset($a['#link']['title'])) ? $a['#link']['title'] : 0;
  45. $b_title = (is_array($b) && isset($b['#link']['title'])) ? $b['#link']['title'] : 0;
  46. if ($a_title == $b_title) {
  47. return 0;
  48. }
  49. return ($a_title < $b_title) ? -1 : 1;
  50. }
  51. return ($a_weight < $b_weight) ? -1 : 1;
  52. }
  53. function _ctools_menu_add_dynamic_items(&$data, &$router_item, &$root_path) {
  54. if ($additions = ctools_menu_add_tab()) {
  55. // If none of the static local tasks are active allow one of the dynamic
  56. // active tasks to be marked as such.
  57. $has_active = FALSE;
  58. if (!empty($data['tabs'][0]['output'])) {
  59. foreach ($data['tabs'][0]['output'] as $element) {
  60. if (!empty($element['#link']['#active'])) {
  61. $has_active = TRUE;
  62. }
  63. }
  64. }
  65. foreach ($additions as $addition) {
  66. $addition['localized_options'] = isset($addition['options']) ? $addition['options'] : array();
  67. if (isset($addition['type']) && $addition['type'] == MENU_LOCAL_ACTION) {
  68. $data['actions']['output'][] = array(
  69. '#theme' => 'menu_local_action',
  70. '#link' => $addition,
  71. );
  72. }
  73. else {
  74. $data['tabs'][0]['output'][] = array(
  75. '#theme' => 'menu_local_task',
  76. '#link' => $addition,
  77. '#active' => (!$has_active && $root_path === $addition['href']),
  78. );
  79. }
  80. }
  81. if (!empty($data['tabs'][0]['output'])) {
  82. uasort($data['tabs'][0]['output'], 'ctools_menu_sort');
  83. $data['tabs'][0]['count'] = count($data['tabs'][0]['output']);
  84. }
  85. if (!empty($data['actions']['output'])) {
  86. uasort($data['actions']['output'], 'ctools_menu_sort');
  87. $data['actions']['count'] = count($data['actions']['output']);
  88. }
  89. }
  90. }