You are here

public function SearchApiAttachmentsLinksAlterSettings::alterItems in Search API attachments 7

Alter items before indexing.

Items which are removed from the array won't be indexed, but will be marked as clean for future indexing. This could for instance be used to implement some sort of access filter for security purposes (e.g., don't index unpublished nodes or comments).

Parameters

array $items: An array of items to be altered, keyed by item IDs.

Overrides SearchApiAttachmentsAlterSettings::alterItems

File

contrib/search_api_attachments_links/includes/callback_attachments_links_settings.inc, line 13
Search API data alteration callback.

Class

SearchApiAttachmentsLinksAlterSettings
@file Search API data alteration callback.

Code

public function alterItems(array &$items) {
  $link_fields = $this
    ->getLinkFields();
  foreach ($items as $id => &$item) {
    $item_wrapper = entity_metadata_wrapper($this->index->item_type, $item);
    foreach ($link_fields as $name => $link_field) {
      if (isset($item->{$name})) {
        $links = $item_wrapper->{$name}
          ->value();

        // Manage case of single value fields by reproducing the structure of
        // multiple values fields.
        if (isset($links['url'])) {
          $links = array(
            $links,
          );
        }
        else {
          if ($links == NULL) {
            $links = [];
          }
        }

        // Limit to the max number of value per field.
        if (isset($this->options['number_indexed']) && $this->options['number_indexed'] != '0' && count($links) > $this->options['number_indexed']) {
          $links = array_slice($links, 0, $this->options['number_indexed']);
        }
        foreach ($links as $link) {
          if (isset($link['url'])) {

            // Get the files.
            if ($this
              ->isFileIndexable($link, $item, $name)) {
              $attachments = 'attachments_links_' . $name;
              if (isset($item->{$attachments})) {
                $item->{$attachments} .= ' ' . $this
                  ->getLinkContent($link);
              }
              else {
                $item->{$attachments} = $this
                  ->getLinkContent($link);
              }
            }
          }
        }
      }
    }
  }
}