1313
1414namespace Pimcore \Bundle \GenericDataIndexBundle \EventSubscriber ;
1515
16+ use Pimcore \Bundle \GenericDataIndexBundle \Enum \SearchIndex \ElementType ;
1617use Pimcore \Bundle \GenericDataIndexBundle \Enum \SearchIndex \IndexQueueOperation ;
1718use Pimcore \Bundle \GenericDataIndexBundle \Installer ;
19+ use Pimcore \Bundle \GenericDataIndexBundle \Service \Search \SearchService \DataObject \SearchHelper ;
20+ use Pimcore \Bundle \GenericDataIndexBundle \Service \SearchIndex \IndexElementIndexServiceInterface ;
1821use Pimcore \Bundle \GenericDataIndexBundle \Service \SearchIndex \IndexQueue \QueueMessagesDispatcher ;
1922use Pimcore \Bundle \GenericDataIndexBundle \Service \SearchIndex \IndexQueue \SynchronousProcessingRelatedIdsServiceInterface ;
2023use Pimcore \Bundle \GenericDataIndexBundle \Service \SearchIndex \IndexQueue \SynchronousProcessingServiceInterface ;
2124use Pimcore \Bundle \GenericDataIndexBundle \Service \SearchIndex \IndexQueueServiceInterface ;
2225use Pimcore \Bundle \GenericDataIndexBundle \Traits \LoggerAwareTrait ;
26+ use Pimcore \Bundle \StaticResolverBundle \Lib \Cache \RuntimeCacheResolverInterface ;
2327use Pimcore \Event \DataObjectEvents ;
2428use Pimcore \Event \Model \DataObjectEvent ;
29+ use Pimcore \Model \DataObject \AbstractObject ;
2530use Pimcore \Model \DataObject \Service ;
2631use Symfony \Component \EventDispatcher \EventSubscriberInterface ;
2732
@@ -35,7 +40,9 @@ final class DataObjectIndexUpdateSubscriber implements EventSubscriberInterface
3540 public function __construct (
3641 private readonly Installer $ installer ,
3742 private readonly IndexQueueServiceInterface $ indexQueueService ,
43+ private readonly IndexElementIndexServiceInterface $ indexElementIndexService ,
3844 private readonly QueueMessagesDispatcher $ queueMessagesDispatcher ,
45+ private readonly RuntimeCacheResolverInterface $ runtimeCacheResolver ,
3946 private readonly SynchronousProcessingServiceInterface $ synchronousProcessing ,
4047 private readonly SynchronousProcessingRelatedIdsServiceInterface $ synchronousProcessingRelatedIds
4148 ) {
@@ -44,57 +51,77 @@ public function __construct(
4451 public static function getSubscribedEvents (): array
4552 {
4653 return [
54+ DataObjectEvents::POST_ADD => 'addDataObject ' ,
4755 DataObjectEvents::POST_UPDATE => 'updateDataObject ' ,
48- DataObjectEvents::POST_ADD => 'updateDataObject ' ,
4956 DataObjectEvents::POST_DELETE => 'deleteDataObject ' ,
5057 ];
5158 }
5259
60+ public function addDataObject (DataObjectEvent $ event ): void
61+ {
62+ $ this ->updateData ($ event );
63+ }
64+
5365 public function updateDataObject (DataObjectEvent $ event ): void
5466 {
55- if (!$ this ->installer ->isInstalled ()) {
56- return ;
67+ $ this ->indexElementIndexService ->updateSiblings ($ event ->getObject (), ElementType::DATA_OBJECT ->value );
68+ if ($ event ->getObject ()->getChildrenSortBy () === AbstractObject::OBJECT_CHILDREN_SORT_BY_INDEX ) {
69+ $ this ->indexElementIndexService ->resetChildrenIndexBy ($ event ->getObject ());
5770 }
71+ $ this ->updateData ($ event );
72+ }
5873
59- //do not update index when auto save or only saving version
60- if (
61- ($ event ->hasArgument ('isAutoSave ' ) && $ event ->getArgument ('isAutoSave ' )) ||
62- ($ event ->hasArgument ('saveVersionOnly ' ) && $ event ->getArgument ('saveVersionOnly ' ))
63- ) {
74+ public function deleteDataObject (DataObjectEvent $ event ): void
75+ {
76+ if (!$ this ->installer ->isInstalled ()) {
6477 return ;
6578 }
6679
6780 $ dataObject = $ event ->getObject ();
68- Service::useInheritedValues (true , fn () =>
69- $ this ->indexQueueService
70- ->updateIndexQueue (
71- $ dataObject ,
72- IndexQueueOperation::UPDATE ->value ,
73- $ this ->synchronousProcessing ->isEnabled (),
74- $ dataObject ->hasChildren (includingUnpublished: true ),
75- $ this ->synchronousProcessingRelatedIds ->isEnabled () === false
76- )
77- ->commit ()
78- );
79-
81+ $ this ->indexQueueService
82+ ->updateIndexQueue (
83+ $ dataObject ,
84+ IndexQueueOperation::DELETE ->value ,
85+ $ this ->synchronousProcessing ->isEnabled (),
86+ $ dataObject ->hasChildren (includingUnpublished: true )
87+ )
88+ ->commit ();
8089 $ this ->queueMessagesDispatcher ->dispatchQueueMessages ();
8190 }
8291
83- public function deleteDataObject (DataObjectEvent $ event ): void
92+ private function updateData (DataObjectEvent $ event ): void
8493 {
8594 if (!$ this ->installer ->isInstalled ()) {
8695 return ;
8796 }
8897
98+ //do not update index when auto save or only saving a version
99+ if (
100+ ($ event ->hasArgument ('isAutoSave ' ) && $ event ->getArgument ('isAutoSave ' )) ||
101+ ($ event ->hasArgument ('saveVersionOnly ' ) && $ event ->getArgument ('saveVersionOnly ' ))
102+ ) {
103+ return ;
104+ }
105+
89106 $ dataObject = $ event ->getObject ();
107+ Service::useInheritedValues (true , fn () =>
90108 $ this ->indexQueueService
91109 ->updateIndexQueue (
92110 $ dataObject ,
93- IndexQueueOperation::DELETE ->value ,
111+ IndexQueueOperation::UPDATE ->value ,
94112 $ this ->synchronousProcessing ->isEnabled (),
95- $ dataObject ->hasChildren (includingUnpublished: true )
113+ $ dataObject ->hasChildren (includingUnpublished: true ),
114+ $ this ->synchronousProcessingRelatedIds ->isEnabled () === false
96115 )
97- ->commit ();
116+ ->commit ()
117+ );
118+
98119 $ this ->queueMessagesDispatcher ->dispatchQueueMessages ();
120+
121+ //clear runtime cache for this object
122+ $ cacheKey = SearchHelper::OBJECT_SEARCH . '_ ' . $ event ->getObject ()->getId ();
123+ if ($ this ->runtimeCacheResolver ->isRegistered ($ cacheKey )) {
124+ $ this ->runtimeCacheResolver ->set ($ cacheKey , null );
125+ }
99126 }
100127}
0 commit comments