5

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' => [
    'api/v1/installation-views/<sourceEntryId:\d+>/<fieldHandle:[A-Za-z]+>' => function($sourceEntryId, $fieldHandle) {
        return [
            'elementType' => 'Asset',
            'criteria' => [
                'relatedTo' => [
                    'sourceElement' => $sourceEntryId,
                    'field' => $fieldHandle
                ],
                'kind' => 'image'
            ],
            'transformer' => function(AssetFileModel $asset) {
                return [
                    'id' => $asset->id,
                    'title' => $asset->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?

1 Answer 1

3

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' => [
    'matrix-blocks/<blockId:\d+>' => function($blockId) {
        return [
            'elementType' => 'MatrixBlock',
            'criteria' => [
                'id' => $blockId
            ],
            'first' => true, // Just return one Matrix block
            'transformer' => function(MatrixBlock $block) {
                $assetsInfo = [];

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

                return [
                    'id' => $block->id,
                    'assets' => $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)
2
  • Thank you! Seems easier than I thought. One little question. My matrix block has an assets field. Can I get them directly from $block or would I have to make an additional query?
    – exophunk
    Commented Oct 20, 2015 at 21:29
  • @RobertKrieg Just updated my answer with an example of adding assets info to the response. Commented Oct 20, 2015 at 21:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.