autocomplete_deluxe.module 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. <?php
  2. /**
  3. * @file
  4. * Define enhanced autocomplete wdiget.
  5. */
  6. /**
  7. * Implements hook_field_info().
  8. */
  9. function autocomplete_deluxe_field_widget_info() {
  10. return array(
  11. 'autocomplete_deluxe_taxonomy' => array(
  12. 'label' => t('Autocomplete Deluxe'),
  13. 'field types' => array('taxonomy_term_reference'),
  14. 'settings' => array(
  15. 'size' => 60,
  16. 'autocomplete_deluxe_path' => 'autocomplete_deluxe/taxonomy',
  17. ),
  18. 'behaviors' => array(
  19. 'multiple values' => FIELD_BEHAVIOR_CUSTOM,
  20. ),
  21. ),
  22. 'autocomplete_deluxe_list' => array(
  23. 'label' => t('Autocomplete Deluxe'),
  24. 'field types' => array('list', 'list_text', 'list_number'),
  25. 'settings' => array(
  26. 'size' => 60,
  27. ),
  28. 'behaviors' => array(
  29. 'multiple values' => FIELD_BEHAVIOR_CUSTOM,
  30. ),
  31. ),
  32. );
  33. }
  34. /**
  35. * Custom taxonomy callback, which also accepts an empty string search.
  36. */
  37. function taxonomy_autocomplete_deluxe($field_name, $tags_typed = '') {
  38. $field = field_info_field($field_name);
  39. // The user enters a comma-separated list of tags. We only autocomplete the last tag.
  40. $tags_typed = drupal_explode_tags($tags_typed);
  41. $tag_last = drupal_strtolower(array_pop($tags_typed));
  42. $matches = array();
  43. // Part of the criteria for the query come from the field's own settings.
  44. $vids = array();
  45. $vocabularies = taxonomy_vocabulary_get_names();
  46. foreach ($field['settings']['allowed_values'] as $tree) {
  47. $vids[] = $vocabularies[$tree['vocabulary']]->vid;
  48. }
  49. $query = db_select('taxonomy_term_data', 't');
  50. $query->addTag('translatable');
  51. $query->addTag('term_access');
  52. $query->addTag('autocomplete_deluxe_taxonomy_autocomplete');
  53. $query->addMetadata('field_name', $field_name);
  54. if ($tag_last != '') {
  55. // Do not select already entered terms.
  56. if (!empty($tags_typed)) {
  57. $query->condition('t.name', $tags_typed, 'NOT IN');
  58. }
  59. // Select rows that match by term name.
  60. $tags_return = $query
  61. ->fields('t', array('tid', 'name'))
  62. ->condition('t.vid', $vids)
  63. ->condition('t.name', '%' . db_like($tag_last) . '%', 'LIKE')
  64. ->execute()
  65. ->fetchAllKeyed();
  66. }
  67. else {
  68. $tags_return = $query
  69. ->fields('t', array('tid', 'name'))
  70. ->condition('t.vid', $vids)
  71. ->execute()
  72. ->fetchAllKeyed();
  73. }
  74. $prefix = count($tags_typed) ? implode(', ', $tags_typed) . ', ' : '';
  75. $term_matches = array();
  76. foreach ($tags_return as $tid => $name) {
  77. $n = $name;
  78. // Term names containing commas or quotes must be wrapped in quotes.
  79. if (strpos($name, ',') !== FALSE || strpos($name, '"') !== FALSE) {
  80. $n = '"' . str_replace('"', '""', $name) . '"';
  81. }
  82. else {
  83. $term_matches[$prefix . $n] = check_plain($name);
  84. }
  85. }
  86. drupal_json_output($term_matches);
  87. }
  88. /**
  89. * Returns all allowed terms for a field without any prefix.
  90. */
  91. function autocomplete_deluxe_allowed_terms($field) {
  92. $options = array();
  93. foreach ($field['settings']['allowed_values'] as $tree) {
  94. if ($vocabulary = taxonomy_vocabulary_machine_name_load($tree['vocabulary'])) {
  95. if ($terms = taxonomy_get_tree($vocabulary->vid, $tree['parent'])) {
  96. foreach ($terms as $term) {
  97. $options[$term->name] = $term->name;
  98. }
  99. }
  100. }
  101. }
  102. return $options;
  103. }
  104. /**
  105. * Implements hook_field_widget_settings_form().
  106. */
  107. function autocomplete_deluxe_field_widget_settings_form($field, $instance) {
  108. $widget = $instance['widget'];
  109. $settings = $widget['settings'];
  110. $type = str_replace('autocomplete_deluxe_', '', $widget['type']);
  111. $form['size'] = array(
  112. '#type' => 'textfield',
  113. '#title' => t('Size of textfield'),
  114. '#default_value' => isset($settings['size']) ? $settings['size'] : 6,
  115. '#element_validate' => array('_element_validate_integer_positive'),
  116. '#required' => TRUE,
  117. );
  118. return $form;
  119. }
  120. /**
  121. * Implodes the tags from the taxonomy module.
  122. *
  123. * This function is essentially the same as axonomy_implode_tags, with the
  124. * difference, that it uses only a comma instead of a comma and a space to
  125. * implode the tags. It will help keep problems with delimiters to a minimum.
  126. */
  127. function autocomplete_deluxe_taxonomy_implode_tags($tags, $vid = NULL) {
  128. $typed_tags = array();
  129. foreach ($tags as $tag) {
  130. // Extract terms belonging to the vocabulary in question.
  131. if (!isset($vid) || $tag->vid == $vid) {
  132. // Make sure we have a completed loaded taxonomy term.
  133. if (isset($tag->name)) {
  134. // Commas and quotes in tag names are special cases, so encode 'em.
  135. if (strpos($tag->name, ',') !== FALSE || strpos($tag->name, '"') !== FALSE) {
  136. $tag->name = '"' . str_replace('"', '""', $tag->name) . '"';
  137. }
  138. $typed_tags[] = $tag->name;
  139. }
  140. }
  141. }
  142. return implode(',', $typed_tags);
  143. }
  144. /**
  145. * Implements hook_field_widget_error().
  146. */
  147. function autocomplete_deluxe_field_widget_error($element, $error, $form, &$form_state) {
  148. form_error($element, $error['message']);
  149. }
  150. /**
  151. * Implements hook_field_widget_form().
  152. */
  153. function autocomplete_deluxe_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  154. $type = str_replace('autocomplete_deluxe_', '', $instance['widget']['type']);
  155. $element += array(
  156. '#type' => 'autocomplete_deluxe',
  157. '#size' => $instance['widget']['settings']['size'],
  158. );
  159. $multiple = $field['cardinality'] > 1 || $field['cardinality'] == FIELD_CARDINALITY_UNLIMITED ? TRUE : FALSE;
  160. switch ($type) {
  161. case 'taxonomy':
  162. $tags = array();
  163. foreach ($items as $item) {
  164. $tags[$item['tid']] = isset($item['taxonomy_term']) ? $item['taxonomy_term'] : taxonomy_term_load($item['tid']);
  165. }
  166. $element['#element_validate'] = array('taxonomy_autocomplete_validate');
  167. if (isset($instance['widget']['settings']['content_taxonomy_autocomplete_new_terms']) && $instance['widget']['settings']['content_taxonomy_autocomplete_new_terms'] == 'deny') {
  168. $options = autocomplete_deluxe_allowed_terms($field);
  169. $default_values = array();
  170. foreach ($tags as $tag) {
  171. $default_values[$tag->name] = $tag->name;
  172. }
  173. $element += array(
  174. '#autocomplete_options' => $options,
  175. '#multiple' => $multiple && count($options) > 1,
  176. '#default_value' => $default_values,
  177. );
  178. }
  179. else {
  180. $element += array(
  181. '#multiple' => $multiple,
  182. '#autocomplete_deluxe_path' => url($instance['widget']['settings']['autocomplete_deluxe_path'] . '/' . $field['field_name'], array('absolute' => TRUE)),
  183. '#default_value' => autocomplete_deluxe_taxonomy_implode_tags($tags),
  184. );
  185. }
  186. $form_state['autocomplete_deluxe']['terms'] = TRUE;
  187. break;
  188. case 'list':
  189. $value_key = key($field['columns']);
  190. $type = str_replace('options_', '', $instance['widget']['type']);
  191. $required = $element['#required'];
  192. $has_value = isset($items[0][$value_key]);
  193. $properties = _options_properties($type, $multiple, $required, $has_value);
  194. // Prepare the list of options.
  195. $options = _options_get_options($field, $instance, $properties);
  196. $default_value = _options_storage_to_form($items, $options, $value_key, $properties);
  197. $element += array(
  198. '#autocomplete_options' => $options,
  199. '#autocomplete_min_length' => 0,
  200. '#multiple' => $multiple && count($options) > 1,
  201. '#value_key' => $value_key,
  202. '#element_validate' => array('options_field_widget_validate'),
  203. '#properties' => $properties,
  204. '#default_value' => $default_value,
  205. );
  206. break;
  207. };
  208. return $element;
  209. }
  210. /**
  211. * Generates the basic form elements and javascript settings.
  212. */
  213. function autocomplete_deluxe_element_process($element) {
  214. $element['#attached'] = array(
  215. 'library' => array(array('system', 'ui.autocomplete'), array('system', 'ui.button')),
  216. 'js' => array(drupal_get_path('module', 'autocomplete_deluxe') . '/autocomplete_deluxe.js'),
  217. 'css' => array(drupal_get_path('module', 'autocomplete_deluxe') . '/autocomplete_deluxe.css'),
  218. );
  219. // Workaround for problems with jquery css in seven theme.
  220. if ($GLOBALS['theme'] == 'seven') {
  221. $element['#attached']['css'][] = drupal_get_path('module', 'autocomplete_deluxe') . '/autocomplete_deluxe.seven.css';
  222. }
  223. $html_id = drupal_html_id('autocomplete-deluxe-input');
  224. $element['#after_build'][] = 'autocomplete_deluxe_after_build';
  225. // Set default options for multiple values.
  226. $element['#multiple'] = isset($element['#multiple']) ? $element['#multiple'] : FALSE;
  227. $element['#prefix'] = '<div class="clearfix autocomplete-container">';
  228. $element['#suffix'] = '</div>';
  229. $element['values'] = array(
  230. '#type' => 'container',
  231. '#attributes' => array(
  232. 'class' => array('autocomplete-deluxe-values'),
  233. ),
  234. );
  235. $element['textfield'] = array(
  236. '#type' => 'textfield',
  237. '#size' => isset($element['#size']) ? $element['#size'] : '',
  238. '#required' => isset($element['#required']) ? $element['#required'] : '',
  239. '#attributes' => array('class' => array('autocomplete-deluxe-form', 'form-autocomplete'), 'id' => array($html_id)),
  240. '#default_value' => '',
  241. );
  242. $js_settings['autocomplete_deluxe'][$html_id] = array(
  243. 'input_id' => $html_id,
  244. 'min_length' => isset($element['#autocomplete_min_length']) ? $element['#autocomplete_min_length'] : 0,
  245. 'multiple' => $element['#multiple'],
  246. );
  247. if (isset($element['#autocomplete_deluxe_path'])) {
  248. if ($element['#multiple']) {
  249. $element['value_field'] = array(
  250. '#type' => 'textfield',
  251. '#attributes' => array('class' => array('autocomplete-deluxe-value-field')),
  252. '#default_value' => isset($element['#default_value']) ? $element['#default_value'] : '',
  253. '#prefix' => '<div class="autocomplete-deluxe-value-container">',
  254. '#suffix' => '</div>',
  255. );
  256. $element['textfield']['#attributes']['style'] = array('display:none');
  257. }
  258. else {
  259. $element['textfield']['#default_value'] = isset($element['#default_value']) ? $element['#default_value'] : '';
  260. }
  261. $js_settings['autocomplete_deluxe'][$html_id] += array(
  262. 'type' => 'ajax',
  263. 'uri' => $element['#autocomplete_deluxe_path'],
  264. );
  265. }
  266. elseif (isset($element['#autocomplete_options'])) {
  267. $js_settings['autocomplete_deluxe'][$html_id] += array(
  268. 'type' => 'list',
  269. 'data' => $element['#autocomplete_options'],
  270. );
  271. $html_id_select = drupal_html_id('autocomplete-deluxe-input-select');
  272. $element['list_value'] = array(
  273. '#type' => 'select',
  274. '#options' => $element['#autocomplete_options'],
  275. '#multiple' => $element['#multiple'],
  276. '#attributes' => array('class' => array('autocomplete-deluxe-form', 'form-autocomplete'), 'id' => array($html_id_select)),
  277. '#default_value' => isset($element['#default_value']) ? $element['#default_value'] : '',
  278. );
  279. // Hide textfield so that, in case javascript is deactivated
  280. $element['textfield']['#attributes']['style'] = 'display: none;';
  281. $js_settings['autocomplete_deluxe'][$html_id]['select_input'] = $html_id_select;
  282. }
  283. else {
  284. // If there is no source (path or data), we don't want to add the js
  285. // settings and so the functions will be abborted.
  286. return $element;
  287. }
  288. $element['#attached']['js'][] = array('data' => $js_settings, 'type' => 'setting');
  289. return $element;
  290. }
  291. /**
  292. * Helper function to determine the value for a autocomplete deluxe form
  293. * element.
  294. */
  295. function autocomplete_deluxe_value(&$element, $input = FALSE, $form_state = NULL) {
  296. // This runs before child elements are processed, so we cannot calculate the
  297. // value here. But we have to make sure the value is an array, so the form
  298. // API is able to proccess the children to set their values in the array. Thus
  299. // once the form API has finished processing the element, the value is an
  300. // array containing the child element values. Then finally the after build
  301. // callback converts it back to the numeric value and sets that.
  302. return array();
  303. }
  304. /**
  305. * Helper function for array_filter.
  306. */
  307. function autocomplete_deluxe_value_filter($var) {
  308. $string = trim($var);
  309. if (empty($string)) {
  310. return FALSE;
  311. }
  312. else {
  313. return TRUE;
  314. }
  315. }
  316. /**
  317. * FAPI after build callback for the duration parameter type form.
  318. * Fixes up the form value by applying the multiplier.
  319. */
  320. function autocomplete_deluxe_after_build($element, &$form_state) {
  321. if (!isset($element['list_value'])) {
  322. $element['#value'] = isset($element['value_field']) ? $element['value_field']['#value'] : $element['textfield']['#value'];
  323. }
  324. else {
  325. if (!empty($form_state['autocomplete_deluxe']['terms']) && !empty($element['#multiple'])) {
  326. $element['#value'] = (!empty($element['list_value']['#value'])) ? implode(',', $element['list_value']['#value']) : "";
  327. }
  328. else {
  329. $element['#value'] = $element['list_value']['#value'];
  330. }
  331. }
  332. form_set_value($element, $element['#value'], $form_state);
  333. return $element;
  334. }
  335. /**
  336. * Implements hook_element_info().
  337. */
  338. function autocomplete_deluxe_element_info() {
  339. $types['autocomplete_deluxe'] = array(
  340. '#input' => TRUE,
  341. '#value_callback' => 'autocomplete_deluxe_value',
  342. '#pre_render' => array('form_pre_render_conditional_form_element'),
  343. '#process' => array('autocomplete_deluxe_element_process'),
  344. );
  345. return $types;
  346. }
  347. /**
  348. * Implements hook_menu().
  349. */
  350. function autocomplete_deluxe_menu() {
  351. $items['autocomplete_deluxe/taxonomy'] = array(
  352. 'title' => 'Autocomplete deluxe taxonomy',
  353. 'page callback' => 'taxonomy_autocomplete_deluxe',
  354. 'access arguments' => array('access content'),
  355. 'type' => MENU_CALLBACK,
  356. );
  357. return $items;
  358. }