HAL Entity with hydrated collection are missing paging details

Good day,

I’am wondering if it’s possible or supported to provide paging details of entity sub collection or should I change approach.

As return example of my PlaceEntity and PlaceMetaCollection (Laminas\Paginator\Paginator), I got:

{
    "status": true,
    "created_on": "2022-01-01T20:23:03+00:00",
    "updated_on": "2022-03-11T15:02:12+00:00",
    "name": "My place",
    "slug": "my-place-slug",
    "_embedded": {
        "place_meta": [
            {
                "slug": "my-place-meta-slug",
                "name": "My meta name",
                "_links": {
                    "self": {
                        "href": "http://localhost:8080/places/my-place-name/metas/my-place-meta-slug"
                    }
                }
            },
            ...
        ]
    },
    "_links": {
        "self": {
            "href": "http://localhost:8080/places/my-place-slug"
        }
    }
}

But I would also looking to inject paging details for place_meta collection instead of rendering them has array of entities.

{
...
    "page_count": 5,
    "page_size": 5,
    "total_items": 25,
    "page": 1
}

Thank you

Hey @jpoitras ,

sorry that it took that long. I was chilling in my hollidays.

Although I can understand your approach very well, I would not recommend this approach because it is not at all resource-saving. You would have to recursively parse the entity and provide each collection with corresponding attributes. This is very time-consuming because many database queries are necessary to generate the pagination for all relevant collections.

A better approach would be calling a single resource for retrieving all place meta data with the expected pagination attributes. For example: /place/{identifier}/meta to get all meta data for a specific place.

Despite all these objections, you can of course realise your plan by writing your own HAL event listener that returns all relevant collections as you would like them to be. The laminas-api-tools/api-tools-hal contains several events, that will be triggered during HAL the rendering process. I 'd suggest to use the renderEntity.post event for manipulating the output of included collections of your entity. Just transform all included collections to an instance of Laminas\Paginator\Paginator and have a look on Laminas\ApiTools\Hal\Plugin::renderCollection(). This method explains, how the HAL plugin renders collections.

Thank you @ezkimo for your time and proposed solution.

I did changed the approach the way you have suggested with single resource call.