sorry if that’s already an answered question, I didn’t found anything so I just had to ask.
Is there any method to overwrite specific functions located in the kirby folder? I want to use the breadcrumb function, but without a specific line in the code (I don’t want to show the “Home” first), so I was wondering where I would change this since the kirby folder should not be touched with several good reasons.
Maybe there’s a common workflow to solve this “problem”, which I just didn’t noticed, and my problem is not a problem. But if, I hope we’ll find a solution together.
Thanks! I didn’t knew how to use the full code snippet you’ve send, because I don’t “speak” PHP, but I figured out a solution using a part of your code. Do you think it’s fine like this, and can you explain your code snippet a bit? That would be pretty helpful Hope you have a great evening and thanks in advance!
I just stored the breadcrumbs minus the home page in a variable, that was all.
$site->breadcrumb()
returns an object of the Pages class (collection of pages)($pages | Kirby CMS), not() is a method of that class that you can use to exclude certain pages from the given collection.
Thank you so much for your time and expertise, it helped a lot! I’m learning so much right now, that’s really nice! Am I using your code correct in the way I used it now? I think I understood how to use the variable.
I just realised, that my Question isn’t really answered, since it wasn’t related to the breadcrumb (which is working fine now) but more of a general question.
So I’m asking again and maybe someone know’s the answer:
Where do I put code, that changes kirby’s core functions/methods, e.G. the breadcrumb which comes with kirby (just because it’s a good example).
Code that comes with Kirby:
public function breadcrumb()
{
// get all parents and flip the order
$crumb = $this->page()->parents()->flip();
// add the home page
$crumb->prepend($this->homePage()->id(), $this->homePage());
// add the active page
$crumb->append($this->page()->id(), $this->page());
return $crumb;
}
I want to remove this part:
// add the home page
$crumb->prepend($this->homePage()->id(), $this->homePage());
Final Code, which I want to know where to put it, to overwrite the default function of Kirby.
public function breadcrumb()
{
// get all parents and flip the order
$crumb = $this->page()->parents()->flip();
// add the active page
$crumb->append($this->page()->id(), $this->page());
return $crumb;
}
If you want to override existing page methods, you can only do this in a page model (which creates a child class of the page class). For other types of classes, there is no easy way to override the method (you can overwrite certain core classes and in these, also their methods, but not as easily as creating page models, see Replacing Kirby's core classes | Kirby CMS).
With custom methods as mentioned by @jimbobrjames, you cannot overwrite default methods.
But, to answer your questions: You cannot create a $site->breadcrumb() method without using a custom site class. Not worth it, though. Either create a custom site method with a different name, or do it like I suggested by excluding the home page.