You are here

public function SearchApiAttachmentsMultipleEntitiesAlterSettings::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_multiple_entities/includes/callback_attachments_multiple_entities_settings.inc, line 12
Search API data alteration callback.

Class

SearchApiAttachmentsMultipleEntitiesAlterSettings
@file Search API data alteration callback.

Code

public function alterItems(array &$items) {

  // Extension restriction.
  $exclude = array();
  foreach (explode(' ', $this->options['excluded_extensions']) as $ext) {
    $exclude[$ext] = file_get_mimetype('dummy.' . $ext);
  }

  // File size restriction.
  $max_file_size = parse_size($this->options['max_file_size']);
  $fields = $this
    ->getFileFields();
  foreach ($items as $id => &$item) {
    foreach ($fields as $name => $field) {
      foreach ($item as &$single_item) {
        if (is_object($single_item) && isset($single_item->{$name})) {
          foreach ($single_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) {

              // Private file restriction.
              if (!$this
                ->isTemporary($file) && !($this->options['excluded_private'] && $this
                ->isPrivate($file))) {

                // Extension restriction.
                if (!in_array($file['filemime'], $exclude)) {

                  // File size restriction.
                  $file_size_errors = file_validate_size((object) $file, $max_file_size);
                  if (empty($file_size_errors)) {
                    $attachments = 'attachments_' . $name;
                    if (isset($item->{$attachments})) {
                      $item->{$attachments} .= ' ' . $this
                        ->getFileContent($file);
                    }
                    else {
                      $item->{$attachments} = $this
                        ->getFileContent($file);
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}