Understanding Str::slug

I am creating pages programatically from a csv. This is my simplified page creation code:

    foreach ($artworks as $w) {

        $wpid = $w['id'];
        $title = [
            'en' => $w['Title (English)'],
            'es' => $w['Title (Spanish)'],
            'ca' => $w['Title (Catalan)'],
        ];
        $slug = $wpid . '-' . str::slug($title['en'], null, null, 64); // I got very long titles, better to trim them
        $data = [
            'slug' => $slug,
            'template' => 'artwork',
            'content' => [
                'wpid' => $wpid,  
                'title' => $title['en'], 
            ]
        ];     
    }

It seems to me that when the page is created the slug is changed.

For example, if I provide the slug as 10633-en-algun-lugar-desconocido-alguien-esta-escondiendo-alguna-cosa- when I dump the created page it shows: [slug] => 10633-en-algun-lugar-desconocido-alguien-esta-escondiendo-alguna-cosa

Did it eat the trailing hyphen for any reason ? Is this normal behaviour or may I look carefully at other parts of my code as responsible for this?

Thanks

It’s intentional and happens here: https://github.com/getkirby/kirby/blob/e8a40bf0ff2a3b5b7ee5729dc86a9e4b0ed141a7/src/Toolkit/Str.php#L961

…so Str::slug always removes any characters from the beginning and end of a string that are not letters or numbers as it “sluggifies” it.

Thanks

What I mean is that it does not seem to happen when i do:

    $slug = $wpid . '-' . str::slug($title['en'], null, null, 64); 

…but only after the page is created, and I look at the created page’s slug, as said:

For example, if I provide the slug as 10633-en-algun-lugar-desconocido-alguien-esta-escondiendo-alguna-cosa- when I dump the created page it shows: [slug] => 10633-en-algun-lugar-desconocido-alguien-esta-escondiendo-alguna-cosa

As if something different than str:slug is applied to the provided page’s slug on creation time ?

:confused:

Don’t know, but "en-algun-lugar-desconocido-alguien-esta-escondiendo-alguna-cosa-" is suspiciously close to 64 chars (it is exactly 64 chars). While the default (which Kirby uses, I guess) is 128 max length.

I think you’re just unlucky to get cut off exactly after a hyphen. Then Kirby probably runs Str::slug again when creating the page, and this time it removes the trailing hyphen.

Yes, it is 64 chars because I do str::slug($title[‘en’], null, null, 64) to create the slug I then use to create the page. And afterwards, kirby will use str:slug but with 128 chars, so that should not be a problem,.

Hmmm could it be that when specifying length, the method does not remove the trailing hyphen ? that’d be weird

Ok, I didn’t explain myself good enough.

Kirby trims the length after removing trailing hyphens. That means that maybe you start with:

En algún lugar desconocido alguien está escondiendo alguna cosa, 2018 , 2018

this becomes

en-algun-lugar-desconocido-alguien-esta-escondiendo-alguna-cosa-2018-2018

Then your Str::slug trims trailing hyphens (there are none).
Then the length is cut to 64 chars:

en-algun-lugar-desconocido-alguien-esta-escondiendo-alguna-cosa-

Then you prepend “10633-” and it becomes

10633-en-algun-lugar-desconocido-alguien-esta-escondiendo-alguna-cosa-

Then you create the page and Kirby calls Str::slug again, this time it almost does nothing, except removing trailing hyphens, so it becomes the final

10633-en-algun-lugar-desconocido-alguien-esta-escondiendo-alguna-cosa
1 Like