I'm calling EVENT_BEFORE_SAVE_ELEMENT to trigger a task that then updates the entry, but it's causing a loop?
The event is triggered when a specific entry type is saved:
Event::on(
Elements::class,
Elements::EVENT_BEFORE_SAVE_ELEMENT,
function(Event $event) {
$entry = $event->sender;
if ($event->element instanceof \craft\elements\Entry) {
$entry = $event->element;
if (ElementHelper::isDraftOrRevision($entry)) {
return;
}
if(($entry->sectionId == 18) && ($entry->typeId == 26)) {
$signpostURL = $entry->getFieldValue('signpostUrl');
if(($signpostURL) && ($signpostURL !== '')) {
Craft::$app->queue->push(new BrowshotTask([
'description' => 'Grabbing screenshot for '.$signpostURL,
'url' => $signpostURL,
'entryId' => $entry->id
]));
}
}
}
}
);
BrowshotTask
then performs some API calls to a third-party service, gets a screenshot, saves an asset and saves the asset in the asset field of the entry.
But this triggers the event again and causes a loop:
$entry->setFieldValue('browsershotUrl', [$assetId]);
if(Craft::$app->elements->saveElement($entry)) {
Sometimes BrowshotTask can take 20 seconds due to the third-party service.. so I thought putting it in a task would be a great idea, but now it's causing me so many problems :(