statamic-fetch icon indicating copy to clipboard operation
statamic-fetch copied to clipboard

Filter by published/unpublished does not work for localized data

Open wanze opened this issue 2 years ago • 1 comments

Description

Fetch returns unpublished content when using filter=published in combination with non-default locale. For example, a call to !/Fetch/collection/project?&limit=6&offset=0&locale=fr&filter=published will return unpublished entries of the project collection for French.

Problem

Fetch::filterData() does not respect localized data. Thus, filtering by published status is always performed on entries of the default locale.

Possible solution

For a quick fix, I extended Fetch::filterData() to localize the data before passing it to Fetch::filterPublished() or Fetch::filterUnpublished().

        if ($this->data instanceof IlluminateCollection) {
            $this->data = $this->data->filter(function ($entry) use ($filter) {
               // START: ADDED CODE
                if ($this->locale && method_exists($entry, 'locale')) {
                    $entry->locale($this->locale);
                }
               // END: ADDED CODE
                return $this->$filter($entry);
            })->filter();
        } else {
            $this->data = $this->$filter($this->data);
        }

Not sure if this fixes it for other data, but it seems to work for collections.

Cheers

wanze avatar Feb 01 '22 15:02 wanze