<p>I managed to get access to the slug in the transformer function by using <code>use</code>. I'm not sure if it's the correct way but it works for now.</p>

<pre><code>'api/series/&lt;slug:{slug}&gt;.json' =&gt; function($slug) {
  // get category by slug
  $category = craft()-&gt;elements-&gt;getCriteria(ElementType::Category);
  $category-&gt;group = 'series';
  $category-&gt;slug = $slug;
  $category-&gt;first();

  return [
    'elementType' =&gt; ElementType::Entry,
    'criteria' =&gt; [
      'section' =&gt; ['lessons'],
      'relatedTo' =&gt; ['targetElement' =&gt; $series]
    ],
    'transformer' =&gt; function(EntryModel $entry) use ($category) {

      $firstCategory = $category-&gt;first();

      $cat = [
        'id' =&gt; $firstCategory-&gt;id,
        'slug' =&gt; $firstCategory-&gt;slug,
        'title' =&gt; $firstCategory-&gt;title
      ];

      return array_merge(structureEntry($entry), structureCategory($cat));
    }
  ];
}
</code></pre>

CC BY-SA 3.0
2017-09-07T16:41:03.797Z
2017-09-07T16:41:03.797Z
samput
<p>Each of my entries has multiple categories (e.g. series, disciplines, and stages). I can get a list of elements related to a single category but I'd also like to get the category information as well (title, summary etc). Ideally, I'd like to pass the <code>$slug</code> to the transformer so I can grab the category from there, is that possible?</p>

<p>Here's my current code for an endpoint from elementapi.php</p>

<pre><code>'api/series/&lt;slug:{slug}&gt;.json' =&gt; function($slug) {
  // get category by slug
  $series = craft()-&gt;elements-&gt;getCriteria(ElementType::Category);
  $series-&gt;group = 'series';
  $series-&gt;slug = $slug;
  $series-&gt;first();

  return [
    'elementType' =&gt; ElementType::Entry,
    'criteria' =&gt; [
      'section' =&gt; ['lessons'],
      'relatedTo' =&gt; ['targetElement' =&gt; $series]
    ],
    'transformer' =&gt; function(EntryModel $entry) {
      return structureEntry($entry);
    }
  ];
},

/*-------------------------------------------*/

function structureEntry($entry) {
  return [
    'id' =&gt; (int)$entry-&gt;id,
    'title' =&gt; $entry-&gt;title,
    'slug' =&gt; $entry-&gt;slug,
    'summary' =&gt; $entry-&gt;summary ? $entry-&gt;summary : null,
    'featuredImage' =&gt; $entry-&gt;featuredImage-&gt;first() ? getImageSizes($entry-&gt;featuredImage-&gt;first()) : null,
    'body' =&gt; $entry-&gt;body ? (string)$entry-&gt;body : null,
    'series' =&gt; $entry-&gt;series ? getCategoryData($entry-&gt;series) : null,
    'stage' =&gt; $entry-&gt;stage ? getCategoryData($entry-&gt;stage) : null,
    'disciplines' =&gt; $entry-&gt;discipline ? getCategoryData($entry-&gt;discipline) : null
  ];
}

function getCategoryData($categories) {
  $cats = array();

  for($i = 0; $i &lt; count($categories); $i++) {
    $cats[$i] = [
      'id' =&gt; (int)$categories[$i]-&gt;id,
      'title' =&gt; $categories[$i]-&gt;title,
      'slug' =&gt; $categories[$i]-&gt;slug,
      'summary' =&gt; $categories[$i]-&gt;summary
    ];
  }

  return $cats;
}
</code></pre>

<p>Ideally, I'd like to do something like this:</p>

<pre><code>'api/series/&lt;slug:{slug}&gt;.json' =&gt; function($slug) {
  // get category by slug
  $series = craft()-&gt;elements-&gt;getCriteria(ElementType::Category);
  $series-&gt;group = 'series';
  $series-&gt;slug = $slug;
  $series-&gt;first();

  return [
    'elementType' =&gt; ElementType::Entry,
    'criteria' =&gt; [
      'section' =&gt; ['lessons'],
      'relatedTo' =&gt; ['targetElement' =&gt; $series]
    ],
    'transformer' =&gt; function(EntryModel $entry) {

      $category = craft()-&gt;elements-&gt;getCriteria(ElementType::Category);
      $category-&gt;group = 'series';
      $category-&gt;slug = $slug;
      $category-&gt;first();

      return array_merge(structureEntry($entry), structureCategory($category));
    }
  ];
},
</code></pre>

<p>Any help would be appreciated, thanks!</p>

<plugin-elementapi><category>
Element API access param in transformer
# Element API access param in transformer

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

Each of my entries has multiple categories (e.g. series, disciplines, and stages). I can get a list of elements related to a single category but I'd also like to get the category information as well (title, summary etc). Ideally, I'd like to pass the $slug to the transformer so I can grab the category from there, is that possible?
Here's my current code for an endpoint from elementapi.php
'api/series/.json' => function($slug) {
  // get category by slug
  $series = craft()->elements->getCriteria(ElementType::Category);
  $series->group = 'series';
  $series->slug = $slug;
  $series->first();

  return [
    'elementType' => ElementType::Entry,
    'criteria' => [
      'section' => ['lessons'],
      'relatedTo' => ['targetElement' => $series]
    ],
    'transformer' => function(EntryModel $entry) {
      return structureEntry($entry);
    }
  ];
},

/*-------------------------------------------*/

function structureEntry($entry) {
  return [
    'id' => (int)$entry->id,
    'title' => $entry->title,
    'slug' => $entry->slug,
    'summary' => $entry->summary ? $entry->summary : null,
    'featuredImage' => $entry->featuredImage->first() ? getImageSizes($entry->featuredImage->first()) : null,
    'body' => $entry->body ? (string)$entry->body : null,
    'series' => $entry->series ? getCategoryData($entry->series) : null,
    'stage' => $entry->stage ? getCategoryData($entry->stage) : null,
    'disciplines' => $entry->discipline ? getCategoryData($entry->discipline) : null
  ];
}

function getCategoryData($categories) {
  $cats = array();

  for($i = 0; $i < count($categories); $i++) {
    $cats[$i] = [
      'id' => (int)$categories[$i]->id,
      'title' => $categories[$i]->title,
      'slug' => $categories[$i]->slug,
      'summary' => $categories[$i]->summary
    ];
  }

  return $cats;
}

Ideally, I'd like to do something like this:
'api/series/.json' => function($slug) {
  // get category by slug
  $series = craft()->elements->getCriteria(ElementType::Category);
  $series->group = 'series';
  $series->slug = $slug;
  $series->first();

  return [
    'elementType' => ElementType::Entry,
    'criteria' => [
      'section' => ['lessons'],
      'relatedTo' => ['targetElement' => $series]
    ],
    'transformer' => function(EntryModel $entry) {

      $category = craft()->elements->getCriteria(ElementType::Category);
      $category->group = 'series';
      $category->slug = $slug;
      $category->first();

      return array_merge(structureEntry($entry), structureCategory($category));
    }
  ];
},

Any help would be appreciated, thanks!

**Tags:** plugin-elementapi, category


---

## 1 answers

### Answer by username_0 on 2017-09-07. Score: 2

I managed to get access to the slug in the transformer function by using use. I'm not sure if it's the correct way but it works for now.
'api/series/.json' => function($slug) {
  // get category by slug
  $category = craft()->elements->getCriteria(ElementType::Category);
  $category->group = 'series';
  $category->slug = $slug;
  $category->first();

  return [
    'elementType' => ElementType::Entry,
    'criteria' => [
      'section' => ['lessons'],
      'relatedTo' => ['targetElement' => $series]
    ],
    'transformer' => function(EntryModel $entry) use ($category) {

      $firstCategory = $category->first();

      $cat = [
        'id' => $firstCategory->id,
        'slug' => $firstCategory->slug,
        'title' => $firstCategory->title
      ];

      return array_merge(structureEntry($entry), structureCategory($cat));
    }
  ];
}


---

