Override slugify - replace + symbol with " plus" for Model names
Created 1 year ago by garethshaw

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  —  1 year 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  —  1 year 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  —  1 year ago

Did you try?

'slug'        => [
    'type'   => 'anomaly.field_type.slug',
    'config' => [
        'slugify' => 'name',
        'type'    => '+',
    ],
],
william  —  1 year ago

That just changes the separator @piterden

garethshaw  —  1 year 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  —  1 year ago

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

william  —  1 year 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  —  1 year ago

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

piterden  —  1 year ago

Pyro uses the standard Laravel's slugifier

piterden  —  1 year ago
preg_replace(
                [
                    '/ /',
                    '@<br>@',
                    '@class="text-(.*)"@',
                    // '/{{ ([a-z_]+) }}/',
                ],
                [
                    ' ',
                    '<br/>',
                    'style="text-align:\$1;"',
                    // '{$1}',
                ],
                $this->getBody()
            )
garethshaw  —  1 year 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  —  1 year ago

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

william  —  1 year 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  —  1 year ago

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

garethshaw  —  1 year 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.

piterden  —  1 year ago

Just write your own global helper.

piterden  —  1 year ago

Look for examples of addons in the core folder

garethshaw  —  1 year ago

@william thanks. Can you point me at a class or file that I can use to work out an override? Example of an override and I can go from there in action. I literally started this project yesterday.

piterden  —  1 year ago

Almost all cases you would need to implement, is already implemented in the core folder. It's better for you to study to understand code, cause it is readable and have A category on the codacy.

piterden  —  1 year ago

You couldn't override global functions

piterden  —  1 year ago

All paths in Pyro have the strict dependence of namespaces.

garethshaw  —  1 year ago

...and slugify is a global function. Is that what you're saying? I'm not sure I follow your relevance to the namespace is. I understand namespacing, but not how that helps or doesn't now.

You're assuming I have a lot of knowledge of this system, which I don't, yet. I've got stuck on a couple of small points and just wanted a pointer after searching the code, forums and googling but to no avail. Sometimes it's just easier to ask a quick question, like this module does something similar, etc.

I really do get that you're trying to help, but on this topic, you aren't unfortunately. You're also assuming I haven't read through the code.

piterden  —  1 year ago

You are able to override any class loaded to the container. You can do it in any your custom addon. Placing all your code in separated addons is the good and preferable practice.

william  —  1 year ago

Starting to wonder if my suggested approach is the best. When you mean globally, what do you really mean? You do mean also for example when you write a page title, it should use your method to suggest the page alias , right?

If so read up on service container / providers in laravel documentation. And then look at what suggested and use that. Once you understand the service container and the providers you will know what i was suggesting.

But depending on which occasions you would like this to be used.. maybe a global helper is better as piterden suggested.

piterden  —  1 year ago

William, he don't know what is the container and what is the global helper)))

garethshaw  —  1 year ago

As I said, "globally available function without fully overriding the slug function for every module"

Yeah I think an addon is the way to go. Then I can call my global function myfunc->slug(), instead of str->slug() where my function will also call str->slug() as I only want to deal with one recurring issue the plus symbol. I'm not sure if this would affect anything, anywhere else, however a lot of cameras, lenses, airframes, etc, use + as an iterator between versions.

piterden  —  1 year ago

Man, you would definitely start with smoking man...

piterden  —  1 year ago

What is str->slug()?

piterden  —  1 year ago

Fuck, what is it function myfunc->slug() ??

Where are you from man?

william  —  1 year ago

@garethshaw In that case, just do a helper file and autoload it.. Several ways to do it .. one is described here.. others you can find just googling. :

https://stackoverflow.com/questions/43168719/custom-helper-classes-in-laravel-5-4

william  —  1 year ago

@piterden most likely : Illuminate\Support\Str

piterden  —  1 year ago

Global function is vendor/laravel/framework/src/Illuminate/Support/helpers.php#L979

piterden  —  1 year ago

Str has only static method Str::slug()

garethshaw  —  1 year ago

haha @william @piterden doesn't even know what str->slug() is.... Denis. We all start somewhere, rather than taking the piss, realise that I'm asking a genuine question and the answers you're giving, aren't helping me to quite understand, yet. It would be like me asking you about astrophysics and taking the piss when you don't understand the forces involved in a black hole, but are interested in finding out. Demoralising to say the least.

Yes @william use Illuminate\Support\Str;

/**
 * Create a new RoleSeeder instance.
 *
 * @param Str $str
 */
public function __construct(
    Str $str
)
{
    $this->makes = $makes;

    $this->disks = $disks;
    $this->folders = $folders;
    $this->files = $files;
    $this->str = $str;
}
        $make_name_slugified = trim($this->str->slug($make_name, "-"));
piterden  —  1 year ago

I doubt that you will override Str method.

garethshaw  —  1 year ago

Will you please, please read what I'm trying to do. I want to create, anything, whatever, I don't care, module, addon, global helper that enables me to create a globally available function. How? What?

I think we settled on Global helper right? If so, I'll go read that document you sent over. Thank you.

piterden  —  1 year ago

Man, I am not started with fucking brains before I read docs. When I fuck Ryan's brain the first time I had ready to go Pyro with Icecast on the back and Vue on the front, with Pusher on WebSockets and so on...

garethshaw  —  1 year ago

I'm not entirely sure what you just tried to write there, sorry. Before talking to Ryan you had already done something on the back and front?

garethshaw  —  1 year ago

and.. again, you're assuming I haven't read the documentation. You realise that the tutorials are out of date and inaccurate, right? They are literally wrong. I've followed them and they don't work, the videos I'm talking about here. I look through the code and couldn't find what I was looking for, searched google, couldn't find it, searched the forum, couldn't find it, so asked and you just have a go at me and take the piss. Dude, uncool!

ryanthompson  —  1 year ago

Hi Gareth! Welcome back 😊

So there are indeed two aspects you will need to override - the JS part for the input and the field type modifier modifier for the API part.

The JS is easy - override the view one way or the other and replace the asset_add with your own version of the plugin's code.

The API one is a little more Laravel-like. Use class binding in an addon service provider and bind this class with your own: https://github.com/anomalylabs/slug-field_type/blob/2.1/src/SlugFieldTypeModifier.php

Then override the method on your own and include any different logic you might need.

Hopefully that does the trick for you! Keep us posted 😊