processor_multivalue_field.inc in Search API Grouping 7
Processor for splitting indexing items on behalf of a multivalue field.
File
includes/processor_multivalue_field.incView source
<?php
/**
* @file
* Processor for splitting indexing items on behalf of a multivalue field.
*/
/**
* Processor for splitting up items on behalf of a multivalue field.
*/
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;
}
}
Classes
Name | Description |
---|---|
SearchApiGroupingMultivalueField | Processor for splitting up items on behalf of a multivalue field. |