Override slugify - replace + symbol with " plus" for Model names

garethshaw - 2 weeks ago

Hi Take S800 and S800+, when slugified they both come out as s800, instead I want s800 and s800-plus. I came up with:

        // slugify it
        $make_name = trim(str_replace("+", " plus", $make_name));
        $make_name_slugified = trim($this->str->slug($make_name, "-"));

However I'd like to be ABLE to use this throughout my application, i.e. not fully override and make this the default, what's the best way to make this a globally available function without fully overriding the slug function for every module?

Thanks Gareth

william - 2 weeks ago

Pyro is using the slug method of Illuminate\Support\Str, i think you could override modify in Anomaly\SlugFieldType\SlugFieldTypeModifier. Just do your str replace before return trim($this->str->slug($value, $type), $type);

Should work.

garethshaw - 2 weeks ago

What happens during an update? Will that file be overwritten? Also doesn't that mean it happens all the time, for every single call to Slugify?

Thanks for your feedback.

piterden - 2 weeks ago

Did you try?

'slug'        => [
    'type'   => 'anomaly.field_type.slug',
    'config' => [
        'slugify' => 'name',
        'type'    => '+',
    ],
],

william - 2 weeks ago

That just changes the separator @piterden

garethshaw - 2 weeks ago

Hi You've misunderstood what I'm trying to do. e.g.1 Original name 1 = S800 Output slug 1 = s800 1.g.2 Original name 2 = S800+ Output slug 2 = s800-plus

otherwise i'll get 2 x url/s800 for two unique items, vs url/s800 AND url/s800-plus

e.g. http://www.dji.com/spreading-wings-s1000 http://www.dji.com/spreading-wings-s1000-plus

Which are two products, however based on the current slugify config would have exactly the same slug because it doesn't deal with the + symbol correctly.

Thanks, see previous comment about a method of fixing it and my question about upgrades undoing this core fix.

garethshaw - 2 weeks ago

I also thought that when using creat( that the slug had to already be slugified?

william - 2 weeks ago

If you override it, it will always use your code.. To make sure its compatible with the slug field type, you could hard set the version in your composer file. And then keep an eye out for changes to the slug field type. And if something changes you could make updates to your code and then switch versions.. I guess there could also be a change from taylor.. but i don't think he will change his method.. but you could also have a look at that when updating to make sure

garethshaw - 2 weeks ago

Is there a tutorial on overriding a field type? I've never done that before.

piterden - 2 weeks ago

Pyro uses the standard Laravel's slugifier

piterden - 2 weeks ago

preg_replace(
                [
                    '/ /',
                    '@<br>@',
                    '@class="text-(.*)"@',
                    // '/{{ ([a-z_]+) }}/',
                ],
                [
                    ' ',
                    '<br/>',
                    'style="text-align:$1;"',
                    // '{}',
                ],
                $this->getBody()
            )

garethshaw - 2 weeks ago

@piterden what relevance is that preg_replace command? think you've put it into the wrong place and what do you mean about slack? Ask questions about Pyrocms on Slack rather than the pyrocms forum?

piterden - 2 weeks ago

Are you really don't see that relevance? How about the seeder in a neighbor topic? Has it relevance?

william - 2 weeks ago

@garethshaw you could just add a binding to your serviceprovider. That would basically tell the system to use your class when its looking to use the default one..

Anomaly\SlugFieldType\SlugFieldTypeModifier::class => YourTheme\YourCass:class

william - 2 weeks ago

@piterden you don't understand what he is trying to do.

garethshaw - 2 weeks ago

I should have started with, I'm a developer but have never used Laravel or PyroCMS before, othe than v2.

So no, I don't see the relevance, given you started by answering the qrong question. I really appreciate you taking the time to respond, but posting code without any explanation isn't helpful. I what preg_replace is, i was asking how to override, i.e. where to put the code, where i can find a tutorial, not for a random irrelevant snippet.