content_taxonomy_autocomplete.module 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. /**
  3. * Implements hook_field_widget_info_alter().
  4. */
  5. function content_taxonomy_autocomplete_field_widget_info_alter(&$info) {
  6. // Add a setting for validating new terms.
  7. $ct_settings = array(
  8. 'content_taxonomy_autocomplete_new_terms' => 'allow',
  9. );
  10. $info['taxonomy_autocomplete']['settings'] += $ct_settings;
  11. // Add this to the Active Tags as well, if enabled.
  12. if (isset($info['active_tags']['settings'])) {
  13. $info['active_tags']['settings'] += $ct_settings;
  14. }
  15. // Add this to the autocomplete deluxe as well, if enabled.
  16. if (isset($info['autocomplete_deluxe_taxonomy']['settings'])) {
  17. $info['autocomplete_deluxe_taxonomy']['settings'] += $ct_settings;
  18. }
  19. // Add this to the entityreference autocomplete as well, if enabled.
  20. if (isset($info['entityreference_autocomplete']['settings'])) {
  21. $info['entityreference_autocomplete']['settings'] += $ct_settings;
  22. }
  23. // Add this to the entityreference autocomplete (tags) as well, if enabled.
  24. if (isset($info['entityreference_autocomplete_tags']['settings'])) {
  25. $info['entityreference_autocomplete_tags']['settings'] += $ct_settings;
  26. }
  27. }
  28. /**
  29. * Implements hook_form_ID_alter().
  30. */
  31. function content_taxonomy_autocomplete_form_field_ui_field_edit_form_alter(&$form, &$form_state, $form_id) {
  32. $field = $form['#field'];
  33. $instance = $form['#instance'];
  34. if ($instance['widget']['type'] == 'taxonomy_autocomplete'
  35. || $instance['widget']['type'] == 'active_tags_taxonomy_autocomplete'
  36. || $instance['widget']['type'] == 'autocomplete_deluxe_taxonomy'
  37. || $instance['widget']['type'] == 'entityreference_autocomplete'
  38. || $instance['widget']['type'] == 'entityreference_autocomplete_tags') {
  39. // Add a setting form for validating new terms.
  40. $options = array(
  41. 'allow' => t('Allow and insert new terms'),
  42. 'moderate' => t('Allow new terms and insert them into a separate vocabulary'),
  43. 'deny' => t('Deny any new terms'),
  44. );
  45. $form['instance']['widget']['settings']['content_taxonomy_autocomplete_new_terms'] = array(
  46. '#type' => 'radios',
  47. '#title' => t('Autocomplete settings'),
  48. '#options' => $options,
  49. '#default_value' => isset($instance['widget']['settings']['content_taxonomy_autocomplete_new_terms']) ? $instance['widget']['settings']['content_taxonomy_autocomplete_new_terms'] : 'allow',
  50. '#description' => t('If option 2 is selected, re-save this settings form and afterwards select a second vocabulary for new terms in the field settings. In case the Autocomplete Deluxe widget is used, new terms can be hidden from the suggestion list.'),
  51. );
  52. }
  53. // Show form for second vocabulary.
  54. if (($instance['widget']['type'] == 'taxonomy_autocomplete'
  55. || $instance['widget']['type'] == 'active_tags_taxonomy_autocomplete'
  56. || $instance['widget']['type'] == 'autocomplete_deluxe_taxonomy'
  57. || $instance['widget']['type'] == 'entityreference_autocomplete'
  58. || $instance['widget']['type'] == 'entityreference_autocomplete_tags')
  59. && isset($form[$instance['field_name']])
  60. && isset($instance['widget']['settings']['content_taxonomy_autocomplete_new_terms'])) {
  61. // Initialize settings, if not set.
  62. if (!isset($field['settings']['allowed_values'][1])) {
  63. $field['settings']['allowed_values'][1] = array(
  64. 'vocabulary' => $field['settings']['allowed_values'][0]['vocabulary'],
  65. 'parent' => 0,
  66. );
  67. }
  68. $vocabularies = taxonomy_get_vocabularies();
  69. $options = array();
  70. $options[''] = '---';
  71. foreach ($vocabularies as $vocabulary) {
  72. $options[$vocabulary->machine_name] = $vocabulary->name;
  73. }
  74. $form['field']['settings']['allowed_values'][1]['vocabulary'] = array(
  75. '#type' => 'select',
  76. '#title' => t('Vocabulary for new terms'),
  77. '#default_value' => isset($field['settings']['allowed_values'][1]['vocabulary']) ? $field['settings']['allowed_values'][1]['vocabulary'] : '',
  78. '#options' => $options,
  79. '#description' => t('New terms form autocompletes will be inserted in this vocabulary.'),
  80. //'#disabled' => $has_data, //todo
  81. );
  82. $form['field']['settings']['allowed_values'][1]['parent'] = array(
  83. '#type' => 'value',
  84. '#value' => $field['settings']['allowed_values'][1]['parent'],
  85. );
  86. if($instance['widget']['type'] == 'autocomplete_deluxe_taxonomy') {
  87. // todo: should this setting be added field_info_alter?
  88. $form['field']['settings']['allowed_values'][1]['content_taxonomy_ignore_in_suggestions'] = array(
  89. '#type' => 'checkbox',
  90. '#title' => t('Ignore vocabulary above for suggested terms.'),
  91. '#default_value' => isset($field['settings']['allowed_values'][1]['content_taxonomy_ignore_in_suggestions']) ? $field['settings']['allowed_values'][1]['content_taxonomy_ignore_in_suggestions'] : FALSE,
  92. );
  93. }
  94. }
  95. }
  96. /**
  97. * Implements hook_field_attach_form().
  98. */
  99. function content_taxonomy_autocomplete_field_attach_form($entity_type, $entity, &$form, &$form_state, $langcode) {
  100. // Add validation function to taxonomy_autocompletes, if necessary.
  101. $instances = field_info_instances($form['#entity_type'], $form['#bundle']);
  102. foreach ($instances as $instance) {
  103. if (($instance['widget']['type'] == 'taxonomy_autocomplete'
  104. || $instance['widget']['type'] == 'autocomplete_deluxe_taxonomy'
  105. || $instance['widget']['type'] == 'entityreference_autocomplete'
  106. || $instance['widget']['type'] == 'entityreference_autocomplete_tags')
  107. && isset($form[$instance['field_name']])
  108. && isset($instance['widget']['settings']['content_taxonomy_autocomplete_new_terms'])) {
  109. // Use the language that is used in this form (which doesn't necessarily
  110. // be the default language in $langcode).
  111. $lang_key = $form[$instance['field_name']]['#language'];
  112. if ($instance['widget']['settings']['content_taxonomy_autocomplete_new_terms'] == 'moderate') {
  113. $form[$instance['field_name']][$lang_key]['#element_validate'][] = 'content_taxonomy_autocomplete_validate_moderate_new_terms';
  114. }
  115. else if ($instance['widget']['settings']['content_taxonomy_autocomplete_new_terms'] == 'deny') {
  116. $form[$instance['field_name']][$lang_key]['#element_validate'][] = 'content_taxonomy_autocomplete_validate_deny_new_terms';
  117. }
  118. }
  119. }
  120. }
  121. /**
  122. * Form element validate handler for taxonomy term autocomplete element, which denies any new terms.
  123. */
  124. function content_taxonomy_autocomplete_validate_deny_new_terms($element, &$form_state, $form) {
  125. $values = $form_state['values'];
  126. if (!empty($element['#array_parents'])) {
  127. foreach ($element['#array_parents'] as $parent) {
  128. $values = $values[$parent];
  129. }
  130. }
  131. foreach ($values as $delta => $value) {
  132. if (isset($value['tid']) && $value['tid'] == 'autocreate') {
  133. form_error($element, t('%name: new terms are not allowed. Please choose from the suggested options.', array('%name' => $element['#title'])));
  134. }
  135. }
  136. }
  137. /**
  138. * Form element validate handler for taxonomy term autocomplete element, which inserts new terms into a separate voc.
  139. */
  140. function content_taxonomy_autocomplete_validate_moderate_new_terms($element, &$form_state) {
  141. // taxonomy_field_validate() is invoked before.
  142. // Reset vocabulary id for new terms.
  143. $field = field_widget_field($element, $form_state);
  144. $values =& $form_state['values'];
  145. foreach ($element['#array_parents'] as $parent) {
  146. $values =& $values[$parent];
  147. }
  148. if (isset($field['settings']['allowed_values'][1])) {
  149. if ($voc2 = taxonomy_vocabulary_machine_name_load($field['settings']['allowed_values'][1]['vocabulary'])) {
  150. foreach ($values as $delta => $value) {
  151. if ($value['tid'] == 'autocreate') {
  152. $values[$delta]['vid'] = $voc2->vid;
  153. $values[$delta]['vocabulary_machine_name'] = $voc2->machine_name;
  154. }
  155. }
  156. }
  157. }
  158. }
  159. /**
  160. * Implements hook_query_TAG_alter().
  161. */
  162. function content_taxonomy_autocomplete_query_autocomplete_deluxe_taxonomy_autocomplete_alter(QueryAlterableInterface $query) {
  163. $field = field_info_field($query->getMetaData('field_name'));
  164. if (isset($field['settings']['allowed_values'][1]) && isset($field['settings']['allowed_values'][1]['content_taxonomy_ignore_in_suggestions']) && $field['settings']['allowed_values'][1]['content_taxonomy_ignore_in_suggestions']) {
  165. if ($voc1 = taxonomy_vocabulary_machine_name_load($field['settings']['allowed_values'][0]['vocabulary'])) {
  166. $query->condition('t.vid', $voc1->vid);
  167. }
  168. }
  169. }
  170. /**
  171. * Implements hook_search_api_processor_info().
  172. */
  173. function content_taxonomy_autocomplete_search_api_processor_info() {
  174. $callbacks['content_taxonomy_autocomplete_moderated_terms'] = array(
  175. 'name' => t('Content Taxonomy Autocomplete Moderated Terms Filter'),
  176. 'description' => t('Filters terms from autocompletes that belong to the moderated vocabulary.'),
  177. 'class' => 'ContentTaxonomyAutocompleteModeratedTermsSearchAPIProcessor',
  178. );
  179. return $callbacks;
  180. }