You are here

class SearchApiDenormalizedEntityField in Search API Grouping 7.2

Processor to configure and handle the denormalization per index.

Hierarchy

Expanded class hierarchy of SearchApiDenormalizedEntityField

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

File

includes/processor_denormalize_field.inc, line 11
Processor for configuring the denormalization per index.

View source
class SearchApiDenormalizedEntityField extends SearchApiAbstractProcessor {

  /**
   * Check if this index is supported.
   */
  public function supportsIndex(SearchApiIndex $index) {
    return $index
      ->datasource() instanceof SearchApiDenormalizedEntityDataSourceController;
  }

  /**
   * Return the settings form for this processor.
   */
  public function configurationForm() {
    $form = parent::configurationForm();
    $form['permutation_limit'] = array(
      '#type' => 'fieldset',
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
      '#title' => t('Permutation limits'),
      '#description' => t('Defines how man permutations should be processed. Can become handy if you just want to have one item of a muti-value field. Leave empty for no limit.'),
      '#weight' => 1,
    );
    $entity_type = $this->index
      ->datasource()
      ->getEntityType();
    $entity_info = entity_get_info($this->index
      ->datasource()
      ->getEntityType());
    $bundle_infos = field_info_instances($this->index
      ->datasource()
      ->getEntityType());
    $options = array();
    foreach ($form['fields']['#options'] as $field_name => $label) {
      if (stristr($field_name, ':')) {
        list($field_name, $property) = explode(':', $field_name, 2);
      }
      if ($field_info = field_info_field($field_name)) {
        $instance = field_read_instance($entity_type, $field_name, reset($field_info['bundles'][$entity_type]));
        if (!empty($field_info['cardinality']) && ($field_info['cardinality'] == FIELD_CARDINALITY_UNLIMITED || $field_info['cardinality'] > 1)) {
          $options[$field_name] = $instance['label'] . ' (' . $field_name . ')';
          $form['permutation_limit'][$field_name] = array(
            '#type' => 'textfield',
            '#title' => $instance['label'] . ' (' . $field_name . ')',
            '#size' => 4,
            '#maxlength' => 10,
            '#states' => array(
              'visible' => array(
                ':input[name$="[denormalization_field][' . $field_name . ']"]' => array(
                  'checked' => TRUE,
                ),
              ),
            ),
            '#default_value' => !empty($this->options['permutation_limit'][$field_name]) ? $this->options['permutation_limit'][$field_name] : NULL,
          );
        }
      }
    }
    $form['fields']['#options'] = $options;

    // Re-use but modify the default form element.
    $form['fields']['#type'] = 'checkboxes';
    unset($form['fields']['#attributes']);
    $form['denormalization_field'] = $form['fields'];
    $form['fields']['#access'] = FALSE;
    $form['denormalization_field'] = array(
      '#title' => t('The field to use to denormalize the items to index.'),
      '#description' => t('The field hast to be selected for indexing to use it for denormalization.'),
      '#default_value' => isset($this->options['denormalization_field']) ? $this->options['denormalization_field'] : NULL,
    ) + $form['denormalization_field'];
    return $form;
  }

  /**
   * Returns the fields to denormalize on.
   *
   * @return array
   *   Associative list of fields to use for denormalization. The value in the
   *   array defines the permutation limit. 0 means no limit.
   */
  public function getDenormalizationFields() {
    $fields =& drupal_static(__FUNCTION__, array());
    if (empty($fields)) {
      $fields = array_filter($this->options['denormalization_field']);
      foreach ($fields as $field_name => $field) {
        $fields[$field_name] = 0;
        if (!empty($this->options['permutation_limit'][$field]) && is_numeric($this->options['permutation_limit'][$field])) {
          $fields[$field_name] = (int) $this->options['permutation_limit'][$field];
        }
      }
    }
    return $fields;
  }

  /**
   * Returns the permutation limit of a field.
   *
   * @param string $field
   *   The field to get the limit for.
   *
   * @return int
   *   The number of permutations to generate from this field. 0 means no limit.
   */
  public function getDenormalizationFieldLimit($field) {
    if (!empty($this->options['permutation_limit'][$field]) && is_numeric($this->options['permutation_limit'][$field])) {
      return (int) $this->options['permutation_limit'][$field];
    }
    return 0;
  }

  /**
   * Denormalizes items on behalf of multivalue fields.
   */
  public function preprocessIndexItems(array &$items) {

    // Run default pre-processing first.
    parent::preprocessIndexItems($items);

    // Reduce item data to its denormalized data set.
    foreach ($items as $item_id => $item) {
      $items[$item_id] = $this
        ->createDocument($item, $item_id);
    }

    // Ensure obsolete permutations are removed from index.
    $this->index
      ->datasource()
      ->removeObsoletePermutationsFromIndex($items, $this->index);
  }

  /**
   * Create a denormalized item for indexing.
   *
   * @param array $item
   *   The item to index.
   * @param string $item_id
   *   The item id of the item to index.
   *
   * @return array
   *   Denormalized item to index.
   */
  protected function createDocument($item, $item_id) {
    $fields = $this
      ->getDenormalizationFields();
    $parts = explode(SEARCH_API_GROUPING_ENTITY_FIELD_SEPERATOR, $item_id);

    // Unshift non delta values to ensure the $fields_index matches.
    $entity_type = array_shift($parts);
    $entity_id = array_shift($parts);
    $fields_index = array_flip(array_keys($fields));
    foreach ($item as $index => $data) {
      list($key) = explode(':', $index, 2);
      if (isset($fields[$key]) && !empty($data['value'])) {
        $item[$index]['value'] = array_intersect_key($data['value'], array(
          $parts[$fields_index[$key]] => $parts[$fields_index[$key]],
        ));
      }
    }
    return $item;
  }

}

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::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::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
SearchApiDenormalizedEntityField::configurationForm public function Return the settings form for this processor. Overrides SearchApiAbstractProcessor::configurationForm
SearchApiDenormalizedEntityField::createDocument protected function Create a denormalized item for indexing.
SearchApiDenormalizedEntityField::getDenormalizationFieldLimit public function Returns the permutation limit of a field.
SearchApiDenormalizedEntityField::getDenormalizationFields public function Returns the fields to denormalize on.
SearchApiDenormalizedEntityField::preprocessIndexItems public function Denormalizes items on behalf of multivalue fields. Overrides SearchApiAbstractProcessor::preprocessIndexItems
SearchApiDenormalizedEntityField::supportsIndex public function Check if this index is supported. Overrides SearchApiAbstractProcessor::supportsIndex