vendor/ibexa/core/src/lib/Search/Common/EventSubscriber/ContentEventSubscriber.php line 94

Open in your IDE?
  1. <?php
  2. /**
  3. * @copyright Copyright (C) Ibexa AS. All rights reserved.
  4. * @license For full copyright and license information view LICENSE file distributed with this source code.
  5. */
  6. namespace Ibexa\Core\Search\Common\EventSubscriber;
  7. use Ibexa\Contracts\Core\Repository\Events\Content\CopyContentEvent;
  8. use Ibexa\Contracts\Core\Repository\Events\Content\DeleteContentEvent;
  9. use Ibexa\Contracts\Core\Repository\Events\Content\DeleteTranslationEvent;
  10. use Ibexa\Contracts\Core\Repository\Events\Content\HideContentEvent;
  11. use Ibexa\Contracts\Core\Repository\Events\Content\PublishVersionEvent;
  12. use Ibexa\Contracts\Core\Repository\Events\Content\RevealContentEvent;
  13. use Ibexa\Contracts\Core\Repository\Events\Content\UpdateContentMetadataEvent;
  14. use Ibexa\Contracts\Core\Search\ContentTranslationHandler;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. class ContentEventSubscriber extends AbstractSearchEventSubscriber implements EventSubscriberInterface
  17. {
  18. public static function getSubscribedEvents(): array
  19. {
  20. return [
  21. CopyContentEvent::class => 'onCopyContent',
  22. DeleteContentEvent::class => 'onDeleteContent',
  23. DeleteTranslationEvent::class => 'onDeleteTranslation',
  24. HideContentEvent::class => 'onHideContent',
  25. PublishVersionEvent::class => 'onPublishVersion',
  26. RevealContentEvent::class => 'onRevealContent',
  27. UpdateContentMetadataEvent::class => 'onUpdateContentMetadata',
  28. ];
  29. }
  30. public function onCopyContent(CopyContentEvent $event)
  31. {
  32. $this->searchHandler->indexContent(
  33. $this->persistenceHandler->contentHandler()->load(
  34. $event->getContent()->getVersionInfo()->getContentInfo()->id,
  35. $event->getContent()->getVersionInfo()->versionNo
  36. )
  37. );
  38. $locations = $this->persistenceHandler->locationHandler()->loadLocationsByContent(
  39. $event->getContent()->getVersionInfo()->getContentInfo()->id
  40. );
  41. foreach ($locations as $location) {
  42. $this->searchHandler->indexLocation($location);
  43. }
  44. }
  45. public function onDeleteContent(DeleteContentEvent $event)
  46. {
  47. $this->searchHandler->deleteContent($event->getContentInfo()->id);
  48. foreach ($event->getLocations() as $locationId) {
  49. $this->searchHandler->deleteLocation($locationId, $event->getContentInfo()->id);
  50. }
  51. }
  52. public function onDeleteTranslation(DeleteTranslationEvent $event)
  53. {
  54. $contentInfo = $this->persistenceHandler->contentHandler()->loadContentInfo(
  55. $event->getContentInfo()->id
  56. );
  57. if (!$contentInfo->isPublished) {
  58. return;
  59. }
  60. if ($this->searchHandler instanceof ContentTranslationHandler) {
  61. $this->searchHandler->deleteTranslation(
  62. $contentInfo->id,
  63. $event->getLanguageCode()
  64. );
  65. }
  66. $this->searchHandler->indexContent(
  67. $this->persistenceHandler->contentHandler()->load(
  68. $contentInfo->id,
  69. $contentInfo->currentVersionNo
  70. )
  71. );
  72. $locations = $this->persistenceHandler->locationHandler()->loadLocationsByContent(
  73. $contentInfo->id
  74. );
  75. foreach ($locations as $location) {
  76. $this->searchHandler->indexLocation($location);
  77. }
  78. }
  79. public function onHideContent(HideContentEvent $event)
  80. {
  81. $locations = $this->persistenceHandler->locationHandler()->loadLocationsByContent($event->getContentInfo()->id);
  82. foreach ($locations as $location) {
  83. $this->indexSubtree($location->id);
  84. }
  85. }
  86. public function onPublishVersion(PublishVersionEvent $event)
  87. {
  88. $this->searchHandler->indexContent(
  89. $this->persistenceHandler->contentHandler()->load($event->getContent()->id, $event->getContent()->getVersionInfo()->versionNo)
  90. );
  91. $locations = $this->persistenceHandler->locationHandler()->loadLocationsByContent($event->getContent()->id);
  92. foreach ($locations as $location) {
  93. $this->searchHandler->indexLocation($location);
  94. }
  95. }
  96. public function onRevealContent(RevealContentEvent $event)
  97. {
  98. $locations = $this->persistenceHandler->locationHandler()->loadLocationsByContent($event->getContentInfo()->id);
  99. foreach ($locations as $location) {
  100. $this->indexSubtree($location->id);
  101. }
  102. }
  103. public function onUpdateContentMetadata(UpdateContentMetadataEvent $event)
  104. {
  105. $contentInfo = $this->persistenceHandler->contentHandler()->loadContentInfo($event->getContent()->id);
  106. if (!$contentInfo->isPublished) {
  107. return;
  108. }
  109. $this->searchHandler->indexContent(
  110. $this->persistenceHandler->contentHandler()->load($contentInfo->id, $contentInfo->currentVersionNo)
  111. );
  112. $this->searchHandler->indexLocation($this->persistenceHandler->locationHandler()->load($contentInfo->mainLocationId));
  113. }
  114. }
  115. class_alias(ContentEventSubscriber::class, 'eZ\Publish\Core\Search\Common\EventSubscriber\ContentEventSubscriber');