<p>The <code>$entry</code> variable that is passed to your endpoint’s transformer function will be an <a href="http://buildwithcraft.com/classreference/models/EntryModel">EntryModel</a> object - the same type of object you use when interacting with entries in your templates. So everything you can do in your templates you can also do from this function.</p>

<p>The easiest way to get a Structure section entry’s children is with the <a href="http://buildwithcraft.com/classreference/models/BaseElementModel#getChildren-detail">getChildren()</a> function, which will actually give you a new ElementCriteriaModel object with the <code>descendantOf</code> and <code>descendantDist</code> params pre-set for you. So here’s what your transformer function might look like:</p>

<pre class="lang-php prettyprint-override"><code>'transformer' =&gt; function(EntryModel $state) {
  $locations = $state-&gt;getChildren()-&gt;type('location')-&gt;find();
  $locationsInfo = [];

  foreach ($locations as $location) {
    $locationsInfo[] = [
      'title' =&gt; $location-&gt;title,
      'url' =&gt; $location-&gt;url,
      // ...
    ];
  }

  return [
    'title' =&gt; $state-&gt;title,
    'url' =&gt; $state-&gt;url,
    'latitude' =&gt; $state-&gt;latitude,
    'longitude' =&gt; $state-&gt;longitude,
    'jsonUrl' =&gt; UrlHelper::getUrl("locations/{$state-&gt;id}.json"),
    'locations' =&gt; $locationsInfo
  ];
},
</code></pre>

<p>(Since you’re dealing with multiple entries within your transformer function, I also went ahead and renamed <code>$entry</code> to <code>$state</code>, just so it didn’t get confusing.)</p>

CC BY-SA 3.0
2015-08-20T19:26:25.230Z
This worked great and makes a lot of sense to structure it that way. Thanks!
coryetzkorn
CC BY-SA 3.0
2015-08-20T19:10:48.240Z
2015-08-20T19:10:48.240Z
Brandon Kelly
<p>I have a structure called "locations" that contains two entry types:</p>

<ul>
<li>State</li>
<li>Location</li>
</ul>

<p>I'd like to access specific states at the URL <code>locations/ny.json</code>, for example, and get a JSON object with all the location entries nested under that state.</p>

<p>The entries are already properly nested for HTML output, but I'm trying to get the JSON to match.</p>

<p>This is my <code>elementsapi.php</code> file right now:</p>

<pre class="lang-php prettyprint-override"><code>&lt;?php
namespace Craft;

return [
  'endpoints' =&gt; [
    'locations.json' =&gt; [
      'elementType' =&gt; 'Entry',
      'criteria' =&gt; [
        'section' =&gt; 'locations',
        'type' =&gt; 'location'
      ],
      'transformer' =&gt; function(EntryModel $entry) {
        return [
          'title' =&gt; $entry-&gt;title,
          'url' =&gt; $entry-&gt;url,
          'latitude' =&gt; $entry-&gt;latitude,
          'longitude' =&gt; $entry-&gt;longitude,
          'jsonUrl' =&gt; UrlHelper::getUrl("locations/{$entry-&gt;id}.json")
        ];
      },
    ],
    'locations/&lt;slug:{slug}&gt;.json' =&gt; function($slug) {
      return [
        'elementType' =&gt; 'Entry',
        'criteria' =&gt; [
          'section' =&gt; 'locations',
          'type' =&gt; 'state',
          'slug' =&gt; $slug
        ],
        'transformer' =&gt; function(EntryModel $entry) {
          return [
            'title' =&gt; $entry-&gt;title,
            'url' =&gt; $entry-&gt;url,
            'latitude' =&gt; $entry-&gt;latitude,
            'longitude' =&gt; $entry-&gt;longitude,
            'jsonUrl' =&gt; UrlHelper::getUrl("locations/{$entry-&gt;id}.json")
          ];
        },
      ];
    },
  ]
];
</code></pre>

<p>The second endpoint is currently returning a single state entry, but I would like it to return all locations that are a descendant of that state. </p>

<p>Is this possible using a criteria param such as <code>descendantOf</code>?
What criteria params are available?</p>

<json><plugin-elementapi>
What criteria can the Element API plugin accept?
# What criteria can the Element API plugin accept?

(Asked by: username_0 on 2015-08-20)

I have a structure called "locations" that contains two entry types:

State
Location

I'd like to access specific states at the URL locations/ny.json, for example, and get a JSON object with all the location entries nested under that state.
The entries are already properly nested for HTML output, but I'm trying to get the JSON to match.
This is my elementsapi.php file right now:
 [
    'locations.json' => [
      'elementType' => 'Entry',
      'criteria' => [
        'section' => 'locations',
        'type' => 'location'
      ],
      'transformer' => function(EntryModel $entry) {
        return [
          'title' => $entry->title,
          'url' => $entry->url,
          'latitude' => $entry->latitude,
          'longitude' => $entry->longitude,
          'jsonUrl' => UrlHelper::getUrl("locations/{$entry->id}.json")
        ];
      },
    ],
    'locations/.json' => function($slug) {
      return [
        'elementType' => 'Entry',
        'criteria' => [
          'section' => 'locations',
          'type' => 'state',
          'slug' => $slug
        ],
        'transformer' => function(EntryModel $entry) {
          return [
            'title' => $entry->title,
            'url' => $entry->url,
            'latitude' => $entry->latitude,
            'longitude' => $entry->longitude,
            'jsonUrl' => UrlHelper::getUrl("locations/{$entry->id}.json")
          ];
        },
      ];
    },
  ]
];

The second endpoint is currently returning a single state entry, but I would like it to return all locations that are a descendant of that state. 
Is this possible using a criteria param such as descendantOf?
What criteria params are available?

**Tags:** json, plugin-elementapi


---

## 1 answers

### Answer by username_1 on 2015-08-20. Score: 6 | Accepted answer

The $entry variable that is passed to your endpoint’s transformer function will be an EntryModel object - the same type of object you use when interacting with entries in your templates. So everything you can do in your templates you can also do from this function.
The easiest way to get a Structure section entry’s children is with the getChildren() function, which will actually give you a new ElementCriteriaModel object with the descendantOf and descendantDist params pre-set for you. So here’s what your transformer function might look like:
'transformer' => function(EntryModel $state) {
  $locations = $state->getChildren()->type('location')->find();
  $locationsInfo = [];

  foreach ($locations as $location) {
    $locationsInfo[] = [
      'title' => $location->title,
      'url' => $location->url,
      // ...
    ];
  }

  return [
    'title' => $state->title,
    'url' => $state->url,
    'latitude' => $state->latitude,
    'longitude' => $state->longitude,
    'jsonUrl' => UrlHelper::getUrl("locations/{$state->id}.json"),
    'locations' => $locationsInfo
  ];
},

(Since you’re dealing with multiple entries within your transformer function, I also went ahead and renamed $entry to $state, just so it didn’t get confusing.)

> **Comments:**
> **username_0**: This worked great and makes a lot of sense to structure it that way. Thanks!  

---

