Currently involved with handling the CategoryModel and using this code
return [
'endpoints' => [
// Category
'category/<slug:{slug}>.json' => function($slug) {
require craft()->path->getConfigPath().'controllers/CategoryTransformer.php';
return [
'elementType' => 'Category',
'criteria' => ['group' => $slug],
'transformer' => new CategoryTransformer(),
];
},
];
refering to CategoryTransformer.php
class CategoryTransformer extends CategoryFieldTransformer
{
var $entry, $typeMedia;
public function transform(CategoryModel $entry)
{
echo $entry['uri']."
";
$child = $entry->getChildren();
}
}
It works all though I am not having success with been able to build a hierarchical object that would have parent and child nodes for the API to return. I made a modest attempt at building the categories in another area of the API interface but did not return what I was hoping for.
public function type_categories($mixedArray){
$categories = array();
foreach($mixedArray as $rkey => $rvalue){
$level = $mixedArray[$rkey]->level - 1;
$argt = array(
'id' => $mixedArray[$rkey]->id,
'uri' => $mixedArray[$rkey]->uri,
'title' => $mixedArray[$rkey]->title,
'level' => $mixedArray[$rkey]->level,
'slug' => $mixedArray[$rkey]->slug,
'groupId' => $mixedArray[$rkey]->groupId,
'level' => $mixedArray[$rkey]->level
);
// Children
$obg = $mixedArray[$rkey]->children;
$kids = array();
foreach($obg as $kid => $kidval){
$kids[] = array(
'id' => $obg[$kid]->id,
'uri' => $obg[$kid]->uri,
'title' => $obg[$kid]->title,
'level' => $obg[$kid]->level,
'slug' => $obg[$kid]->slug,
'groupId' => $obg[$kid]->groupId,
'level' => $obg[$kid]->level
);
}
$argt['children'] = $kids;
$categories[$level][] = $argt;
}
return $categories;
}
I as well took a peek at CategoriesController.php in the core and couldn't find anything that could assist me. It ends with Twig handling the object.
Is it possible if anyone has done something similar that I may reference to build a clean object which is intuitive for someone to loop on in JSON format?
Following is an example of the intended hierarchy:
stdClass Object
(
[0] => Array
(
[id] => 1
[uri] => cars
[title] => Cars
[children] => Array
(
[0] => Array
(
[id] => 4
[uri] => bmw
[title] => BMW
)
[1] => Array
(
[id] => 6
[uri] => audi
[title] => Audi
)
[2] => Array
(
[id] => 7
[uri] => vw
[title] => VW
[children] => Array
(
[0] => Array
(
[id] => 50
[uri] => hatchback
[title] => Hatchback
)
[1] => Array
(
[id] => 50
[uri] => sedan
[title] => Sedan
)
)
)
)
)
[1] => Array
(
[id] => 1
[uri] => trucks
[title] => trucks
)
)
Thanks