Possible to display search result if found in structure field entry

Hi!

Is it possible to have search results also display specifically a page’s structure field row?

For example: There’s a page “comics” with a structure field “characters” which contains for example the columns “name” and “date”.

  1. The user searches for “Mickey Mouse”
  2. The page uid in which the string “Mickey Mouse” is found is being returned/displayed
  3. Now I can use $page->title() on the page

However, can I also only display the single structure field row which contains the string “Mickey Mouse”? Or just the entire page?

My search controller is the following:

return function ($site) {
  $query   = get('q');
  $results = $site->search($query)->listed();

  return [
    'query'   => $query,
    'results' => $results
  ];
};

The “characters” structure field would be:

# | N a m e | D a t e |
-----------------------
1 | Mickey Mouse | 1950
-----------------------
2 | Donald Duc k | 1960
-----------------------
3 | Buuugs Bunny | 1980

My foreach is:
(sorry for the structure)

    <?php foreach ($results as $result): ?>
        <?php if($result->template() == "comics"): ?>
          <?php $rows = $result->characters()->toStructure(); foreach ($rows as $row): ?>
            <?php if(in_array($query, $row)): ?>
               <?= $row->name() . $row->date() ?>
            <?php endif ?>
          <?php endforeach ?>
        <?php endif ?>
      <?php endforeach ?>

Thanks for any input!

Kirby’s search only ever returns pages as result. For anything that goes deeper then that, you have to actually try and find the result in the fields.

I thought so. I’m trying the following now and it works, however it fails to output the array in UTF-8, so it doesn’t work on search queries including umlauts for instance.

  <?php foreach ($results as $result): ?>
    <?php if($result->template() == "comics"): ?>          
      <?php $rows = $result->termine()->yaml(); foreach ($rows as $row): ?>
        <?php $array = array("name" => strtolower($row[name]), "date" => strtolower($row[date])); ?>
        
        <?php if(in_array($query, $array)): ?>
          in array = true
        <?php endif ?>
        
        <?php dump($array)?>
      <?php endforeach ?>
    <?php endif ?>
  <?php endforeach ?>

I tried utf8_encode(strtolower($row[name])) to no avail. What’s strange is, that the parent array is UTF-8 but not the new array.

I guess this is because it’s interpreted as bytes instead of UTF-8

Try https://www.php.net/manual/de/function.mb-strtolower.php

or