3

When an entry is created and saved in a locale the corresponding entry is created for other locales using the original title. However, when the title of the locale which needs translating is edited the slug isn't auto generated from the new localised title and remains in the original language.

Of course, the slug can be manually edited but unfortunately the likelihood is that users won't bother, or forget, meaning that the urls for entries won't be localised.

What would be ideal is that on the first edit of the localised version that the slug is auto generated from the title as it is for the initial entry. Is this possible either via a plugin or could it be considered in a future version?

1 Answer 1

3

Yes, it's possible with a plugin. Make it listen to onBeforeSaveEntry events and look up how often the entry was already saved by counting entry versions using the getVersionsByEntryId method. If it is the entry's second save (one version exists) overwrite the slug with an empty string (will generate a new slug from the title).

You should add another conditional to only make it generate a new slug for all locales but the one you first saved in (not exactly sure how to get this), but this should get you started:

public function init()
{
    parent::init();

    craft()->on('entries.onBeforeSaveEntry', function(Event $event)
    {
        $entry = $event->params['entry'];

        if ($entry->id)
        {
            $versionsCount = count(craft()->entryRevisions->getVersionsByEntryId($entry->id, $entry->locale));
        }

        // $localeFirstSave = ??
        // if ($entry->locale != $localeFirstSave)

        if (isset($versionsCount) && $versionsCount == 1)
        {
            $entry->slug = '';
        }
    });
}

another variant you could try is to always load the JS that generates the slug on the fly:

public function init()
{
    if (craft()->request->isCpRequest())
    {
        craft()->templates->includeJs('window.slugGenerator = new Craft.SlugGenerator("#title", "#slug");');
    }
}
2
  • If anyone knows how to get the locale an entry was first saved in, please feel free to edit my answer.
    – carlcs
    Commented Feb 5, 2015 at 18:26
  • @Gogster maybe a solution that always makes the slug generate on title edits works better for your client?
    – carlcs
    Commented Feb 6, 2015 at 11:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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