distantnative / search-for-kirby

Kirby 3 plugin for adding a search index (sqlite or Algolia).
43 stars 3 forks source link

Define fields only for specific templates #50

Open antoine1000 opened 3 years ago

antoine1000 commented 3 years ago

First of all, thanks for this extra nice plugin!

I have a question related to fields defined in config. I'm maybe missing something super obvious, but I would like to know if it's possible to define fields only for one (or some) template(s)?

For exemple, I got a website with members pages. Each member has his own page with his profile pic, bio, projects...etc. I would like to add a "profilepic" field query only for "member" template, and not for other pages. So far I got this:

# config.php

[...]

'search' => [
    'provider' => 'algolia',
      'algolia' => [
          'app'     => $ALGOLIA_APP,
          'key'     => $ALGOLIA_KEY,
          'index' => 'my_index_name'
    ],
    'entries' => [
        'pages' => 'site.index', 
        'files' => false,
        'users' => false
    ],
    'fields' => [
      'pages' => [
        'title',
        'profilepic' => function ($model) {
          if($model->intendedTemplate() == 'member') {
            $profilepic = $model->profilepic()->toFile();
            return ($profilepic) ? $profilepic->url() : null;
          }
        },
      ]
    ]
  ],

profilepic query is added correctly in Algolia Indices but it is added in all my pages, and not only for members template. This is not a big issue as profilepic will be null for all other pages but I think it will be cleaner not to add unnecessary queries to pages and I was curious how to achieve this!

Thanks for your help ☺️

distantnative commented 3 years ago

There actually isn't a way right now. I'll see if we can add this at some point.