content_taxonomy_autocomplete.module 7.4 KB

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