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>

1 Answer 1

6

Try this:

...
'transformer' => function (EntryModel $entry) {
    return [
        'title' => $entry->title,
        'url' => $entry->url,
        'summary' => (string) $entry->summary
    ];
},
...
2
  • 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
    Commented Nov 29, 2015 at 1: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
    Commented Nov 30, 2015 at 12:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.