craftcms / element-api

Create a JSON API/Feed for your elements in Craft.
MIT License
498 stars 56 forks source link

Pagination No Longer Working after Update to v4.1.0 #188

Closed kmgdevelopment closed 1 month ago

kmgdevelopment commented 3 months ago

Description

After updating Element API to 4.1.0 the pagination on my endpoints no longer works. Following the "next" link to ?page=2 for example simply returns the page 1 results.

Here's my endpoint code:

return [
    'endpoints' => [
        'recipes.json' => function() {
            $entryCriteria = [
                'section' => 'recipes'
            ];

            // filter by search query
            $searchQuery = Craft::$app->request->getQueryParam('query');
            if($searchQuery) {
                $entryCriteria += [
                    'search' => $searchQuery,
                    'orderBy' => 'score'
                ];
            }

            // filter by category
            $categoryQuery = Craft::$app->request->getQueryParam('category');
            if($categoryQuery) {
                $operator = ['and'];
                $categoryList = explode(' ', $categoryQuery);

                $entryCriteria += [
                    'relatedTo' => array_merge($operator, $categoryList)
                ];
            }

            return [
                'elementType' => Entry::class,
                'elementsPerPage' => 12,
                'criteria' => $entryCriteria,
                'resourceKey' => 'entries',
                'transformer' => function(Entry $entry) {
                    return [
                        'id' => $entry->id,
                        'title' => $entry->title,
                        'uri' => $entry->uri,
                        'recipeImg' => $entry->recipeImg->one()->url
                    ];
                },
                'pretty' => true
            ];
        },
    ]
];

Steps to reproduce

  1. Update Element API from 3.0.1.1 to 4.1.0
  2. Access a query-string paginated page on your endpoint (for example: my-site.tld/endpoint.json?page=2)

Additional info

i-just commented 2 months ago

Hi, thanks for reaching out! I’m unable to replicate this.

Do you have more than 12 entries matching the criteria? If I only have 12 entries, the results don’t show the pagination link on page 1, but if I manually edit the URL to request page 2, the results from page 1 will be returned.

kmgdevelopment commented 1 month ago

Sorry for the late reply Iwona, this seems to have fixed itself. Thank you!

i-just commented 1 month ago

Thanks for confirming!