You are here

public function SearchApiAttachmentsAlterSettings::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 SearchApiAlterCallbackInterface::alterItems

10 methods override SearchApiAttachmentsAlterSettings::alterItems()
SearchApiAttachmentsCommentAlterSettings::alterItems in contrib/search_api_attachments_comment/includes/callback_attachments_comment_settings.inc
Alter items before indexing.
SearchApiAttachmentsCommerceProductReferenceAlterSettings::alterItems in contrib/search_api_attachments_commerce_product_reference/includes/callback_attachments_commerce_product_reference_settings.inc
Alter items before indexing.
SearchApiAttachmentsEntityreferenceAlterSettings::alterItems in contrib/search_api_attachments_entityreference/includes/callback_attachments_entityreference_settings.inc
Alter items before indexing.
SearchApiAttachmentsFieldCollectionsAlterSettings::alterItems in contrib/search_api_attachments_field_collections/includes/callback_attachments_field_collections_settings.inc
Alter items before indexing.
SearchApiAttachmentsLinksAlterSettings::alterItems in contrib/search_api_attachments_links/includes/callback_attachments_links_settings.inc
Alter items before indexing.

... See full list

File

includes/callback_attachments_settings.inc, line 19
Search API data alteration callback.

Class

SearchApiAttachmentsAlterSettings
Indexes files content.

Code

public function alterItems(array &$items) {
  if ($this->index
    ->getEntityType() == 'file' || $this
    ->isMultipleIndexWithFile()) {
    foreach ($items as &$item) {
      $file = array();
      if ($this
        ->isMultipleIndexWithFile()) {
        $file = (array) $item->file;
      }
      else {
        foreach ($item as $key => $value) {
          $file[$key] = $value;
        }
      }
      if ($this
        ->isFileIndexable($file, $item)) {
        $item->attachments_content = $this
          ->getFileContent($file);
      }
    }
  }
  else {
    $fields = $this
      ->getIndexableFileFields();
    foreach ($items as &$item) {
      foreach ($fields as $name => $field) {
        if (isset($item->{$name})) {
          foreach ($item->{$name} as $value) {

            // Limit to the max number of value per field.
            if (isset($this->options['number_indexed']) && $this->options['number_indexed'] != '0' && count($value) > $this->options['number_indexed']) {
              $value = array_slice($value, 0, $this->options['number_indexed']);
            }
            foreach ($value as $file) {
              if ($this
                ->isFileIndexable($file, $item, $name)) {
                $attachments = 'attachments_' . $name;
                if (isset($item->{$attachments})) {
                  $item->{$attachments} .= ' ' . $this
                    ->getFileContent($file);
                }
                else {
                  $item->{$attachments} = $this
                    ->getFileContent($file);
                }
              }
            }
          }
        }
      }
    }
  }
}