Skip to content

Commit

Permalink
Merge pull request #30 from tv2regionerne/defensive-collection-fallback
Browse files Browse the repository at this point in the history
Check if the fallback collection exists
  • Loading branch information
SylvesterDamgaard authored Sep 3, 2024
2 parents e9b2ce3 + dbe670a commit 5ca1d3b
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions src/Tags/StatamicCuratedCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Contracts\Pagination\Paginator;
use Statamic\Data\DataCollection;
use Statamic\Facades\Collection;
use Statamic\Facades\Entry;
use Statamic\Tags\Concerns\GetsQueryResults;
use Statamic\Tags\Concerns\OutputsItems;
Expand Down Expand Up @@ -87,20 +88,23 @@ public function wildcard($tag)

if (! $results instanceof Paginator) {
if ($this->params->get('fallback', false) && count($entries) < $limit) {
$fallbackEntries = Entry::query()
->where('collection', $curatedCollection->fallback_collection)
->whereStatus('published')
->whereNotIn('id', $ids ?? [])
->limit($limit - count($entries))
->orderBy($curatedCollection->fallback_sort_field, $curatedCollection->fallback_sort_direction)
->get()
->transform(function ($e) {
$e->set('curated_collection_source', 'fallback');

return $e;
});

$entries = $entries->concat($fallbackEntries);
$collection = Collection::findByHandle($curatedCollection->fallback_collection);
if ($collection) {
$fallbackEntries = Entry::query()
->where('collection', $curatedCollection->fallback_collection)
->whereStatus('published')
->whereNotIn('id', $ids ?? [])
->limit($limit - count($entries))
->orderBy($curatedCollection->fallback_sort_field, $curatedCollection->fallback_sort_direction)
->get()
->transform(function ($e) {
$e->set('curated_collection_source', 'fallback');

return $e;
});

$entries = $entries->concat($fallbackEntries);
}
}
}

Expand Down

0 comments on commit 5ca1d3b

Please sign in to comment.