Skip to content

Commit

Permalink
Use rootElementIfCanonical to check if we should actually run
Browse files Browse the repository at this point in the history
  • Loading branch information
janhenckens committed Feb 15, 2024
1 parent e1d628a commit 2348440
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions src/Scout.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}
}
);
Expand Down

0 comments on commit 2348440

Please sign in to comment.