content_taxonomy_autocomplete_moderated_terms.inc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * Search API Processor that filters out terms from moderated vocabularies.
  4. */
  5. class ContentTaxonomyAutocompleteModeratedTermsSearchAPIProcessor extends SearchApiAbstractProcessor {
  6. /**
  7. * Confiuration callback.
  8. *
  9. * Only allow users to select taxonomy fields.
  10. */
  11. public function configurationForm() {
  12. $form = parent::configurationForm();
  13. $form['fields']['#options'] = $this->filterTaxonomyFieldsOptions($form['fields']['#options']);
  14. return $form;
  15. }
  16. /**
  17. * Postprocess items while indexing and filter out the moderated terms.
  18. */
  19. public function preprocessIndexItems(array &$items) {
  20. $fields = $this->getTaxonomyFields($this->options['fields']);
  21. foreach ($items as &$item) {
  22. foreach ($fields as $search_api_property_name => $field) {
  23. if (isset($item[$search_api_property_name])) {
  24. $this->processTaxonomyField($item[$search_api_property_name]['value'], $item[$search_api_property_name]['type'], $field);
  25. }
  26. }
  27. }
  28. }
  29. /**
  30. * Processes a single field on a single item and does the actual filtering.
  31. *
  32. * As such a field can be nested, recursions are used until we reach the top
  33. * level.
  34. */
  35. protected function processTaxonomyField(&$value, &$type, $field) {
  36. if (!isset($value) || $value === '') {
  37. return;
  38. }
  39. if (substr($type, 0, 10) == 'list<list<') {
  40. $inner_type = $t1 = substr($type, 5, -1);
  41. foreach ($value as &$v) {
  42. $t1 = $inner_type;
  43. $this->processTaxonomyField($v, $t1, $field);
  44. }
  45. return;
  46. }
  47. if (is_array($value)) {
  48. foreach ($value as $key => $v) {
  49. if ($this->fieldValueIsModerated($v, $field)) {
  50. unset($value[$key]);
  51. }
  52. }
  53. }
  54. elseif ($this->fieldValueIsModerated($value, $field)) {
  55. $value = NULL;
  56. }
  57. }
  58. /**
  59. * Returns TRUE if the given term id is from moderated vocabulary.
  60. *
  61. * @param $value
  62. * The term id.
  63. * @param $field
  64. * The according field API field.
  65. */
  66. private function fieldValueIsModerated($value, $field) {
  67. $allowed_voc = $field['settings']['allowed_values'][0]['vocabulary'];
  68. $term = taxonomy_term_load($value);
  69. if ($term && $term->vocabulary_machine_name == $allowed_voc) {
  70. return FALSE;
  71. }
  72. return TRUE;
  73. }
  74. /**
  75. * Helper function that filters the configuration field options for taxonomy
  76. * fields.
  77. */
  78. private function filterTaxonomyFieldsOptions($options) {
  79. $taxonomy_fields = array();
  80. foreach ($options as $search_api_property_name => $label) {
  81. if ($this->getTaxonomyField($search_api_property_name)) {
  82. $taxonomy_fields[$search_api_property_name] = $label;
  83. }
  84. }
  85. return $taxonomy_fields;
  86. }
  87. /**
  88. * Helper function that returns the taxonomy fields for the given search API
  89. * property names.
  90. */
  91. private function getTaxonomyFields($search_api_properties) {
  92. $taxonomy_fields = array();
  93. foreach ($search_api_properties as $search_api_property_name => $label) {
  94. if ($field = $this->getTaxonomyField($search_api_property_name)) {
  95. $taxonomy_fields[$search_api_property_name] = $field;
  96. }
  97. }
  98. return $taxonomy_fields;
  99. }
  100. /**
  101. * Helper function that extracts the taxonomy field from a search API property
  102. * name.
  103. */
  104. private function getTaxonomyField($search_api_property_name) {
  105. $parts = explode(':', $search_api_property_name);
  106. foreach ($parts as $part) {
  107. if (substr($part, 0, 6) == 'field_') {
  108. $field = field_info_field($part);
  109. if ($field && isset($field['type']) && $field['type'] == "taxonomy_term_reference") {
  110. return $field;
  111. }
  112. }
  113. }
  114. return FALSE;
  115. }
  116. }