ctools_automodal.module 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. /**
  3. * Implements hook_module_implements_alter().
  4. */
  5. function ctools_automodal_module_implements_alter(&$implementations, $hook) {
  6. if ($hook == 'menu_alter') {
  7. // Try to ensure that ctools_automodal_menu_alter() gets invoked last.
  8. $group = $implementations['ctools_automodal'];
  9. unset($implementations['ctools_automodal']);
  10. $implementations['ctools_automodal'] = $group;
  11. }
  12. }
  13. /**
  14. * Implements hook_menu_alter().
  15. */
  16. function ctools_automodal_menu_alter(&$items) {
  17. $modal_paths = array();
  18. foreach ($items as $path => $item) {
  19. if (!empty($item['modal']) && strpos($path, '%ctools_js') === FALSE) {
  20. if ($item['page callback'] == 'drupal_get_form') {
  21. $items["$path/%ctools_js"] = $item;
  22. $items["$path/%ctools_js"]['page callback'] = 'ctools_automodal_get_form';
  23. $items["$path/%ctools_js"]['page arguments'][] = substr_count($path, '/') + 1;
  24. $items["$path/%ctools_js"]['type'] = MENU_CALLBACK;
  25. }
  26. else {
  27. $items["$path/%ctools_js"] = $item + array('page arguments' => array());
  28. $items["$path/%ctools_js"]['page callback'] = 'ctools_automodal_get_page';
  29. array_unshift($items["$path/%ctools_js"]['page arguments'], $item['page callback']);
  30. $items["$path/%ctools_js"]['page arguments'][] = substr_count($path, '/') + 1;
  31. $items["$path/%ctools_js"]['type'] = MENU_CALLBACK;
  32. }
  33. $modal_paths[] = preg_replace('/%[^\/]*/', '*', $path);
  34. }
  35. }
  36. variable_set('ctools_automodal_paths', $modal_paths);
  37. }
  38. /**
  39. * Check if an internal Drupal path should be converted to a modal link.
  40. */
  41. function ctools_automodal_is_path_modal($path) {
  42. static $modal_paths_regex;
  43. if (!isset($modal_paths_regex)) {
  44. $modal_paths = variable_get('ctools_automodal_paths', array());
  45. foreach ($modal_paths as &$modal_path) {
  46. $modal_path = preg_quote($modal_path, '/');
  47. $modal_path = str_replace('\*', '.*', $modal_path);
  48. }
  49. $modal_paths_regex = '/^(' . implode('|', $modal_paths) . ')$/';
  50. }
  51. return (bool) preg_match($modal_paths_regex, $path);
  52. }
  53. /**
  54. * Implements hook_preprocess_link().
  55. */
  56. function ctools_automodal_preprocess_link(&$variables) {
  57. static $ctools_modal_included = FALSE;
  58. if (ctools_automodal_is_path_modal($variables['path'])) {
  59. $item = menu_get_item($variables['path']);
  60. // Only process the modal includes once per request.
  61. if (!$ctools_modal_included) {
  62. ctools_include('modal');
  63. ctools_modal_add_js();
  64. $ctools_modal_included = TRUE;
  65. }
  66. if(!is_array($variables['options']['attributes']['class'])){
  67. if($variables['options']['attributes']['class']){
  68. $variables['options']['attributes']['class'] = array($variables['options']['attributes']['class']);
  69. }else{
  70. $variables['options']['attributes']['class'] = array();
  71. }
  72. }
  73. $variables['options']['attributes']['class'][] = 'ctools-use-modal';
  74. if (strpos($variables['path'], 'nojs') === FALSE) {
  75. $variables['path'] .= '/nojs';
  76. }
  77. }
  78. }
  79. /**
  80. * Implements hook_preprocess_menu_local_action().
  81. */
  82. function ctools_automodal_preprocess_menu_local_action(&$variables) {
  83. // Prepare the link array in the way that the hook_preprocess_link() expects.
  84. $link = array(
  85. 'path' => &$variables['element']['#link']['href'],
  86. 'options' => &$variables['element']['#link']['localized_options'],
  87. 'text' => &$variables['element']['#link']['title'],
  88. );
  89. ctools_automodal_preprocess_link($link);
  90. }
  91. /**
  92. * Dirty, dirty function to fix the 'current path' global on modal pages.
  93. */
  94. function ctools_automodal_fix_get_q() {
  95. $path = current_path();
  96. // Pop off the /js or /nojs suffix to the path.
  97. $path = substr($path, 0, strrpos($path, '/'));
  98. // @todo Shower multiple times after modifing global variables.
  99. $_GET['q'] = $path;
  100. }
  101. /**
  102. * Display a Drupal form using CTools modal or normal page display.
  103. */
  104. function ctools_automodal_get_form() {
  105. $args = func_get_args();
  106. $form_id = array_shift($args);
  107. $js = $ajax = array_pop($args);
  108. ctools_automodal_fix_get_q();
  109. if ($ajax) {
  110. ctools_include('modal');
  111. ctools_include('ajax');
  112. $form_state = array(
  113. 'ajax' => $ajax,
  114. 'build_info' => array('args' => $args),
  115. );
  116. $commands = ctools_modal_form_wrapper($form_id, $form_state);
  117. if (empty($commands)) {
  118. $commands[] = ctools_modal_command_loading();
  119. if (!empty($_GET['destination'])) {
  120. $commands[] = ctools_ajax_command_redirect($_GET['destination']);
  121. }
  122. }
  123. print ajax_render($commands);
  124. exit();
  125. }
  126. else {
  127. array_unshift($args, $form_id);
  128. return call_user_func_array('drupal_get_form', $args);
  129. }
  130. }
  131. /**
  132. * Display a normal Drupal page using CTools modal.
  133. */
  134. function ctools_automodal_get_page() {
  135. $args = func_get_args();
  136. $callback = array_shift($args);
  137. $ajax = array_pop($args);
  138. ctools_automodal_fix_get_q();
  139. if (function_exists($callback)) {
  140. $output = call_user_func_array($callback, $args);
  141. if ($ajax) {
  142. ctools_include('modal');
  143. ctools_include('ajax');
  144. $commands = ctools_automodal_page_render($output);
  145. if (empty($commands)) {
  146. $commands[] = ctools_modal_command_loading();
  147. if (!empty($_GET['destination'])) {
  148. $commands[] = ctools_ajax_command_redirect($_GET['destination']);
  149. }
  150. }
  151. print ajax_render($commands);
  152. exit();
  153. }
  154. else {
  155. return $output;
  156. }
  157. }
  158. else {
  159. return MENU_NOT_FOUND;
  160. }
  161. }
  162. /**
  163. * Render a page into an AJAX display.
  164. */
  165. function ctools_automodal_page_render($output) {
  166. if (is_array($output)) {
  167. $output = drupal_render($output);
  168. }
  169. $title = drupal_get_title();
  170. // If there are messages for the form, render them.
  171. if ($messages = theme('status_messages')) {
  172. $output = '<div class="messages">' . $messages . '</div>' . $output;
  173. }
  174. $commands = array();
  175. $commands[] = ctools_modal_command_display($title, $output);
  176. return $commands;
  177. }