mpac.module 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. /**
  3. * @file
  4. * Find node paths on menu item creation via autocomplete.
  5. */
  6. /**
  7. * Implement hook_menu().
  8. *
  9. * @return An array of menu items.
  10. */
  11. function mpac_menu() {
  12. $items = array();
  13. $items['mpac/autocomplete'] = array(
  14. 'title' => 'Multi path autocomplete',
  15. 'description' => 'Autocomplete callback for Multi path autocomplete',
  16. 'page callback' => 'mpac_autocomplete',
  17. 'access callback' => 'user_access',
  18. 'access arguments' => array('access content'),
  19. 'type' => MENU_CALLBACK,
  20. );
  21. $items['admin/config/search/mpac'] = array(
  22. 'title' => 'Multi path autocomplete',
  23. 'description' => 'Administer settings for Multi path autocomplete',
  24. 'page callback' => 'drupal_get_form',
  25. 'page arguments' => array('mpac_settings_form'),
  26. 'access callback' => 'user_access',
  27. 'access arguments' => array('administer site configuration'),
  28. 'file' => 'mpac.admin.inc',
  29. );
  30. return $items;
  31. }
  32. /**
  33. * Find nodes, shortcuts and URL aliases based on title.
  34. *
  35. * @return
  36. * List of elements with path as key.
  37. */
  38. function mpac_autocomplete() {
  39. $args = arg();
  40. // Strip off the first 2 arguments ("mpac" and "autocomplete").
  41. $args = array_slice($args, 2);
  42. // Get the type of autocomplete we would like to display results for.
  43. $type = array_shift($args);
  44. // Validate type.
  45. if (!in_array($type, array('alias', 'menu', 'shortcut'))) {
  46. // The given type is not supported.
  47. return;
  48. }
  49. if (count($args) == 0) {
  50. // No more arguments so there is nothing to do for MPAC.
  51. return;
  52. }
  53. // Prepare the search query (if an argument contains a forward slash it has
  54. // been splitted into two arguments so join it once again).
  55. $query = implode('/', $args);
  56. $matches = array();
  57. $title = filter_xss_admin($query);
  58. // Get a list of all nodes where the title matches the given string.
  59. $matches = _mpac_get_matches_for_nodes($title);
  60. if (($type == 'menu') && module_exists('path')) {
  61. // Get a list of all URL aliases where the destination matches the given
  62. // string.
  63. $matches = array_merge($matches, _mpac_get_matches_for_aliases($title));
  64. }
  65. if (($type == 'shortcut') && module_exists('shortcut')) {
  66. // Get a list of menu items containing the given string in their title.
  67. $matches = array_merge($matches, _mpac_get_matches_for_shortcuts($title));
  68. if (module_exists('path')) {
  69. // Get matching url aliases.
  70. $matches = array_merge($matches, _mpac_get_matches_for_aliases($title));
  71. }
  72. }
  73. // Sort result list.
  74. natsort($matches);
  75. // Allow other modules to add paths.
  76. drupal_alter('mpac_autocomplete_paths', $matches, $title);
  77. // Reduce results to maximum number of items.
  78. $matches = array_slice($matches, 0, variable_get('mpac_max_items', 20));
  79. // Print results.
  80. drupal_json_output($matches);
  81. }
  82. /**
  83. * Implement hook_form_FORM_ID_alter().
  84. *
  85. * Change path field to autocomplete field.
  86. */
  87. function mpac_form_menu_edit_item_alter(&$form, &$form_state) {
  88. if ($form['link_path']['#type'] == 'textfield') {
  89. $form['link_path']['#autocomplete_path'] = 'mpac/autocomplete/menu';
  90. $form['link_path']['#description'] .= '<br />' . t("You may enter the title of the node you'd like to link to to get a list of all possible matches.");
  91. if (module_exists('path')) {
  92. $form['link_path']['#description'] .= ' ' . t('Matches marked with %marker are URL aliases.', array('%marker' => t('*')));
  93. }
  94. }
  95. }
  96. /**
  97. * Implement hook_form_FORM_ID_alter().
  98. *
  99. * Change path field for URL aliases to autocomplete field.
  100. */
  101. function mpac_form_path_admin_form_alter(&$form, &$form_state) {
  102. if ($form['source']['#type'] == 'textfield') {
  103. $form['source']['#autocomplete_path'] = 'mpac/autocomplete/alias';
  104. $form['source']['#description'] .= '<br />' . t("You may enter the title of the node you'd like to link to to get a list of all possible matches.");
  105. }
  106. }
  107. /**
  108. * Implement hook_form_FORM_ID_alter().
  109. *
  110. * Change path field for new shortcuts to autocomplete field.
  111. */
  112. function mpac_form_shortcut_link_add_alter(&$form, &$form_state) {
  113. if ($form['shortcut_link']['link_path']['#type'] == 'textfield') {
  114. $form['shortcut_link']['link_path']['#autocomplete_path'] = 'mpac/autocomplete/shortcut';
  115. $form['shortcut_link']['link_path']['#description'] .= '<br />' . t("You may enter the title of a node or a page you'd like to link to to get a list of all possible matches.");
  116. if (module_exists('path')) {
  117. $form['shortcut_link']['link_path']['#description'] .= ' ' . t('Matches marked with %marker are URL aliases.', array('%marker' => t('*')));
  118. }
  119. }
  120. }
  121. /**
  122. * Implement hook_form_FORM_ID_alter().
  123. *
  124. * Change path field for existing shortcuts to autocomplete field.
  125. */
  126. function mpac_form_shortcut_link_edit_alter(&$form, &$form_state) {
  127. if ($form['shortcut_link']['link_path']['#type'] == 'textfield') {
  128. $form['shortcut_link']['link_path']['#autocomplete_path'] = 'mpac/autocomplete/shortcut';
  129. $form['shortcut_link']['link_path']['#description'] .= '<br />' . t("You may enter the title of a node or a page you'd like to link to to get a list of all possible matches.");
  130. if (module_exists('path')) {
  131. $form['shortcut_link']['link_path']['#description'] .= ' ' . t('Matches marked with %marker are URL aliases.', array('%marker' => t('*')));
  132. }
  133. }
  134. }
  135. /**
  136. * Helper function to find all nodes containing the given title.
  137. *
  138. * @param $title
  139. * The title to search for.
  140. */
  141. function _mpac_get_matches_for_nodes($title = '') {
  142. $matches = array();
  143. if ($title == '') {
  144. return $matches;
  145. }
  146. $query = db_select('node', 'n')->extend('PagerDefault');
  147. $query->condition('title', '%' . $title . '%', 'LIKE');
  148. $result = $query
  149. ->fields('n')
  150. ->limit(variable_get('mpac_max_items', 20))
  151. ->execute();
  152. foreach ($result as $node) {
  153. // Add node path and title to list.
  154. if (node_access('view', $node) && $node->status) {
  155. $matches['node/' . $node->nid] = check_plain($node->title);
  156. }
  157. }
  158. return $matches;
  159. }
  160. /**
  161. * Helper function to find all url aliases containing the given title in their
  162. * destination string.
  163. *
  164. * @param $title
  165. * The title to search for.
  166. */
  167. function _mpac_get_matches_for_aliases($title = '') {
  168. $matches = array();
  169. if ($title == '') {
  170. return $matches;
  171. }
  172. $query = db_select('url_alias')->extend('PagerDefault');
  173. $query->condition('alias', '%' . $title . '%', 'LIKE');
  174. $result = $query
  175. ->fields('url_alias')
  176. ->limit(variable_get('mpac_max_items', 20))
  177. ->execute();
  178. foreach ($result as $alias) {
  179. $matches[$alias->source] = check_plain($alias->alias) . t('*');
  180. }
  181. return $matches;
  182. }
  183. /**
  184. * Helper function to find all menu items containing the given string in their
  185. * title.
  186. *
  187. * @param $title
  188. * The title to search for.
  189. */
  190. function _mpac_get_matches_for_shortcuts($title = '') {
  191. $matches = array();
  192. if ($title == '') {
  193. return $matches;
  194. }
  195. $query = db_select('menu_router')->extend('PagerDefault');
  196. $query->condition('title', '%' . $title . '%', 'LIKE');
  197. $query->where('path NOT LIKE :percent', array(':percent' => '%\%%'));
  198. $result = $query
  199. ->fields('menu_router')
  200. ->limit(variable_get('mpac_max_items', 20))
  201. ->execute();
  202. foreach ($result as $menu_item) {
  203. $matches[$menu_item->path] = check_plain($menu_item->title);
  204. }
  205. return $matches;
  206. }