Answers.Body: 
  
  <p>I've determined what I think to be the best way to dynamically add Element's field content values to my responses.</p>
  
  <p>First off, the reason why field content values are being returned using <code>$element-&gt;getContent()</code> regardless of the Element relationship with the fields, is because the content table has field values as columns themselves. So when a Content model is populated, all fields are included. If I setup a field for an Entry, it will appear as a field for a User when using this method.</p>
  
  <p>My solution:</p>
  
  <pre class="lang-php prettyprint-override"><code>$entries = array();
  
  $results = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry)-&gt;find();
  
  foreach ($results as $model) {
      $entry = array();
  
      $attributes = $model-&gt;getAttributes();
  
      foreach ($attributes as $name =&gt; $value) {
          $entry[$name] = $value; // Do some transformation here (type casting, etc.)
      }
  
      $content = $model-&gt;getContent(); // No extra DB queries here - Model is already populated with content values
  
      $fields = array_map(function (FieldLayoutFieldModel $model) {
          return $model-&gt;getField();
      }, $model-&gt;getFieldLayout()-&gt;getFields());
  
      foreach ($fields as $field) {
          $entry[$field-&gt;handle] = $content-&gt;{$field-&gt;handle};
      }
  
      $entries[] = $entry;
  }
  
  return $this-&gt;returnJson(array(
      'entries' =&gt; $entries,
  ));
  </code></pre>
  
  <p>In my particular development, I've separated a lot of the above logic into services, which I'm using like Transformers.</p>
  
  <p>This helps me deliver field content values through my API. I spent hours pouring through source code, and I'm pretty confident this is the best solution based on efficiency, best practices, and similarity with logic concepts found throughout built-in services and models. Let me know what you guys think.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-22T19:17:21.550Z
Answers.LastActivityDate: 2014-07-22T19:17:21.550Z
Answers.UserDisplayName: Matt
Body: 
  
  <p>I'm creating a HTTP RESTful API plugin for Craft. In this case, I'm trying to return Elements (based on criteria), and include their content (via <code>$element-&gt;getContent()</code>).</p>
  
  <p>By default, if I return just the Elements collection returned by <code>craft()-&gt;elements-&gt;getCriteria($type)-&gt;find()</code>, the content is not included.</p>
  
  <p>I'm also running each Element through a Transformer which uses <code>ModelHelper::packageAttributeValue($value)</code>, and converts attributes to their proper PHP type using attribute configs.</p>
  
  <p>I'm not sure the best way to go about including content in my responses dynamically. Suggestions?</p>
  
  <p>Edit:</p>
  
  <p>I've just found out that <code>$element-&gt;getContent()</code> returns fields that aren't even associated with that particular element. Weird. Anybody know anything about this? I've instead resorted to using:</p>
  
  <pre class="lang-php prettyprint-override"><code>$fields = array_map(function (FieldLayoutFieldModel $model) {
      return $model-&gt;getField();
  }, $element-&gt;getFieldLayout()-&gt;getFields());
  </code></pre>
  
  <p>Not sure what's best here.</p>
  
Tags: <plugins><content>
Title: What is the best way to include an Element's content?
ThreadText: # What is the best way to include an Element's content?
  
  (Asked by: username_0 on 2014-07-22)
  
  I'm creating a HTTP RESTful API plugin for Craft. In this case, I'm trying to return Elements (based on criteria), and include their content (via `$element->getContent()`).
  
  By default, if I return just the Elements collection returned by `craft()->elements->getCriteria($type)->find()`, the content is not included.
  
  I'm also running each Element through a Transformer which uses `ModelHelper::packageAttributeValue($value)`, and converts attributes to their proper PHP type using attribute configs.
  
  I'm not sure the best way to go about including content in my responses dynamically. Suggestions?
  
  Edit:
  
  I've just found out that `$element->getContent()` returns fields that aren't even associated with that particular element. Weird. Anybody know anything about this? I've instead resorted to using:
  
  ```php
  $fields = array_map(function (FieldLayoutFieldModel $model) {
      return $model->getField();
  }, $element->getFieldLayout()->getFields());
  
  ```
  
  Not sure what's best here.
  
  **Tags:** plugins, content
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2014-07-22. Score: 4
  
  I've determined what I think to be the best way to dynamically add Element's field content values to my responses.
  
  First off, the reason why field content values are being returned using `$element->getContent()` regardless of the Element relationship with the fields, is because the content table has field values as columns themselves. So when a Content model is populated, all fields are included. If I setup a field for an Entry, it will appear as a field for a User when using this method.
  
  My solution:
  
  ```php
  $entries = array();
  
  $results = craft()->elements->getCriteria(ElementType::Entry)->find();
  
  foreach ($results as $model) {
      $entry = array();
  
      $attributes = $model->getAttributes();
  
      foreach ($attributes as $name => $value) {
          $entry[$name] = $value; // Do some transformation here (type casting, etc.)
      }
  
      $content = $model->getContent(); // No extra DB queries here - Model is already populated with content values
  
      $fields = array_map(function (FieldLayoutFieldModel $model) {
          return $model->getField();
      }, $model->getFieldLayout()->getFields());
  
      foreach ($fields as $field) {
          $entry[$field->handle] = $content->{$field->handle};
      }
  
      $entries[] = $entry;
  }
  
  return $this->returnJson(array(
      'entries' => $entries,
  ));
  
  ```
  
  In my particular development, I've separated a lot of the above logic into services, which I'm using like Transformers.
  
  This helps me deliver field content values through my API. I spent hours pouring through source code, and I'm pretty confident this is the best solution based on efficiency, best practices, and similarity with logic concepts found throughout built-in services and models. Let me know what you guys think.
  
  
  ---
  
  