Skip to content

Instantly share code, notes, and snippets.

@fjahn
Created August 31, 2021 07:32
Show Gist options
  • Save fjahn/72ccaffe5939caabb670cacd9dc1537f to your computer and use it in GitHub Desktop.
Save fjahn/72ccaffe5939caabb670cacd9dc1537f to your computer and use it in GitHub Desktop.
Statamic filter for authors
<?php
namespace App\Scopes;
use Illuminate\Support\Facades\Auth;
use Statamic\Query\Scopes\Filter;
class AuthorFilter extends Filter
{
public $pinned = true;
static function title()
{
return __('Author');
}
function fieldItems()
{
return [
'author' => [
'type' => 'radio',
'options' => [
'all' => __('All entries'),
'own' => __('Me only'),
]
]
];
}
function autoApply()
{
return [
'author' => 'own'
];
}
function apply($query, $values)
{
$selectedValue = $values['author'];
if ($selectedValue == 'own')
$query->where('author', Auth::user()?->id());
}
function badge($values)
{
$selectedValue = $values['author'];
return match($selectedValue) {
'own' => __('Author: Me only'),
'all' => __('Author: any'),
default => '',
};
}
function visibleTo($key)
{
return $key === 'entries';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment