Filter images with multiple tags via select field

Hello, I am trying to build a filter based on the cookbook’s Using filters with forms (e.g. select fields). I fetch all images of a page, every image can have multiple tags. It works if the image has only 1 tag, lets say “White”. I select “White” and it dispays the image. But if it has multiple tags like "White and “Black” and I select “White” it displays nothing. How can I get it working? Thanks a lot in advance!

Blueprint

one:
  label: One
  type: tags
  index: all

Controller

return function($site, $pages, $page) {

  $one = $page->images()->pluck('one', ',', true);
  $keys = array('one');
  $projects = $page->images();

  if(r::is('POST') && $data = get()) {
    $projects = $page->images()->filter(function($child) use($keys, $data) {
      foreach($data as $key => $value) {
        if($value && in_array($key, $keys)) {
          if(!$match = $child->$key() == $value) {
            return false;
          }
        }
      }
      return $child;
    });
  }
  return compact('projects', 'one', 'data');
};

Select Form

<form id="filters" method="post">
  <select name="one" onchange="this.form.submit()">
    <option selected value="">One</option>
    <?php foreach($one as $item): ?>
      <?php if ($item == "") continue; ?>
      <option<?php e(isset($data['one']) && $data['one'] == $item, ' selected') ?> value="<?= $item ?>"><?= $item ?></option>
    <?php endforeach ?>
  </select>
</form>

Template

$items = $projects;

if($items->count()): ?>
    <?php foreach ($items as $item): ?>
        <img src="<?= $item->thumb('default')->url() ?>">
    <?php endforeach; ?>
<?php endif ?>

In this case you have to check if the selected values is contained in the array of values for that image:

if(!$match = in_array($value, $child->$key()->split(','))) {
            return false;
          }
1 Like

Thanks a lot for your input! If I replace the mentioned line with yours I unfortunatelay get an error:

syntax error, unexpected ‘return’ (T_RETURN)

Did I do something wrong?

My bad, forgot a closing parenthesis, corrected above.

Oh I’m sorry, this I should have seen also – thank you very much! And I’m stupid, I had another typo in the code myself. Now it works – thanks again for the help!