3

I'm not sure what I'm missing, but I'm getting an empty object back from the Element API for the summary and/or body for any given entry.

elementapi.php

...
'transformer' => function (EntryModel $entry) {
    return [
        'title' => $entry->title,
        'url' => $entry->url,
        'summary' => $entry->summary
    ];
},
...

Here is the json I am getting back:

{"data":[{"title":"My Title","url":"http:\/\/local.mysite.dev\/section\/my-entry","summary":{}}

EDIT/SOLUTION

Thank you again @Oliver!

I wanted to update my post in the event someone else came across it. I am using mustache.js to render my html from the json that is getting output via the ElementAPI.

By casting the summary as a string, you will need to tell mustache to parse the html by using three curly-brackets instead of the usual two.

mustache

<p>Title: {{title}}</p>
<p>URL: {{url}}</p>
{{{ summary }}}

Otherwise you will end up with something that looks like this:

Title: My Title
URL: www.mysite.com
<p>This is my summary</p>
Damon
  • 4,706
  • 1
  • 20
  • 36

1 Answers1

6

Try this:

...
'transformer' => function (EntryModel $entry) {
    return [
        'title' => $entry->title,
        'url' => $entry->url,
        'summary' => (string) $entry->summary
    ];
},
...
Oli
  • 7,495
  • 9
  • 17
  • Hi Oliver - thank you, that did it! I don't get it though. Was/is it something I did that I have to cast the summary as a string? – Damon Nov 29 '15 at 01:10
  • Hey, I can't remember where I found this but no, you didn't do anything wrong. Rich text field just need to be cast as string to return the html as a string :) – Oli Nov 30 '15 at 12:19