How to fetch matrix block items with the ElementAPI I have a question regarding the ElementAPI:

I am sucessfully using the ElementAPI to fetch assets related to an entry. Now I have a matrix field where a user can add multiple galleries (multiple block of selected assets) and i want to retrieve the images. But I can't figure out how to use it for nested matrix blocks...

This is how I fetch the data with ElementAPI from non-matrix assets fields (elementapi.php config)

'endpoints' =&gt; [
    'api/v1/installation-views/&lt;sourceEntryId:\d+&gt;/&lt;fieldHandle:[A-Za-z]+&gt;' =&gt; function($sourceEntryId, $fieldHandle) {
        return [
            'elementType' =&gt; 'Asset',
            'criteria' =&gt; [
                'relatedTo' =&gt; [
                    'sourceElement' =&gt; $sourceEntryId,
                    'field' =&gt; $fieldHandle
                ],
                'kind' =&gt; 'image'
            ],
            'transformer' =&gt; function(AssetFileModel $asset) {
                return [
                    'id' =&gt; $asset-&gt;id,
                    'title' =&gt; $asset-&gt;title,
                    ...
                ];
            },
        ];
    }
]


How would I do that for asset fields nested inside a matrix block? I guess I can't use the nested field handle as handle?
Matrix blocks are elements just like entries and assets, so this is just a matter of setting the elementType to 'MatrixBlock', and adjusting your criteria to fetch the block you’re after.

The simplest approach would be to pass the Matrix block ID in the URL, and apply it to the id criteria parameter.

'endpoints' =&gt; [
    'matrix-blocks/&lt;blockId:\d+&gt;' =&gt; function($blockId) {
        return [
            'elementType' =&gt; 'MatrixBlock',
            'criteria' =&gt; [
                'id' =&gt; $blockId
            ],
            'first' =&gt; true, // Just return one Matrix block
            'transformer' =&gt; function(MatrixBlock $block) {
                $assetsInfo = [];

                foreach ($block-&gt;myAssetsfield as $asset) {
                    $assetsInfo[] = [
                        'url' =&gt; $asset-&gt;url,
                        'title' =&gt; $asset-&gt;title,
                        // ...
                    ];
                }

                return [
                    'id' =&gt; $block-&gt;id,
                    'assets' =&gt; $AssetsInfo,
                    // ...
                ];
            },
        ];
    }
]


You can also use the following criteria parameters to narrow down the search:


fieldId (the Matrix field ID)
ownerId (the owner element ID, e.g. your entry’s ID)
ownerLocale (the locale of the owner element that the block should have been saved to)
type (the block type handle)