Changing Functions in Kirby Folder

Hello,

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.

Greetings, Jean-Paul

Welcome :slight_smile:

You can create your own custom page or site methods without your bread crumb code in it

if you want to base it off the built in breadcrumb, the code for that is here kirby/src/Cms/Site.php at 3.9.5 · getkirby/kirby · GitHub

The easiest way to exclude the home page without creating a custom method:

$breacrumbs = $site->breadcrumb()->not('home');

Every day is a school day… i had no idea you could do that :slight_smile:

The breadcrumb() method returns a Pages object, so you can use any $pages methods on it.

:heart_eyes: :heart_eyes: :heart_eyes: :heart_eyes:

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 :slight_smile: Hope you have a great evening and thanks in advance!

<nav class="breadcrumb" aria-label="breadcrumb">
  <ol>
    <?php foreach($site->breadcrumb()->not('home') as $crumb): ?>
    <li>
      <a href="<?= $crumb->url() ?>" <?= e($crumb->isActive(), 'aria-current="page"') ?>>
        <?= html($crumb->title()) ?>
      </a>
    </li>
    <?php endforeach ?>
  </ol>
</nav>

Looks fine!

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.

If you want to know more about classes, objects and method: A brief intro to object oriented programming in PHP | Kirby CMS

1 Like

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.

<nav class="breadcrumb" aria-label="breadcrumb">
  <ol>
    <?php
      $breadcrumbs = $site->breadcrumb()->not('home');
      foreach($breadcrumbs as $crumb): ?>
    <li>
      <a href="<?= $crumb->url() ?>" <?= e($crumb->isActive(), 'aria-current="page"') ?>>
        <?= html($crumb->title()) ?>
      </a>
    </li>
    <?php endforeach ?>
  </ol>
</nav>

Perfectly fine, although in this case, the variable is not really needed and your code above was already correct.

1 Like

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;
	}

Thanks in advance, Jean-Paul <3

So ->not() didnt do it?

As i said above, you can make custom methods. Just call it crumb or something and in there you can do exactly what you want.

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.

For a full list of stuff you can customize, check out the different plugin types: Extensions | Kirby CMS and core components: Core components | Kirby CMS

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.