Replace the core `Str::widont()` method

,

I’ve been wanting to built a plugin to fix the built-in Str::widont() method, since the current method is quite rudimentary (doesn’t work on multiple paragraphs and breaks strings ending with an HTML tag). I’m targeting the Str::widont() method since it is the underlying method used for both the ->widont() field method and the widont() helper function.

I couldn’t find any documentation or examples on how to modify a Kirby helper class using a plugin. Can this be done without replacing the Str class if at all?

If overwriting the method isn’t an option, I’m considering creating a custom field, helper, and Str method. I already know how to create a custom field method and a helper function, but I don’t know what the best approaches to extend a core toolkit class is.

Thank you in advance for your help(^_^)

Noé Gogniat

You could create your own `Str` class in your plugin that extends the core class and use that in your snippets, templates, etc.

class Str extends \Kirby\Toolkit\Str
{
  public static function widont(): string
  {
    // TODO: Your implementation
  }
}

You could then add your own custom field method, because you cannot overwrite the core methods. You would also have to add your own helper function, because you cannot overwrite widont() either.

Thanks for the clarifications. ^^

Given that, I think creating a custom helper function and field method is the best approach.

I’m still working on regex to handle all edge cases which are always trickier than anticipated. I’ll hopefully have a plugin soon if anyone’s interested :3