summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Hutterer2012-05-24 13:21:04 (GMT)
committer Matthias Hutterer2012-05-24 13:21:04 (GMT)
commitc531fd6b247acec4ff0e86e98e2bfbd3ba13edd3 (patch)
treea4abe567e11c368072f98144504cc74cff76ecbd
parent5d0257a2f955e2d9d9b74e0e3b152ebaad1a3cb1 (diff)
Added Search API Postprocessor for filtering out moderated terms from facets.
-rw-r--r--content_taxonomy_autocomplete.info2
-rw-r--r--content_taxonomy_autocomplete.module14
-rw-r--r--includes/content_taxonomy_autocomplete_moderated_terms.inc124
3 files changed, 139 insertions, 1 deletions
diff --git a/content_taxonomy_autocomplete.info b/content_taxonomy_autocomplete.info
index f04bfba..154e4a7 100644
--- a/content_taxonomy_autocomplete.info
+++ b/content_taxonomy_autocomplete.info
@@ -6,4 +6,6 @@ package = Fields
dependencies[] = content_taxonomy
+files[] = includes/content_taxonomy_autocomplete_moderated_terms.inc
+
diff --git a/content_taxonomy_autocomplete.module b/content_taxonomy_autocomplete.module
index cedb158..845ae7b 100644
--- a/content_taxonomy_autocomplete.module
+++ b/content_taxonomy_autocomplete.module
@@ -148,4 +148,16 @@ function content_taxonomy_autocomplete_query_autocomplete_deluxe_taxonomy_autoco
$query->condition('t.vid', $voc1->vid);
}
}
-} \ No newline at end of file
+}
+
+/**
+ * Implements hook_search_api_processor_info().
+ */
+function content_taxonomy_autocomplete_search_api_processor_info() {
+ $callbacks['content_taxonomy_autocomplete_moderated_terms'] = array(
+ 'name' => t('Content Taxonomy Autocomplete Moderated Terms Filter'),
+ 'description' => t('Filters terms from autocompletes that belong to the moderated vocabulary.'),
+ 'class' => 'ContentTaxonomyAutocompleteModeratedTermsSearchAPIProcessor',
+ );
+ return $callbacks;
+}
diff --git a/includes/content_taxonomy_autocomplete_moderated_terms.inc b/includes/content_taxonomy_autocomplete_moderated_terms.inc
new file mode 100644
index 0000000..d2e3c2e
--- /dev/null
+++ b/includes/content_taxonomy_autocomplete_moderated_terms.inc
@@ -0,0 +1,124 @@
+<?php
+
+/**
+ * Search API Processor that filters out terms from moderated vocabularies.
+ */
+class ContentTaxonomyAutocompleteModeratedTermsSearchAPIProcessor extends SearchApiAbstractProcessor {
+
+ /**
+ * Confiuration callback.
+ *
+ * Only allow users to select taxonomy fields.
+ */
+ public function configurationForm() {
+ $form = parent::configurationForm();
+ $form['fields']['#options'] = $this->filterTaxonomyFieldsOptions($form['fields']['#options']);
+ return $form;
+ }
+
+ /**
+ * Postprocess items while indexing and filter out the moderated terms.
+ */
+ public function preprocessIndexItems(array &$items) {
+ $fields = $this->getTaxonomyFields($this->options['fields']);
+ foreach ($items as &$item) {
+ foreach ($fields as $search_api_property_name => $field) {
+ if (isset($item[$search_api_property_name])) {
+ $this->processField($item[$search_api_property_name]['value'], $item[$search_api_property_name]['type'], $field);
+ }
+ }
+ }
+ }
+
+ /**
+ * Processes a single field on a single item and does the actual filtering.
+ *
+ * As such a field can be nested, recursions are used until we reach the top
+ * level.
+ */
+ protected function processField(&$value, &$type, $field) {
+ if (!isset($value) || $value === '') {
+ return;
+ }
+ if (substr($type, 0, 10) == 'list<list<') {
+ $inner_type = $t1 = substr($type, 5, -1);
+ foreach ($value as &$v) {
+ $t1 = $inner_type;
+ $this->processField($v, $t1, $field);
+ }
+ return;
+ }
+ if (is_array($value)) {
+ foreach ($value as $key => $v) {
+ if ($this->fieldValueIsModerated($v, $field)) {
+ unset($value[$key]);
+ }
+ }
+ }
+ elseif ($this->fieldValueIsModerated($value, $field)) {
+ $value = NULL;
+ }
+ }
+
+ /**
+ * Returns TRUE if the given term id is from moderated vocabulary.
+ *
+ * @param $value
+ * The term id.
+ * @param $field
+ * The according field API field.
+ */
+ private function fieldValueIsModerated($value, $field) {
+ $allowed_voc = $field['settings']['allowed_values'][0]['vocabulary'];
+ $term = taxonomy_term_load($value);
+ if ($term && $term->vocabulary_machine_name == $allowed_voc) {
+ return FALSE;
+ }
+ return TRUE;
+ }
+
+ /**
+ * Helper function that filters the configuration field options for taxonomy
+ * fields.
+ */
+ private function filterTaxonomyFieldsOptions($options) {
+ $taxonomy_fields = array();
+ foreach ($options as $search_api_property_name => $label) {
+ if ($this->getTaxonomyField($search_api_property_name)) {
+ $taxonomy_fields[$search_api_property_name] = $label;
+ }
+ }
+ return $taxonomy_fields;
+ }
+
+ /**
+ * Helper function that returns the taxonomy fields for the given search API
+ * property names.
+ */
+ private function getTaxonomyFields($search_api_properties) {
+ $taxonomy_fields = array();
+ foreach ($search_api_properties as $search_api_property_name => $label) {
+ if ($field = $this->getTaxonomyField($search_api_property_name)) {
+ $taxonomy_fields[$search_api_property_name] = $field;
+ }
+ }
+ return $taxonomy_fields;
+ }
+
+ /**
+ * Helper function that extracts the taxonomy field from a search API property
+ * name.
+ */
+ private function getTaxonomyField($search_api_property_name) {
+ $parts = explode(':', $search_api_property_name);
+ foreach ($parts as $part) {
+ if (substr($part, 0, 6) == 'field_') {
+ $field = field_info_field($part);
+ if ($field && isset($field['type']) && $field['type'] == "taxonomy_term_reference") {
+ return $field;
+ }
+ }
+ }
+ return FALSE;
+ }
+}