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()),


1 Answer 1


I took a break from this project. Came back, looked at the code, and saw a simple overlooked error - had a ")" instead if "}". Grrr. Starring too much at pixels lately. Fixed the code. So it's all working now. The array_map with the Tag Model does work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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