statamic / cms

The core Laravel CMS Composer package
https://statamic.com
Other
3.58k stars 489 forks source link

Toggle field type in filter does not work #10194

Open godismyjudge95 opened 1 month ago

godismyjudge95 commented 1 month ago

Bug description

I was experimenting adding a filter to be able to filter by a certain status. I wanted to be able to use a simple toggle (instead of using a radio or checkbox). I was able to add the toggle field, but when toggling nothing happens. The clear button on the popup does not clear the state either.

My guess is that the toggle field type does not have the proper events/hooks to work with filters - or the implementation is bugged.

How to reproduce

  1. Create a new filter
  2. Set the $pinned property to true
  3. In the fieldItems() method add a new field like so:
    'custom_filter' => [
    'type' => 'toggle',
    'inline_label' => 'Filter',
    ],
  4. Add some code for filtering the query to the apply() method:
    if ($values['custom_filter'] ?? false) {
    $query->where('content', '!=', '');
    }
  5. Change the badge() method to something like:
    return $values['custom_filter'] ?? false ? 'Custom Filter' : null;
  6. Change the visibleTo() method to something like:
    return $key === 'entries';
  7. Go to the control panel and add a collection or go to an existing one.
  8. Click the dropdown to reveal the toggle and attempt to toggle the filter on - it does not filter when toggled
  9. Click the "Clear" button when the filter is toggled on - it does not reset the filter

Logs

No response

Environment

Environment
Application Name: Website
Laravel Version: 11.8.0
PHP Version: 8.3.6
Composer Version: 2.7.6
Environment: local
Debug Mode: ENABLED
URL: website.test
Maintenance Mode: OFF

Cache
Config: NOT CACHED
Events: NOT CACHED
Routes: NOT CACHED
Views: CACHED

Drivers
Broadcasting: redis
Cache: statamic
Database: sqlite
Logs: stack / single
Mail: smtp
Queue: redis
Session: redis

Livewire
Livewire: v3.5.0

Statamic
Addons: 16
Sites: 1
Stache Watcher: Disabled
Static Caching: Disabled
Version: 5.5.0 PRO

Statamic Addons
aerni/livewire-forms: 9.0.2
aerni/social-links: 3.2.0
aryehraber/statamic-logbook: 3.3.0
edalzell/forma: 3.0
godismyjudge95/statamic-embera: 1.1.1
jonassiewertsen/statamic-livewire: 3.3.1
mitydigital/statamic-google-fonts: 2.0.0
plugrbase/statamic-maintenance-mode: 1.07
statamic-rad-pack/mailchimp: 5.0.1
statamic-rad-pack/meilisearch: 3.3.0
statamic/seo-pro: 6.0.2
stillat/antlers-components: 2.2.0
stillat/relationships: 2.2.0
thoughtco/statamic-restrict-fields: 1.1.0

Installation

Fresh statamic/statamic site via CLI

Additional details

No response

duncanmcclean commented 1 month ago

I did some digging into this last week but I didn't have time to work out a fix.

It looks like boolean value is being filtered out before the AJAX request happens to get the filtered results:

https://github.com/statamic/cms/blob/46b4d39cc99f3be8a12cbb7958e3caf14b01a1ba/resources/js/components/data-list/Filter.vue#L65-L67

We probably need to update the check to allow for boolean values. I'll loop back to this issue this week, if no one beats me to it.