Skip to content

Kirby 6.0.0

Panel search

Panel areas also allow you to add your own search type to the Panel – next to pages, files and users – or even to overwrite these existing search implementations.

Search definition

Searches are defined as a part of areas, with a label, an icon and a query handler. The handler receives the search query, the number of results per page and the current page, and returns the results:

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

Kirby::plugin(
  name: 'your-plugin/todos',
  extends: [
    'areas' => [
      'todos' => fn () => [
        // ... other code for the custom Panel area
        'searches' => [
          'todos' => [
            'label' => 'Todos',
            'icon'  => 'check',
            'query' => function (string $query, int $limit, int $page) {
              $results = [];

              foreach (todos() as $id => $todo) {
                if (str_contains($todo['text'], $query) === true) {
                  $results[] = [
                    'text' => $todo['text'],
                    'link' => '/todos',
                    'info' => 'Get it done!'
                  ];
                }
              }

              return $results;
            }
          ]
        ]
      ]
    ]
  ]
);

The search automatically appears in the search dialog of the Panel. With the search option of the area, you can define which search type is preselected there, e.g. 'search' => 'todos'.

You can also run a search from your Vue components:

const { results } = await this.$panel.search("todos", "Searchy search");

Search results

A search returns its results as an array with these parameters for each item:

Parameter Type Description
image array Optional image settings
icon string The name of the result icon
info string Optional info text on the right
link string The URL or path which will be visited on click
text string The label for the search result
uuid string The UUID of the search result

text and info are escaped unless they are trusted HTML. To paginate the results, return an array with the results and the pagination of your collection instead, e.g. 'pagination' => $collection->pagination()->toArray().

Controller classes

Instead of a query closure, you can handle a search with a controller class. Pass its class name as action. The class extends Kirby\Panel\Controller\SearchController, which takes the query, limit and page from the request and provides them as $this->query, $this->limit and $this->page:

/site/plugins/todos/classes/TodosSearchController.php
<?php

use Kirby\Panel\Controller\SearchController;

class TodosSearchController extends SearchController
{
  public function load(): array
  {
    $results = [];

    foreach (todos() as $id => $todo) {
      if (str_contains($todo['text'], $this->query) === true) {
        $results[] = [
          'text' => $todo['text'],
          'link' => '/todos',
          'icon' => 'check'
        ];
      }
    }

    return $results;
  }
}
'searches' => [
  'todos' => [
    'label'  => 'Todos',
    'icon'   => 'check',
    'action' => TodosSearchController::class
  ]
]

Extending default search types

You don't like the way the pages search works? Maybe you already have your data in Elasticsearch or Algolia and you want to use a real search server instead? You can override the core searches with your own action:

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

use Kirby\Panel\Controller\Search\PagesSearchController;

Kirby::plugin(
  name: 'example/search',
  extends: [
    'areas' => [
      'site' => fn () => [
        'searches' => [
          'pages' => [
            'action' => function (string $query, int $limit, int $page) {
              if (kirby()->user()->isAdmin() === true) {
                // run your own search for admins here …
              }

              // fall back to the core search
              return (new PagesSearchController($query, $limit, $page))->load();
            }
          ]
        ]
      ]
    ]
  ]
);

The core searches are handled by controller classes, so a query closure for a core search has no effect. To remove a core search, set it to false.

You find all core searches in the area definitions in /kirby/config/areas and their controllers in /kirby/src/Panel/Controller/Search.