From 2348440a6fabc00f790ce386c426c4998dd1da96 Mon Sep 17 00:00:00 2001 From: Jan Henckens Date: Thu, 15 Feb 2024 17:12:13 +0100 Subject: [PATCH] Use rootElementIfCanonical to check if we should actually run --- src/Scout.php | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/src/Scout.php b/src/Scout.php index d0416b6..c0f365e 100644 --- a/src/Scout.php +++ b/src/Scout.php @@ -10,6 +10,7 @@ use craft\events\DefineBehaviorsEvent; use craft\events\ElementEvent; use craft\events\RegisterComponentTypesEvent; +use craft\helpers\ElementHelper; use craft\services\Elements; use craft\services\Utilities; use craft\web\twig\variables\CraftVariable; @@ -157,22 +158,25 @@ private function registerEventHandlers(): void function(ElementEvent $event) { /** @var SearchableBehavior $element */ $element = $event->element; - if (!$element->hasMethod('searchable') || !$element->shouldBeSearchable()) { - return; - } + $baseElement = ElementHelper::rootElementIfCanonical($element); + if ($baseElement) { + if (!$baseElement->hasMethod('searchable') || !$baseElement->shouldBeSearchable()) { + return; + } - if (Scout::$plugin->getSettings()->queue) { - Craft::$app->getQueue() - ->ttr(Scout::$plugin->getSettings()->ttr) - ->priority(Scout::$plugin->getSettings()->priority) - ->push( - new IndexElement([ - 'id' => $element->id, - 'siteId' => $element->site ? $element->site->id : null, - ]) - ); - } else { - $element->searchable(); + if (Scout::$plugin->getSettings()->queue) { + Craft::$app->getQueue() + ->ttr(Scout::$plugin->getSettings()->ttr) + ->priority(Scout::$plugin->getSettings()->priority) + ->push( + new IndexElement([ + 'id' => $baseElement->id, + 'siteId' => $baseElement->site ? $baseElement->site->id : null + ]) + ); + } else { + $baseElement->searchable(); + } } } );