My entries' titles are auto-generated from several fields values on first save.

When one of these fields values change, I would like to update the auto-generated slug also:

    function (Event $event) {
        $entry = Entry::find()

        if ([] !== $entry) {
            $title = craft\helpers\StringHelper::toAscii($entry->id.'-'.$entry->title);
            $title = craft\helpers\StringHelper::toLowerCase($title);
            $title = str_replace(' ', '-', $title);

            // 👷 I tried this below, but none works
            $entry->slug = $title; // ⬅️ Not working
            $entry->setFieldValue('slug', $title); // ⬅️ Error
            Craft::$app->elements->saveElement($entry); // ⬅️ Infinite loop
            $event->element->slug = $event->element->id; // ⬅️ Not working

Unfortunately, none of these methods works. Any idea how to achieve this?


You can change the value in the DB directly instead of using the Elements service

$entry = $event->element;
$elementId = $entry->id;
$siteId = $entry->siteId;
        ['slug' => ElementHelper::createSlug('new-slug')],
            'elementId' => $elementId,
            'siteId'    => $siteId

By the way the following condition [] !== $entry won't be any good when you fetch an element with one() because one returns an object of instance ElementInterface or null but never an empty array. You can check !== null instead and if you fetch things via all you can perform an empty check if( empty($entries) === false)

  • Awesome, thank you! I had never thought of the DB way. Thank you also for the other explanations and advice, I really appreciate. – Romain Poirier Feb 13 at 13:28

Another shorter and probably slightly more performant solution, using EVENT_BEFORE_SAVE:

* Automatically update Element slugs to match the title if updated.
    function(craft\events\ModelEvent $event) {
        $entry = $event->sender;
        $entry->slug = ElementHelper::createSlug($entry->title);
  • This solution is not working, you should review the suggested code. – Romain Poirier Apr 29 at 15:53
  • Works fine for me (it's tested and being used). Did you actually try it? – Udo Apr 29 at 16:57
  • @RomainPoirier it will not work with EVENT_AFTER_SAVE_ELEMENT but it does with EVENT_BEFORE_SAVE_ELEMENT – Udo Apr 29 at 17:03
  • Yep, I tried but but too quickly, I'm sorry. I was missing the loading of craft\base\Element and craft\helpers\ElementHelper. Would you use EVENT_BEFORE_SAVE_ELEMENT instead of EVENT_BEFORE_SAVE? I can't find official reference of EVENT_BEFORE_SAVE in the Craft 3 docs. If you know, can you explain the utility of false at the end? Thank you! – Romain Poirier Apr 30 at 8:10
  • Since recent Craft updates, $entry->slug = ElementHelper::createSlug($entry->title); doesn't works as expected: the slug is sometimes saved as empty. Delete $entry->slug = and only keep ElementHelper::createSlug($entry->title); seems to fix it. – Romain Poirier Sep 23 at 9:23

I finally use this simple solution, which works pretty fine:

    function(craft\events\ModelEvent $event) {
        $entry = $event->sender;
        if (!\craft\helpers\ElementHelper::isDraftOrRevision($entry)) {
            if (!empty($entry->id)) {
                $entry->slug = $entry->title;

Where $entry->slug = $entry->title syncs the slug to the title if the entry is not new (!empty($entry->id)).


$entry->slug = ($entry->title !== null) ? \craft\helpers\StringHelper::slugify($entry->title) : $entry->slug;

This prevents to get a wrong slug on first save.

  • Due to the draft changes, this is the right approach for Craft 3.2 and later. – Fred Carlsen Oct 2 at 8:32

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.