context_condition_menu.inc 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. * Expose menu items as a context condition.
  4. */
  5. class context_condition_menu extends context_condition {
  6. /**
  7. * Override of condition_values().
  8. */
  9. function condition_values() {
  10. if (module_exists('menu')) {
  11. $menus = menu_parent_options(menu_get_menus(), array('mlid' => 0));
  12. $root_menus = array();
  13. foreach ($menus as $key => $name) {
  14. $id = explode(':', $key);
  15. if ($id[1] == '0') {
  16. $root_menus[$id[0]] = $name;
  17. }
  18. else {
  19. $link = menu_link_load($id[1]);
  20. $identifier = $link['link_path'];
  21. $root_menu = $root_menus[$id[0]];
  22. while (isset($menus[$root_menu][$identifier])) {
  23. $identifier .= "'";
  24. }
  25. $menus[$root_menu][$identifier] = $name;
  26. }
  27. unset($menus[$key]);
  28. }
  29. array_unshift($menus, "-- " . t('None') . " --");
  30. }
  31. else {
  32. $menus = array();
  33. }
  34. return $menus;
  35. }
  36. /**
  37. * Override of condition_form().
  38. * Use multiselect widget.
  39. */
  40. function condition_form($context) {
  41. $form = parent::condition_form($context);
  42. $menu_count = count($form['#options'], COUNT_RECURSIVE);
  43. $form['#type'] = 'select';
  44. $form['#multiple'] = TRUE;
  45. $form['#attributes'] = array('size' => $menu_count > 20 ? 20 : $menu_count);
  46. return $form;
  47. }
  48. /**
  49. * Override of condition_form_submit().
  50. * Trim any identifier padding for non-unique path menu items.
  51. */
  52. function condition_form_submit($values) {
  53. // Trim any identifier padding for non-unique path menu items.
  54. $values = parent::condition_form_submit($values);
  55. $trimmed = array();
  56. foreach ($values as $key => $value) {
  57. $trimmed[trim($key, "'")] = trim($value, "'");
  58. }
  59. return $trimmed;
  60. }
  61. /**
  62. * Override of execute().
  63. */
  64. function execute() {
  65. if ($this->condition_used()) {
  66. $trail = menu_get_active_trail();
  67. foreach ($trail as $item) {
  68. if (!empty($item['href'])) {
  69. foreach ($this->get_contexts($item['href']) as $context) {
  70. $this->condition_met($context, $item['href']);
  71. }
  72. }
  73. }
  74. }
  75. }
  76. }