Filter to exclude files that contain specific string in their name

I made a custom filter out of @distantnative’s code, that can be used with \filterBy, like this

$page->images()->filterBy('filename', '!*=', '-big')

where !*= means “doesn’t contain”. To install this filter create a file filterStringContainsNot.php (or whatever you like) at plugins/ and save this code:

<?php

collection::$filters['!*='] = function($collection, $field, $value, $split = false) {

  foreach($collection->data as $key => $item) {
    if(!str::contains((string)collection::extractValue($item, $field), $value)) continue;
    unset($collection->$key);
  }

  return $collection;

};
4 Likes