vendor/sulu/sulu/src/Sulu/Bundle/MediaBundle/Search/Subscriber/StructureMediaSearchSubscriber.php line 53

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of Sulu.
  4. *
  5. * (c) Sulu GmbH
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace Sulu\Bundle\MediaBundle\Search\Subscriber;
  11. use Massive\Bundle\SearchBundle\Search\Event\PreIndexEvent;
  12. use Massive\Bundle\SearchBundle\Search\SearchEvents;
  13. use Sulu\Bundle\MediaBundle\Content\MediaSelectionContainer;
  14. use Sulu\Bundle\MediaBundle\Media\Manager\MediaManagerInterface;
  15. use Sulu\Component\Content\Document\Behavior\StructureBehavior;
  16. use Sulu\Component\Webspace\Analyzer\RequestAnalyzerInterface;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. /**
  19. * This subscriber populates the image URL field
  20. * when a Structure containing an image field is indexed.
  21. */
  22. class StructureMediaSearchSubscriber implements EventSubscriberInterface
  23. {
  24. /**
  25. * @param string $searchImageFormat the format of the image which will be returned in the search
  26. */
  27. public function __construct(
  28. protected MediaManagerInterface $mediaManager,
  29. protected ?RequestAnalyzerInterface $requestAnalyzer,
  30. protected $searchImageFormat
  31. ) {
  32. }
  33. /**
  34. * Returns the events this subscriber has subscribed.
  35. *
  36. * @return array
  37. */
  38. public static function getSubscribedEvents()
  39. {
  40. return [
  41. SearchEvents::PRE_INDEX => 'handlePreIndex',
  42. ];
  43. }
  44. /**
  45. * Adds the image to the search document.
  46. */
  47. public function handlePreIndex(PreIndexEvent $e)
  48. {
  49. $metadata = $e->getMetadata();
  50. $document = $e->getDocument();
  51. $subject = $e->getSubject();
  52. $evaluator = $e->getFieldEvaluator();
  53. if (
  54. !$e->getSubject() instanceof StructureBehavior
  55. && StructureBehavior::class !== $metadata->getName()
  56. ) {
  57. return;
  58. }
  59. if (!$imageUrlField = $metadata->getImageUrlField()) {
  60. return;
  61. }
  62. $data = $evaluator->getValue($subject, $imageUrlField);
  63. $locale = $subject->getLocale();
  64. if (!$data) {
  65. $document->setImageUrl(null);
  66. return;
  67. }
  68. $imageUrl = $this->getImageUrl($data, $locale);
  69. $document->setImageUrl($imageUrl);
  70. }
  71. /**
  72. * Returns the url for the image.
  73. *
  74. * @param array|MediaSelectionContainer $data
  75. * @param string $locale
  76. *
  77. * @return string|null
  78. *
  79. * @throws \RuntimeException
  80. * @throws \InvalidArgumentException
  81. */
  82. private function getImageUrl($data, $locale)
  83. {
  84. // new structures will container an instance of MediaSelectionContainer
  85. if ($data instanceof MediaSelectionContainer) {
  86. $medias = $data->getData();
  87. } else {
  88. $ids = [];
  89. if (isset($data['ids'])) {
  90. $ids = \array_merge($ids, $data['ids']);
  91. } elseif (isset($data['id'])) {
  92. $ids = \array_merge($ids, [$data['id']]);
  93. }
  94. if (0 === \count($ids)) {
  95. return null;
  96. }
  97. $medias = $this->mediaManager->get($locale, [
  98. 'ids' => $ids,
  99. ]);
  100. }
  101. // no media, no thumbnail URL
  102. if (!$medias) {
  103. return null;
  104. }
  105. $media = \current($medias);
  106. if (!$media) {
  107. return null;
  108. }
  109. $formats = $media->getThumbnails();
  110. if (empty($formats)) {
  111. return null;
  112. }
  113. if (!isset($formats[$this->searchImageFormat])) {
  114. throw new \InvalidArgumentException(
  115. \sprintf('Search image format "%s" is not known', $this->searchImageFormat)
  116. );
  117. }
  118. return $formats[$this->searchImageFormat];
  119. }
  120. }