Can we extend Site object method?

Hi,

In the Panel, I display pages with template “partnership”

site.yml:

sections:
   siteSectionPartnersListed:
       headline: Partnerships - Online
       type: pages
       status: listed
       templates: partnership

I’ve got a custom user role “Partner”
In the partner.yml blueprint, I set a checkbox field to select Partnership page the user will be allowed to see in the Panel.

I would like to be able to filter the Partnership pages displayed in the panel to only display partnership pages selected for the current logged in user.

With “Page models” we can extend Page object methods, is there also a way to extend Site object method?

In my case here, I would like to extends the $site->children() or $site->childrenAndDrafts() methods to return pages only if they are selected in the current user page.

You can extend with site methods:

With site methods, you can’t override existing site methods. And there is nothing like a site model, I think.

So if I understand correctly “site methods” is only used to define new custom method for the site object?

Exactly. https://getkirby.com/docs/reference/plugins/extensions/site-methods

For those who might be interested, this is my solution:

1- Create a custom site methods “getPartnerPartnerships”

…/plugins/site-methods/index.php

Kirby::plugin('gillesvauvarin/plugin', [
    'siteMethods' => [
        'getPartnerPartnerships' => function () {
            $current_user = kirby()->user();
            $partnerPartnerships = $current_user->userPartnership()->split();
            $getPartnerPartnerships = array();
            $partnerships = $this->childrenAndDrafts()->filterBy( 'template' , 'partnership' )->listed();
            foreach ( $partnerships as $partnership ) :
                if ( ! in_array ( $partnership->uid() , $partnerPartnerships ) ) :
                    $partnerships->remove( $partnership->uid() );
                endif;
            endforeach;
            return $partnerships;
      }
  ]
]);

2- I use the “k3-pagesdisplay-section” plugin from @rasteiner in my site.yml blueprint to call my custom method:

sections:
  siteSectionPartnersListed:
      headline: Partnerships - Online
      type: pagesdisplay
      query: site.getPartnerPartnerships

3- This is my custom “partner” user blueprint where I set a partnership checkbox field

partner.yml

     userPartnership:
        label: Partnership
        type: checkboxes
        options: query
        query:
          fetch: site.children.filterBy('template','partnership').listed
          text: "{{ page.title }}"
          value: "{{ page.uid }}"
1 Like

I think it’s a very successful solution :+1:

Thank you @ahmetbora and @texnixe for your help!

What I like with Kirby is there is often a solution and a helpful support/community :slight_smile:

1 Like