1

I tried to build JSON-API which returns all categories, whose result will be cascaded like "nav" tag with craft twig templating (see: https://craftcms.com/docs/templating/craft.categories ).

to get all root categories (which has no parent category, and which can have child categories), I've tried to filter giving within "criteria" something like that, but the result was just returned all categories:

'endpoints' => [
    'api/categories' => [
        'elementType' => ElementType::Category,
        'criteria' => [ 'descendantOf' => null ],
        'transformer' => function($entry) {
            return [
                'id' => $entry->id,
                'title' => $entry->title,
                'parent' => ($entry->parent) ? $entry->parent->id : null,
                'children' => ($entry->children) ?
                    children_in_array($entry->children) : null,
            ];
        },
    ]
]

function children_in_array() returns same structure like result of 'transformer' function (id, title, parent, children).

How can I get only root categories in this way?

or is there other solution all cascaded category list in JSON format?

1

To get all root categories you can use: 'criteria' => ['level' => 1]

  • thank you, and sorry for replying lately... but it works fine! – Yoshito Maeoka Mar 21 '18 at 15:10

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.