You are here

class SearchApiGroupingMultivalueField in Search API Grouping 7

Processor for splitting up items on behalf of a multivalue field.

Hierarchy

Expanded class hierarchy of SearchApiGroupingMultivalueField

1 string reference to 'SearchApiGroupingMultivalueField'
search_api_grouping_search_api_processor_info in ./search_api_grouping.module
Implements hook_search_api_processor_info().

File

includes/processor_multivalue_field.inc, line 11
Processor for splitting indexing items on behalf of a multivalue field.

View source
class SearchApiGroupingMultivalueField extends SearchApiAbstractProcessor {

  /**
   * Return the settings form for this processor.
   */
  public function configurationForm() {
    $form = parent::configurationForm();

    // Re-use but modify the default form element.
    $form['fields']['#type'] = 'select';
    unset($form['fields']['#attributes']);
    $form['fields']['#options'] = array(
      NULL => t('<none>'),
    ) + $form['fields']['#options'];
    $form['split_field'] = $form['fields'];
    $form['fields']['#access'] = FALSE;
    $form['split_field'] = array(
      '#title' => 'The field to use to split the items to index.',
      '#default_value' => isset($this->options['split_field']) ? $this->options['split_field'] : NULL,
    ) + $form['split_field'];
    return $form;
  }

  /**
   * Splits up the items on behalf of a multivalue field.
   */
  public function preprocessIndexItems(array &$items) {

    // Preprocess first.
    parent::preprocessIndexItems($items);
    $source_items = $items;
    $all_items = array();
    if (!empty($this->options['split_field'])) {
      foreach ($items as $id => $item) {

        // Find the properties of the split field.
        $field_properties = array();
        list($key, $property) = explode(':', $this->options['split_field']);
        foreach ($item as $related_name => $related_field) {
          if (substr($related_name, 0, mb_strlen($key) + 1) == $key . ':') {
            $field_properties[] = $related_name;
          }
        }

        // Split field and properties found.
        if (!empty($field_properties)) {
          foreach ($item[$this->options['split_field']]['value'] as $delta => $value) {
            $split_item = $item;
            foreach ($field_properties as $field_property) {
              if (search_api_is_list_type($item[$field_property]['type'])) {
                $split_item[$field_property]['value'] = array(
                  $item[$field_property]['value'][$delta],
                );
              }
              else {

                // Very unlikely case but it's better to cover it.
                $split_item[$field_property]['value'] = $item[$field_property]['value'];
              }
            }
            $all_items[$id . ':delta' . $delta] = $split_item;
          }
        }
        else {
          $all_items[$id] = $item;
        }
      }
    }
    $items = $all_items;
  }

  /**
   * Fix potentially split id's.
   *
   * This is necessary since the id is reused to fetch the related entity.
   */
  public function postprocessSearchResults(array &$response, SearchApiQuery $query) {
    if (!empty($response['results'])) {
      foreach ($response['results'] as $id => &$result) {
        if (mb_strstr($id, ':delta') !== FALSE) {
          $result['id'] = preg_replace('/:delta\\d+/', '', $result['id']);
        }
      }
    }
    return;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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::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
SearchApiGroupingMultivalueField::configurationForm public function Return the settings form for this processor. Overrides SearchApiAbstractProcessor::configurationForm
SearchApiGroupingMultivalueField::postprocessSearchResults public function Fix potentially split id's. Overrides SearchApiAbstractProcessor::postprocessSearchResults
SearchApiGroupingMultivalueField::preprocessIndexItems public function Splits up the items on behalf of a multivalue field. Overrides SearchApiAbstractProcessor::preprocessIndexItems