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?