Override slugify - replace + symbol with " plus" for Model names
Created 6 years ago by garethshawHi 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
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.
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.
I also thought that when using creat( that the slug had to already be slugified?
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
Is there a tutorial on overriding a field type? I've never done that before.
@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
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.
...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.
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.
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.
@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
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, "-"));
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.
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?
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!
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 😊
Pyro is using the slug method of
Illuminate\Support\Str
, i think you could override modify inAnomaly\SlugFieldType\SlugFieldTypeModifier
. Just do your str replace beforereturn trim($this->str->slug($value, $type), $type);
Should work.