1

I'm just wondering when it comes to Element API is there a way of returning a type of Category.

I'm returning all the other information fine.

enter image description here

'fees.json' => [
    'elementType' => 'Entry',
        'criteria' => [
            'title' => craft()->request->getQuery('title'),
            'section' => 'fees',
        ],
        'transformer' => function(EntryModel $entry) {
            foreach ($entry->fee as $block) {
                $matrixSections[] = [
                    'first' => $block->firstInstallment,
                    'second' => $block->secondInstallment,
                    'full' => $block->full,
                    'grant' => $block->feeCategory
                ];
            }
        return [
            'title' => $entry->title,
            'menu' => [
                'sections' => $matrixSections
            ]
        ];
    }
]

enter image description here

2

The Element API plugin returns JSON, so you'll need to be specific about what you want to return for your categories – i.e. you can't return a CategoryModel (or an array of CategoryModels) in full directly from a Categories field.

The easiest option is to use array_map() like this:

'first' => $block->firstInstallment,
'second' => $block->secondInstallment,
'full' => $block->full,
'grant' => array_map(function (CategoryModel $category) {
    return [
        'id' => $category->id,
        'title' => $category->title,
    ];
}, $block->categoriesField->find()),

Of course, if your individual category has a lot of fields, you can always create an external function in your elementapi.php file and use that for the array_map call, instead of inlining the function (the external function needs to live above the return ['endpoints']... part):

function getCategoryData(CategoryModel $category)
{
    return [
        'id' => $category->id,
        'title' => $category->title,
        ...
    ];
}

...

'grant' => array_map('getCategoryData', $block->categoriesField->find()),

Also, there's the $category->getAttributes() method which can be handy if you want to return all stock attributes (i.e. id, dateUpdated etc – pretty much everything sans the title, for some reason):

function getCategoryData(CategoryModel $category)
{
    return array_merge($category->getAttributes(), [
        'title' => $category->title,
        'someCustomField' => $category->someCustomField,
    ];
}
  • Hey Mats, declaring the $matrixSections outside of the loop didn't make a difference. The only one of those options that returned anything was the ids option. Any other idea on this? Appreciate the reply none the less. – andrew-caulfield Jul 27 '16 at 21:39
  • You're positive that the feeCategory field for the block in question actually has enabled categories selected? – Mats Mikkel Rummelhoff Jul 27 '16 at 22:06
  • 1
    I've added image to post to show that feeCategory has been added to the block. – andrew-caulfield Jul 27 '16 at 22:14
  • I had a case of stupid earlier – please see revised answer. I even tested it this time :) – Mats Mikkel Rummelhoff Jul 27 '16 at 22:20
  • No worries. Appreciate the time anyway. – andrew-caulfield Jul 28 '16 at 10:10
0

I'm sure the answer from Mats also works but I thought the cleaner way would be the following.

'fees.json' => [
        'elementType' => 'Entry',
            'criteria' => [
                'title' => craft()->request->getQuery('title'),
                'section' => 'fees',
            ],
            'transformer' => function(EntryModel $entry) {
            $matrixSections = [];
            foreach ($entry->fee as $block) {
            $items = [];

            foreach ($block->grantType as $itemEntry) {
                $items[] = [
                'title' => $itemEntry->title,
                ];
            }
            $matrixSections[] = [
                'first' => $block->firstInstallment,
                'second' => $block->secondInstallment,
                'full' => $block->full,

                'items' => $items,
            ];
            }
            return [
                'title' => $entry->title,
                'menu' => [
                    'sections' => $matrixSections,
                ]
            ];
        }
]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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