Include page slug in Search

I have a bunch of pages with Titles that have various accents in them. These pages don’t appear in searches unless it’s spelt exactly, meaning I need a fuzzy search but sadly the old plugin is not for K3. I did notice that the accents are removed when the title is sluggified though, is there any way I can include those as a search criteria? Or repurpose them as a field for search via a hook?

  1. Creating an additional hidden field via a hook would certainly be one option.

  2. A page model with extended content, but the downside would be that you would need a model for each page type.

  3. Custom search component

As a lot of the pages have already been created, 1 would only work for new pages. I’m only searching one type of page template, how would number 2 work?

You would have to overwrite the content() method and add a new (virtual) content field (e.g. titleslug) in a page model

Something like this:

    public function content(string $languageCode = null)
    {
        // single language support
        if ($this->kirby()->multilang() === false) {
            if (is_a($this->content, 'Kirby\Cms\Content') === true) {
                $content = $this->content->toArray();
                $content['titleslug'] = Str::slug('hallo');
                return $this->content = new Content($content, $this);
            }
            $content = $this->readContent();
            $content['titleslug'] = Str::slug($content['title']);
            return $this->setContent($content)->content;

        // multi language support
        } else {

            // only fetch from cache for the default language
            if ($languageCode === null && is_a($this->content, 'Kirby\Cms\Content') === true) {
                $content = $this->content->toArray();
                $content['titleslug'] = Str::slug($content['title']);
                return $this->content = new Content($content, $this);
            }

            // get the translation by code
            if ($translation = $this->translation($languageCode)) {
                $content = $translation->content();
                $content['titleslug'] = Str::slug($content['title']);
                $content = new Content($content, $this);
            } else {
                throw new InvalidArgumentException('Invalid language: ' . $languageCode);
            }

            // only store the content for the current language
            if ($languageCode === null) {
                $this->content = $content;
            }

            return $content;
        }
    }

Then you should be able to use the field titleslug in your search. Instead of sluggifying the title, you could also just remove accents.