Cms: Slug conversion to ASCII does not work when using custom title

Created on 8 Nov 2017  路  15Comments  路  Source: craftcms/cms

Description

Auto-generate slugs (conversion from 盲-枚-眉-脽 => ae-oe-ue-ss) from a title does work, but defining a title like {entryField.first().title} doesn't convert the 盲眉枚脽, but removes them.

Steps to reproduce

  1. Define auto title like {entryField.first().title}
  2. Remove previously generated slug
  3. Save a title with "枚盲眉脽脰脛脺" and see that everything non-ASCII get's removed instead of converted

If you can't reproduce, I am glad to help in any further bugfixing!

bug need more info

Most helpful comment

@steveooo9 Try this for me鈥β爋pen up craft/app/helpers/ElementHelper.php and replace your setValidSlug() method with this:

public static function setValidSlug(BaseElementModel $element)
{
    $slug = $element->slug;

    if (!$slug)
    {
        // Create a slug for them, based on the element's title.
        // Replace periods, underscores, and hyphens with spaces so they get separated with the slugWordSeparator
        // to mimic the default JavaScript-based slug generation.
        $slug = str_replace(array('.', '_', '-'), ' ', $element->title);

        // Enforce the limitAutoSlugsToAscii config setting
        if (craft()->config->get('limitAutoSlugsToAscii'))
        {
            if (!craft()->config->get('allowUppercaseInSlug'))
            {
                // Do this now because our ASCII character mappings are lowercase only.
                $slug = mb_strtolower($slug);
            }

            $slug = StringHelper::asciiString($slug);
        }
    }

    $element->slug = static::createSlug($slug);
}

Then clear out the auto-generated entry slug and try re-saving it.

All 15 comments

What version of Craft are you seeing this in?

@brandonkelly I am using Craft CMS 2.6.2989

Just tested and I鈥檓 not able to reproduce on 2.6.2997. Can you try updating and see if that helps?

The only thing I can think of is, maybe you enabled the limitAutoSlugsToAscii config setting in craft/config/general.php? Although if you had, it should have affected the auto-generated slug as well.

@brandonkelly I am unable to test in the most recent version at the moment..

I have following in my general.php: 'omitScriptNameInUrls' => true, 'limitAutoSlugsToAscii' => true, 'convertFilenamesToAscii' => true,

'limitAutoSlugsToAscii' => true is your problem. You are telling Craft to downgrade/discard anything but plain ASCII characters in slugs.

@brandonkelly But this conversion from 盲 => ae and such did work before... Where do I control these character conversions?

I want that my characters (often used in German language) 盲枚眉脽脛脰脺 to convert to the corresponding ASCII letters..

@brandonkelly It seems that the logic from the limitAutoSlugsToAscii setting (discard anything but ASCII chars) is being applied before some other character conversions (e.g. 盲 => ae)?

@steveooo9 Tested both and both were converting correctly for me. Possible you added that config setting _after_ the initial entry had been saved?

There are two places that config setting is taken into account聽鈥撀爄n the CP JavaScript, when auto-populating the Slug value as you type in the Title field, and in the PHP when the entry is being saved.

I'm not able to reproduce this on a stock Craft install, either. The limitAutoSlugsToAscii config settings calls StringHelper::asciiString($str), which uses the Craft default ASCII character mappings, but also factors in any custom ASCII character mappings via the customAsciiCharMappings config setting. Are you setting any custom mappings via that last config setting?

@takobell Can it be that when using auto title via entry.first().titlesomehow a step is missing? Is seems that without it everything get's lowercased and ASCII get mapped. Maybe with the first one, only it get's ASCII mapped but not lowercased. In Craft's asciiString method, there's no conversion for uppercase 脰脛脺, so maybe there's an issue?

@steveooo9 Try this for me鈥β爋pen up craft/app/helpers/ElementHelper.php and replace your setValidSlug() method with this:

public static function setValidSlug(BaseElementModel $element)
{
    $slug = $element->slug;

    if (!$slug)
    {
        // Create a slug for them, based on the element's title.
        // Replace periods, underscores, and hyphens with spaces so they get separated with the slugWordSeparator
        // to mimic the default JavaScript-based slug generation.
        $slug = str_replace(array('.', '_', '-'), ' ', $element->title);

        // Enforce the limitAutoSlugsToAscii config setting
        if (craft()->config->get('limitAutoSlugsToAscii'))
        {
            if (!craft()->config->get('allowUppercaseInSlug'))
            {
                // Do this now because our ASCII character mappings are lowercase only.
                $slug = mb_strtolower($slug);
            }

            $slug = StringHelper::asciiString($slug);
        }
    }

    $element->slug = static::createSlug($slug);
}

Then clear out the auto-generated entry slug and try re-saving it.

@brandonkelly Yep, this works great! Great fix!

Awesome, thanks for letting me know. We鈥檒l get that fix into the next release.

@brandonkelly Great!

Was this page helpful?
0 / 5 - 0 ratings