In Element API v1, I am setting up an endpoint that is trying to expose a news entry including the tags (title, id, slug) associated with it. Thought it could be set up similarly to how you can retrieve categories using an array_map. But my code doesn't seem to work for tags.
'api/news.json' => [
'elementType' => ElementType::Entry,
'criteria' => ['section' => 'news'],
'transformer' => function(EntryModel $entry) {
return [
'id' => $entry->id,
'title' => $entry->title,
'slug' => $entry->slug,
'postDate' => $entry->postDate,
'url' => $entry->url,
'jsonUrl' => UrlHelper::getUrl("api/news/{$entry->id}.json"),
'description' => (string) $entry->newsDescription,
'tags' => array_map( function(TagModel $tag) {
return [
'id' => $tag->id,
'title' => $tag->title,
'slug' => $tag->slug,
];
}, $entry->newsTags->find()),
'type' => array_map( function(CategoryModel $category) {
return [
'id' => $category->id,
'title' => $category->title,
'slug' => $category->slug,
];
}, $entry->newsType->find()),
];
},
],