You are here

function hook_search_api_attachments_content_extracted in Search API attachments 8

Same name and namespace in other branches
  1. 9.0.x search_api_attachments.api.php \hook_search_api_attachments_content_extracted()

Allow other modules to run after content extraction for a file.

Parameters

object $file: A file object.

\Drupal\Core\Entity\EntityInterface $entity: The entity where the file was referenced in.

2 invocations of hook_search_api_attachments_content_extracted()
ExtractorQueue::processItem in src/Plugin/QueueWorker/ExtractorQueue.php
Works on a single queue item.
FilesExtractor::extractOrGetFromCache in src/Plugin/search_api/processor/FilesExtractor.php
Extract non text file data or get it from cache if available and cache it.

File

./search_api_attachments.api.php, line 41
Hooks provided by the "Search API attachments" module.

Code

function hook_search_api_attachments_content_extracted($file, \Drupal\Core\Entity\EntityInterface $entity) {

  // Search for nodes using media item in specific fields.
  if ($entity
    ->getEntityTypeId() === 'media') {
    $query = \Drupal::entityQuery('node')
      ->condition('field_pdf', $entity
      ->id())
      ->condition('status', 1);

    // Remove access check to ensure all entities are returned.
    $query
      ->accessCheck(FALSE);
    $results = $query
      ->execute();
    if ($results) {

      // For each node, get all the applicable indexes
      // and mark items as need reindex.
      $nodes = \Drupal::entityTypeManager()
        ->getStorage('node')
        ->loadMultiple($results);
      foreach ($nodes as $node) {
        $indexes = ContentEntity::getIndexesForEntity($node);
        $item_ids = [];
        if (is_a($node, TranslatableInterface::class)) {
          $translations = $node
            ->getTranslationLanguages();
          foreach ($translations as $translation_id => $translation) {
            $item_ids[] = $node
              ->id() . ':' . $translation_id;
          }
        }
        $datasource_id = 'entity:node';
        foreach ($indexes as $index) {
          $index
            ->trackItemsUpdated($datasource_id, $item_ids);
        }
      }
    }
  }
}