<p>I'm using Craft as a headless CMS, so I wanted full control over the JSON it was returning. I didn't figure out how to do this with Element API, but it's very simple to write your own plugin that turns entries into JSON. Craft already includes the code to look up entries, so it's easy.</p>

<p>A basic controller might look something like:
    

<pre><code>class MyCustomPlugin_SomeResourceController {

    public function actionWidgets() {
        $criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
        $widgetEntries = $criteria-&gt;section("resources")-&gt;type("widget")-&gt;find();

        // map entries to PHP objects
        $widgetObjects = array_map(function($entry) {
            return [
                "name" =&gt; $entry-&gt;title
            ];
        }, $widgetEntries);

        $this-&gt;returnJson([
            "widgets" =&gt; $widgetObjects,
            "anotherThing" =&gt; "somethingElse"
        ]);
    }

}
</code></pre>

<p>In the actual app, I'm combining several different types of entries into one endpoint, so I extracted the reading and formatting into services, which I combine in various controller actions. </p>

<p>Presumably, Element API has some other features that I'm missing out on, but I haven't noticed. </p>

CC BY-SA 3.0
2018-01-11T13:24:55.690Z
yeah, i gave up trying to find a solution and ended up creating my own plugin.
BillyMedia
CC BY-SA 3.0
2018-01-10T18:39:42.197Z
2018-01-10T18:39:42.197Z
michael_r
<p>I need to return two types of entry to an external service, and am trying to do so in one endpoint. </p>

<p>I am able to return all of the instances of each type, but it returns an array of mixed types.</p>

<pre><code>"api/myEndpoint" =&gt; function() {
        return [
            "criteria" =&gt; [
                "type" =&gt; ["typeOne", "typeTwo"],
            ],
            "transformer" =&gt; new MyTransformer()
        ];
    }
</code></pre>

<p>I'm trying to achieve: <code>{ "data": { "typeOne": [{...}, {...}], "typeTwo": [{...}, {...}]}}</code></p>

<p>I can think of various workarounds for this specific case. It would be easy enough to add a type field within the Transformer, and sort them on the other side. Or, simply make two calls. However, our team was planning on using Craft as a headless CMS for future projects, and it's important to me  be able to control the structure of Element API's JSON response. </p>

CC BY-SA 3.0
2018-01-09T17:24:52.803Z
dont suppose you ever got to the bottom of this one did you? having the same problem.
BillyMedia
<plugin-elementapi>
With Element API, can I combine two entry types into a JSON object, keyed by type?
# With Element API, can I combine two entry types into a JSON object, keyed by type?

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

I need to return two types of entry to an external service, and am trying to do so in one endpoint. 
I am able to return all of the instances of each type, but it returns an array of mixed types.
"api/myEndpoint" => function() {
        return [
            "criteria" => [
                "type" => ["typeOne", "typeTwo"],
            ],
            "transformer" => new MyTransformer()
        ];
    }

I'm trying to achieve: { "data": { "typeOne": [{...}, {...}], "typeTwo": [{...}, {...}]}}
I can think of various workarounds for this specific case. It would be easy enough to add a type field within the Transformer, and sort them on the other side. Or, simply make two calls. However, our team was planning on using Craft as a headless CMS for future projects, and it's important to me  be able to control the structure of Element API's JSON response.

**Tags:** plugin-elementapi

> **Comments:**
> **username_1**: dont suppose you ever got to the bottom of this one did you? having the same problem.  

---

## 1 answers

### Answer by username_0 on 2018-01-10. Score: 2 | Accepted answer

I'm using Craft as a headless CMS, so I wanted full control over the JSON it was returning. I didn't figure out how to do this with Element API, but it's very simple to write your own plugin that turns entries into JSON. Craft already includes the code to look up entries, so it's easy.
A basic controller might look something like:
    

class MyCustomPlugin_SomeResourceController {

    public function actionWidgets() {
        $criteria = craft()->elements->getCriteria(ElementType::Entry);
        $widgetEntries = $criteria->section("resources")->type("widget")->find();

        // map entries to PHP objects
        $widgetObjects = array_map(function($entry) {
            return [
                "name" => $entry->title
            ];
        }, $widgetEntries);

        $this->returnJson([
            "widgets" => $widgetObjects,
            "anotherThing" => "somethingElse"
        ]);
    }

}

In the actual app, I'm combining several different types of entries into one endpoint, so I extracted the reading and formatting into services, which I combine in various controller actions. 
Presumably, Element API has some other features that I'm missing out on, but I haven't noticed.

> **Comments:**
> **username_1**: yeah, i gave up trying to find a solution and ended up creating my own plugin.  

---

