You are here

public function Denormalize::alterIndexedItems in Search API Grouping 8

Alter the items to be indexed.

Parameters

\Drupal\search_api\Item\ItemInterface[] $items: An array of items to be indexed, passed by reference.

Overrides ProcessorPluginBase::alterIndexedItems

File

src/Plugin/search_api/processor/Denormalize.php, line 115

Class

Denormalize
This processor allows you to denormalize items.

Namespace

Drupal\search_api_grouping\Plugin\search_api\processor

Code

public function alterIndexedItems(array &$items) {
  $all_nodes = [];
  foreach ($items as $item_id => $item) {
    $node = $this
      ->getDenormalizationFields();
    foreach ($node as $field_name => $permutation_limit) {
      $values = $item
        ->getField($field_name)
        ->getValues();
      if (!empty($item
        ->getField($field_name)
        ->getValues())) {
        $all_nodes[$item_id][$field_name] = $values;
      }
    }
  }

  // All denormalized items.
  $denormalized_items = [];

  // Loop through every item.
  foreach ($items as $original_item_id => $original_item) {
    $part_denormalized_items[$original_item_id] = $original_item;
    $original_fields = $all_nodes[$original_item_id];

    // Loop through the fields to denormalize on.
    foreach ($original_fields as $field_name => $field) {

      // Loop through the already denormalized items
      // If the item has not been denormalized before,
      // the original item gets denormalized.
      foreach ($part_denormalized_items as $part_denormalized_item_name => $part_denormalized_item) {
        foreach ($field as $field_value) {
          $part_denormalized_items[$part_denormalized_item_name . $field_name . $field_value] = $this
            ->createDocument($part_denormalized_item, $field_name, $field_value);
        }
        unset($part_denormalized_items[$part_denormalized_item_name]);
      }
      unset($part_denormalized_items[$original_item_id]);
    }
    $denormalized_items += $part_denormalized_items;
    $part_denormalized_items = [];
  }
  $items = $denormalized_items;
}