Commit 34fe6f51 authored by Brandon Bergren's avatar Brandon Bergren Committed by Ismaeil Abouljamal
Browse files

Issue #2757973 by bdragon, izus: Add a hook for customizing indexability of files.

parent 70eb317f
Loading
Loading
Loading
Loading
+35
Changes for search_api_attachments.api.php: 35 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

/**
 * @file
 * Hooks provided by the "Search API attachments" module.
 */

/**
 * @addtogroup hooks
 * @{
 */

/**
 * Determines whether an attachment should be indexed.
 *
 * @param object $file
 *   A file object.
 * @param \Drupal\search_api\Item\ItemInterface $item
 *   The item the file was referenced in.
 * @param string $field_name
 *   The name of the field the file was referenced in.
 *
 * @return bool|null
 *   Return FALSE if the attachment should not be indexed.
 */
function hook_search_api_attachments_indexable($file, $item, $field_name) {
  // Don't index files on entities owned by our bulk upload bot accounts.
  if (in_array($item->getOriginalObject()->uid, my_module_blocked_uids())) {
    return FALSE;
  }
}

/**
 * @} End of "addtogroup hooks".
 */
 No newline at end of file
+11 −2
Changes for src/Plugin/search_api/processor/FilesFieldsProcessorPlugin.php: 11 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -122,7 +122,7 @@ class FilesFieldsProcessorPlugin extends ProcessorPluginBase {
                ->loadMultiple($fids);
              $extraction = '';
              foreach ($files as $file) {
                if ($this->isFileIndexable($file)) {
                if ($this->isFileIndexable($file, $item, $field_name)) {
                  $extraction .= $this->extractOrGetFromCache($file, $extractor_plugin);
                }
              }
@@ -189,11 +189,15 @@ class FilesFieldsProcessorPlugin extends ProcessorPluginBase {
   *
   * @param object $file
   *   A file object.
   * @param \Drupal\search_api\Item\ItemInterface $item
   *   The item the file was referenced in.
   * @param string|null $field_name
   *   The name of the field the file was referenced in, if applicable.
   *
   * @return bool
   *   TRUE or FALSE
   */
  public function isFileIndexable($file) {
  public function isFileIndexable($file, $item, $field_name = NULL) {
    // File should exist in disc.
    $indexable = file_exists($file->getFileUri());
    if (!$indexable) {
@@ -216,6 +220,11 @@ class FilesFieldsProcessorPlugin extends ProcessorPluginBase {
    }
    // Whether a private file can be indexed or not.
    $indexable = $indexable && $this->isPrivateFileAllowed($file);
    if (!$indexable) {
      return FALSE;
    }
    $result = \Drupal::moduleHandler()->invokeAll('search_api_attachments_indexable', $file, $item, $field_name);
    $indexable = !in_array(FALSE, $result, TRUE);
    return $indexable;
  }