I'm trying to build my first little plugin.
I'm stuck with a problem: I need to modify the value of the specific field reference
, right after the saving of the entry because the new value is supposed to be based on the same entry id
.
This works to get the Entry object and modify the reference:
Event::on(
Element::class,
Element::EVENT_AFTER_SAVE,
function(Event $event) {
if ($event->sender instanceof Entry) {
$idLast = Entry::find('id')
->section('annunci')
->orderBy('elements.dateUpdated desc')
->one();
$newRef = 'AV' . $idLast['id'];
// assign new reference ? At this point the record is already saved!!!
$event->sender->setFieldValue('reference', $newRef);
// then what?
}
});
I can manage to retrieve the last saved entry, grab its id, and then create my $newRef
, but after that, how am I supposed to resave the entry and update the entry record with the new value?
Second question: I would like to intercept the event only on the first save (aka on creation), and avoiding recalculating the field value on every successive saving. Is this possible?
Thanks in advance for any help, much appreciated!