element-api icon indicating copy to clipboard operation
element-api copied to clipboard

Pagination No Longer Working after Update to v4.1.0

Open kmgdevelopment opened this issue 10 months ago • 1 comments

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

  • Craft version: 4.8.6
  • PHP version: 8.1.23
  • Database driver & version: MySQL 8.0.33
  • Plugins & versions: Amazon S3 2.2.1; Asset Rev 7.0.0; CKEditor 3.8.3; Super Table 3.0.12

kmgdevelopment avatar Mar 31 '24 19:03 kmgdevelopment

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.

i-just avatar May 02 '24 07:05 i-just

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

kmgdevelopment avatar May 21 '24 23:05 kmgdevelopment

Thanks for confirming!

i-just avatar May 22 '24 08:05 i-just