You are here

class ContentTaxonomyAutocompleteModeratedTermsSearchAPIProcessor in Content Taxonomy 7

Search API Processor that filters out terms from moderated vocabularies.

Hierarchy

Expanded class hierarchy of ContentTaxonomyAutocompleteModeratedTermsSearchAPIProcessor

1 string reference to 'ContentTaxonomyAutocompleteModeratedTermsSearchAPIProcessor'
content_taxonomy_autocomplete_search_api_processor_info in ./content_taxonomy_autocomplete.module
Implements hook_search_api_processor_info().

File

includes/content_taxonomy_autocomplete_moderated_terms.inc, line 6

View source
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
            ->processTaxonomyField($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 processTaxonomyField(&$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
          ->processTaxonomyField($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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ContentTaxonomyAutocompleteModeratedTermsSearchAPIProcessor::configurationForm public function Confiuration callback. Overrides SearchApiAbstractProcessor::configurationForm
ContentTaxonomyAutocompleteModeratedTermsSearchAPIProcessor::fieldValueIsModerated private function Returns TRUE if the given term id is from moderated vocabulary.
ContentTaxonomyAutocompleteModeratedTermsSearchAPIProcessor::filterTaxonomyFieldsOptions private function Helper function that filters the configuration field options for taxonomy fields.
ContentTaxonomyAutocompleteModeratedTermsSearchAPIProcessor::getTaxonomyField private function Helper function that extracts the taxonomy field from a search API property name.
ContentTaxonomyAutocompleteModeratedTermsSearchAPIProcessor::getTaxonomyFields private function Helper function that returns the taxonomy fields for the given search API property names.
ContentTaxonomyAutocompleteModeratedTermsSearchAPIProcessor::preprocessIndexItems public function Postprocess items while indexing and filter out the moderated terms. Overrides SearchApiAbstractProcessor::preprocessIndexItems
ContentTaxonomyAutocompleteModeratedTermsSearchAPIProcessor::processTaxonomyField protected function Processes a single field on a single item and does the actual filtering.
SearchApiAbstractProcessor::$index protected property
SearchApiAbstractProcessor::$options protected property
SearchApiAbstractProcessor::configurationFormSubmit public function Submit callback for the form returned by configurationForm(). Overrides SearchApiProcessorInterface::configurationFormSubmit
SearchApiAbstractProcessor::configurationFormValidate public function Validation callback for the form returned by configurationForm(). Overrides SearchApiProcessorInterface::configurationFormValidate 4
SearchApiAbstractProcessor::implodeTokens protected function Internal helper function for imploding tokens into a single string.
SearchApiAbstractProcessor::normalizeTokens protected function Internal helper function for normalizing tokens.
SearchApiAbstractProcessor::postprocessSearchResults public function Does nothing. Overrides SearchApiProcessorInterface::postprocessSearchResults 2
SearchApiAbstractProcessor::preprocessSearchQuery public function Calls processKeys() for the keys and processFilters() for the filters. Overrides SearchApiProcessorInterface::preprocessSearchQuery 1
SearchApiAbstractProcessor::process protected function Function that is ultimately called for all text by the standard implementation, and does nothing by default. 5
SearchApiAbstractProcessor::processField protected function Method for preprocessing field data.
SearchApiAbstractProcessor::processFieldValue protected function Called for processing a single text element in a field. The default implementation just calls process(). 2
SearchApiAbstractProcessor::processFilters protected function Method for preprocessing query filters.
SearchApiAbstractProcessor::processFilterValue protected function Called for processing a single filter value. The default implementation just calls process().
SearchApiAbstractProcessor::processKey protected function Called for processing a single search keyword. The default implementation just calls process().
SearchApiAbstractProcessor::processKeys protected function Method for preprocessing search keys.
SearchApiAbstractProcessor::supportsIndex public function Check whether this processor is applicable for a certain index. Overrides SearchApiProcessorInterface::supportsIndex
SearchApiAbstractProcessor::testField protected function Determines whether to process data from the given field.
SearchApiAbstractProcessor::testType protected function Determines whether fields of the given type should normally be processed.
SearchApiAbstractProcessor::__construct public function Constructor, saving its arguments into properties. Overrides SearchApiProcessorInterface::__construct 2