<p>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.</p>

CC BY-SA 3.0
2017-06-20T04:05:24.583Z
2017-06-20T04:16:45.810Z
2017-06-20T04:16:45.810Z
Nomad Guy
<p>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.</p>

<pre><code>'api/news.json' =&gt; [
  'elementType' =&gt; ElementType::Entry,
  'criteria' =&gt; ['section' =&gt; 'news'],
  'transformer' =&gt; function(EntryModel $entry) {
    return [

      'id' =&gt; $entry-&gt;id,
      'title' =&gt; $entry-&gt;title,
      'slug' =&gt; $entry-&gt;slug,
      'postDate' =&gt; $entry-&gt;postDate,
      'url' =&gt; $entry-&gt;url,
      'jsonUrl' =&gt; UrlHelper::getUrl("api/news/{$entry-&gt;id}.json"),
      'description' =&gt; (string) $entry-&gt;newsDescription,
      'tags' =&gt; array_map( function(TagModel $tag) {
        return [
          'id' =&gt; $tag-&gt;id,
          'title' =&gt; $tag-&gt;title,
          'slug' =&gt; $tag-&gt;slug,
        ];
      }, $entry-&gt;newsTags-&gt;find()),
      'type' =&gt; array_map( function(CategoryModel $category) {
        return [
          'id' =&gt; $category-&gt;id,
          'title' =&gt; $category-&gt;title,
          'slug' =&gt; $category-&gt;slug,
        ];
      }, $entry-&gt;newsType-&gt;find()),

    ];
  },
],
</code></pre>

CC BY-SA 3.0
2017-06-19T20:37:57.760Z
I think you might need to use the relatedto feature, check this post: https://craftcms.stackexchange.com/questions/14436/calling-related-entries-using-the-element-api
Rick Kukiela
<plugin-elementapi><tags>
How do I retrieve tags and tag data for an entry in Element API?
# How do I retrieve tags and tag data for an entry in Element API?

(Asked by: username_0 on 2017-06-17)

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

    ];
  },
],

**Tags:** plugin-elementapi, tags
> **Comments:**
> **username_1**: I think you might need to use the relatedto feature, check this post: https://craftcms.stackexchange.com/questions/14436/calling-related-entries-using-the-element-api  

---

## 1 answers

### Answer by username_0 on 2017-06-20. Score: 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.


---

