1

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!

3

The accepted answer works but it's for elements in general, of which an entry is just one. Instead, you can skip checking if the element is an entry (if ($event->element instanceof Entry)) and just use the Entry class itself (it extends Element) like so:

use craft\elements\Entry;
use craft\events\ModelEvent;

Event::on(
    Entry::class,
    Entry::EVENT_AFTER_SAVE,
    function(ModelEvent $event) {
        $entry = $event->sender;
        $isNew = $event->isNew;

        if ($isNew) {
            // do your thing
        }
    }
);
| improve this answer | |
  • 1
    I wish this was the accepted answer - more efficient, thanks. – iamkeir Dec 5 '19 at 18:31
  • I wish it was in Craft's docs! :p Took me forever to figure out the appropriate events to use for simple tasks. – Ryan Dec 6 '19 at 17:13
  • Yeah, the events are hard to track down. – iamkeir Dec 8 '19 at 17:56
2

Following @lenka suggestion, I was able to make the script work this way:

use craft\services\Elements;
use craft\events\ElementEvent;
use craft\elements\Entry;

Event::on(
    Elements::class, 
    Elements::EVENT_AFTER_SAVE_ELEMENT, 
    function(ElementEvent $event) {
        if ($event->element instanceof Entry) {
            $entry = $event->element;
            if($event->isNew){
                $entry->reference= 'AV' . (string)$entry->id;
                Craft::$app->elements->saveElement($entry);    
            }
        }
});
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.