Allow other modules to alter the list of attachment of the node to be indexed

Created on 13 December 2019, almost 5 years ago
Updated 1 May 2024, 7 months ago

At the moment search_api_elasticsearch_attachments module only indexes file/image fields and source files from media fields of a node. It's very common to have files/media in a paragraph instead of the parent node, or even in nested paragraphs. As there is no reliable method to retrieve those files to index, IMO it'd be better to have an alter hook to allow other modules to add their own files to the index. The alter hook will also allow a custom module to index embedded media in text fields.

/**
 * Alter the list of attachment to be indexed of a node.
 *
 * @param \Drupal\file\FileInterface[] $attachments
 *   The list of files.
 * @param \Drupal\node\NodeInterface $node
 *   The node.
 * @param \Drupal\search_api\Item\ItemInterface $item
 *   The search index item.
 */
function hook_search_api_elasticsearch_attachments_node_attachments_alter(array &$attachments, NodeInterface $node, ItemInterface $item) {
  if ($node->bundle() == 'custom_node') {
    /** @var \Drupal\paragraphs\ParagraphInterface $paragraph */
    foreach ($node->get('field_paragraphs')->referencedEntities() as $paragraph) {
      /** @var \Drupal\media\MediaInterface $document */
      foreach ($paragraph->get('field_paragraph_document')->referencedEntities() as $document) {
        $fid = $document->getSource()->getSourceFieldValue($document);
        $attachments[$fid] = File::load($fid);
      }
    }
  }
}
Feature request
Status

Needs review

Version

6.0

Component

Code

Created by

🇦🇺Australia sonnykt Melbourne, Australia

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024