Untitled

 avatar
unknown
plain_text
5 months ago
3.5 kB
4
Indexable
<?php

namespace Drupal\custom_search_filter\Plugin\search_api\processor;

use Drupal\search_api\Processor\ProcessorPluginBase;
use Drupal\search_api\Item\ItemInterface;
use Drupal\media\Entity\Media;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Filters out media entities not referenced by content.
 *
 * @SearchApiProcessor(
 *   id = "media_reference_processor",
 *   label = @Translation("Media Reference Processor"),
 *   description = @Translation("Only index media entities that are referenced by other content."),
 *   stages = {
 *     "alter_items" = 0
 *   }
 * )
 */
class MediaReferenceProcessor extends ProcessorPluginBase {

  /**
   * The entity type manager service.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Constructs a MediaReferenceProcessor object.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin ID for the processor.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager service.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $container->get('entity_type.manager')
    );
  }

  /**
   * {@inheritdoc}
   */
  public function alterIndexedItems(array &$items) {
    foreach ($items as $id => $item) {
      $entity = $item->getOriginalObject()->getValue();

      // Check if the entity is a media entity.
      if ($entity instanceof Media) {
        // Check if the media entity is referenced by any node or other entities.
        if (!$this->isMediaReferenced($entity)) {
          // If the media entity is not referenced, remove it from the indexing.
          unset($items[$id]);
        }
      }
    }
  }

  /**
   * Checks if the media entity is referenced by any content.
   *
   * @param \Drupal\media\Entity\Media $media
   *   The media entity.
   *
   * @return bool
   *   TRUE if the media entity is referenced, FALSE otherwise.
   */
  protected function isMediaReferenced(Media $media) {
    $media_id = $media->id();
    
    // Check if media is referenced in node entity.
    $node_references = $this->entityTypeManager->getStorage('node')
      ->getQuery()
      ->condition('field_media_reference', $media_id, '=')
      ->range(0, 1)
      ->execute();
    
    if (!empty($node_references)) {
      return TRUE;
    }

    // Check if media is referenced in paragraph entity.
    $paragraph_references = $this->entityTypeManager->getStorage('paragraph')
      ->getQuery()
      ->condition('field_media_reference', $media_id, '=')
      ->range(0, 1)
      ->execute();
    
    if (!empty($paragraph_references)) {
      return TRUE;
    }

    return FALSE;
  }

}
Editor is loading...
Leave a Comment