Page method is not returning when using Kirby Query Language

Hi there,

I’m trying to return a plugin’s page method using the Kirby Query language plugin, if the method is output to the template it works ok and also shows that it has been registered if i output all registered methods but returns blank when being used in the kql plugin.

are page methods allowed?

doesn’t look like it:

The easiest work around would obviously be to edit your local copy of the kql plugin and add your method name to the above list. But this means that every time you update the plugin, you have to reapply your patch.


Otherwise… I haven’t tested this, but it might be worth a shot…

Depending on your use case, especially if the pagemethod is used only on a few types of pages, you could add page models to your kirby installation and add your own “Interceptor” classes:
Like if you want to use the method on pages with blueprint/template “article”:

<?php 
// site/models/article.php

//we just need a class name in the kirby namespace
namespace Kirby\Cms {
  class ArticlePage extends Page {}
}

// create an alias so the model can be loaded
namespace {
  class_alias('Kirby\\Cms\\ArticlePage', 'ArticlePage');
}

//now create your Interceptor with matching name
namespace Kirby\Kql\Interceptors\Cms {
  class ArticlePage extends Page {
    public function allowedMethods(): array {
      return [... parent::allowedMethods(), 'myPageMethod'];
    }
  }
}

If you use the method on many page types, that don’t already have or need a model, it might be worth doing this in a generalized way in a plugin… something like:

<?php 
// site/plugins/myplugin/index.php

namespace {
    $types = [
      'article',
      'pizza', 
      'ham',
      'pineapple',
      'cheese',
      'italianCurseWords'
    ];
    
    Kirby::plugin('my/plugin', [
        'pageModels' => array_map(fn() => 'Kirby\\Cms\\LoosePage', array_flip($types))
    ]);
}

namespace Kirby\Cms {
    class LoosePage extends Page {}
}

namespace Kirby\Kql\Interceptors\Cms {
    class LoosePage extends Page
    {
        public function allowedMethods(): array
        {
            return [... parent::allowedMethods(), 'myMethodName'];
        }
    }
}