getkirby / kql

Kirby's Query Language API combines the flexibility of Kirby's data structures, the power of GraphQL and the simplicity of REST.
https://getkirby.com
MIT License
145 stars 5 forks source link

How to query custom site methods? #48

Closed parislettau closed 1 year ago

parislettau commented 1 year ago

Hi there – I've created a custom site method that I would like to query but am have trouble doing so. The custom site method works from the Kirby instance.

Here's my method:

<?php

Kirby::plugin('lettau/custom-site-methods', [
    'siteMethods' => [
        'venues' => function () {
            /**
             * @kql-allowed
             */
            //  custom site method goes here
            $site = kirby()->site();
            $array = [];
            $reviews = $site->find('reviews')->children()->listed()->flip();

            // exhibitions yaml
            foreach ($reviews as $review) {
                $exhibitions = $review->exhibitions()->yaml();
                $venues = A::pluck($exhibitions, 'venue');
                $list = implode(', ', $venues);

                array_push($array, $list);
            }

            // venues field
            foreach ($reviews as $review) {
                $venues = $review->venue();
                array_push($array, $venues);
            }

            $array = implode(', ', $array);
            $array = explode(', ', $array);
            $array = array_unique($array);
            $array = array_filter($array);
            sort($array);

            return $array;
        }
    ]
]);

I have tried to allowed methods in the config as follows:

return[
  'kql' => [
    'methods' => [
      'allowed' => [
        'siteMethods::venues'
      ],
    ]
  ],
]

I make the following query from KQL:

 {
    query: `site.venues`,
  }

But nothing is returned:

{
    "code": 200,
    "result": "",
    "status": "ok"
}
lukasbestle commented 1 year ago

The docblock comment needs to be before the method, not inside it:

Kirby::plugin('lettau/custom-site-methods', [
    'siteMethods' => [
        /**
         * @kql-allowed
         */
        'venues' => function () {

If you use the option syntax, I think it should be Kirby\Cms\Site::venues instead of siteMethods::venues.

bastianallgeier commented 1 year ago

@parislettau does this answer your question?