Answers.Body: <p>The Element API plugin returns JSON, so you'll need to be specific about what you want to return for your categories – i.e. you can't return a CategoryModel (or an array of CategoryModels) in full directly from a Categories field.</p>
  
  <p>The easiest option is to use <a href="http://php.net/manual/en/function.array-map.php" rel="nofollow"><code>array_map()</code></a> like this:</p>
  
  <pre><code>'first' =&gt; $block-&gt;firstInstallment,
  'second' =&gt; $block-&gt;secondInstallment,
  'full' =&gt; $block-&gt;full,
  'grant' =&gt; array_map(function (CategoryModel $category) {
      return [
          'id' =&gt; $category-&gt;id,
          'title' =&gt; $category-&gt;title,
      ];
  }, $block-&gt;categoriesField-&gt;find()),
  </code></pre>
  
  <p>Of course, if your individual category has a lot of fields, you can always create an external function in your <code>elementapi.php</code> file and use that for the <code>array_map</code> call, instead of inlining the function (the external function needs to live above the <code>return ['endpoints']...</code> part):</p>
  
  <pre><code>function getCategoryData(CategoryModel $category)
  {
      return [
          'id' =&gt; $category-&gt;id,
          'title' =&gt; $category-&gt;title,
          ...
      ];
  }
  
  ...
  
  'grant' =&gt; array_map('getCategoryData', $block-&gt;categoriesField-&gt;find()),
  </code></pre>
  
  <p>Also, there's the <code>$category-&gt;getAttributes()</code> method which can be handy if you want to return all stock attributes (i.e. <code>id</code>, <code>dateUpdated</code> etc – pretty much everything sans the title, for some reason):</p>
  
  <pre><code>function getCategoryData(CategoryModel $category)
  {
      return array_merge($category-&gt;getAttributes(), [
          'title' =&gt; $category-&gt;title,
          'someCustomField' =&gt; $category-&gt;someCustomField,
      ];
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-07-27T21:39:22.060Z
Answers.Text: Hey Mats, declaring the $matrixSections outside of the loop didn't make a difference. The only one of those options that returned anything was the ids option. Any other idea on this? Appreciate the reply none the less.
Answers.UserDisplayName: andrew-caulfield
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-07-27T22:06:19.760Z
Answers.Text: You're positive that the `feeCategory` field for the block in question actually has enabled categories selected?
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-07-27T22:14:43.523Z
Answers.Text: I've added image to post to show that feeCategory has been added to the block.
Answers.UserDisplayName: andrew-caulfield
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-07-27T22:20:48.117Z
Answers.Text: I had a case of stupid earlier – please see revised answer. I even tested it this time :)
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-07-28T10:10:19.187Z
Answers.Text: No worries. Appreciate the time anyway.
Answers.UserDisplayName: andrew-caulfield
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-07-27T21:22:45.040Z
Answers.LastActivityDate: 2016-07-27T22:25:21.423Z
Answers.LastEditDate: 2016-07-27T22:25:21.423Z
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.Body: <p>I'm sure the answer from Mats also works but I thought the cleaner way would be the following.</p>
  
  <pre><code>'fees.json' =&gt; [
          'elementType' =&gt; 'Entry',
              'criteria' =&gt; [
                  'title' =&gt; craft()-&gt;request-&gt;getQuery('title'),
                  'section' =&gt; 'fees',
              ],
              'transformer' =&gt; function(EntryModel $entry) {
              $matrixSections = [];
              foreach ($entry-&gt;fee as $block) {
              $items = [];
  
              foreach ($block-&gt;grantType as $itemEntry) {
                  $items[] = [
                  'title' =&gt; $itemEntry-&gt;title,
                  ];
              }
              $matrixSections[] = [
                  'first' =&gt; $block-&gt;firstInstallment,
                  'second' =&gt; $block-&gt;secondInstallment,
                  'full' =&gt; $block-&gt;full,
  
                  'items' =&gt; $items,
              ];
              }
              return [
                  'title' =&gt; $entry-&gt;title,
                  'menu' =&gt; [
                      'sections' =&gt; $matrixSections,
                  ]
              ];
          }
  ]
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-07-28T10:09:57.497Z
Answers.LastActivityDate: 2016-07-28T10:09:57.497Z
Answers.UserDisplayName: andrew-caulfield
Body: <p>I'm just wondering when it comes to Element API is there a way of returning a type of Category.</p>
  
  <p>I'm returning all the other information fine.</p>
  
  <p><a href="https://i.sstatic.net/tLTPx.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/tLTPx.png" alt="enter image description here"></a></p>
  
  <pre><code>'fees.json' =&gt; [
      'elementType' =&gt; 'Entry',
          'criteria' =&gt; [
              'title' =&gt; craft()-&gt;request-&gt;getQuery('title'),
              'section' =&gt; 'fees',
          ],
          'transformer' =&gt; function(EntryModel $entry) {
              foreach ($entry-&gt;fee as $block) {
                  $matrixSections[] = [
                      'first' =&gt; $block-&gt;firstInstallment,
                      'second' =&gt; $block-&gt;secondInstallment,
                      'full' =&gt; $block-&gt;full,
                      'grant' =&gt; $block-&gt;feeCategory
                  ];
              }
          return [
              'title' =&gt; $entry-&gt;title,
              'menu' =&gt; [
                  'sections' =&gt; $matrixSections
              ]
          ];
      }
  ]
  </code></pre>
  
  <p><a href="https://i.sstatic.net/KGhTx.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/KGhTx.png" alt="enter image description here"></a></p>
  
Tags: <entries><matrix><categories><plugin-elementapi>
Title: Element API return a type of Category
ThreadText: # Element API return a type of Category
  
  (Asked by: username_0 on 2016-07-27)
  
  I'm just wondering when it comes to Element API is there a way of returning a type of Category.
  
  I'm returning all the other information fine.
  
  [![enter image description here](https://i.sstatic.net/tLTPx.png)](https://i.sstatic.net/tLTPx.png)
  
  ```
  'fees.json' => [
      'elementType' => 'Entry',
          'criteria' => [
              'title' => craft()->request->getQuery('title'),
              'section' => 'fees',
          ],
          'transformer' => function(EntryModel $entry) {
              foreach ($entry->fee as $block) {
                  $matrixSections[] = [
                      'first' => $block->firstInstallment,
                      'second' => $block->secondInstallment,
                      'full' => $block->full,
                      'grant' => $block->feeCategory
                  ];
              }
          return [
              'title' => $entry->title,
              'menu' => [
                  'sections' => $matrixSections
              ]
          ];
      }
  ]
  
  ```
  
  [![enter image description here](https://i.sstatic.net/KGhTx.png)](https://i.sstatic.net/KGhTx.png)
  
  **Tags:** entries, matrix, categories, plugin-elementapi
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2016-07-27. Score: 2
  
  The Element API plugin returns JSON, so you'll need to be specific about what you want to return for your categories – i.e. you can't return a CategoryModel (or an array of CategoryModels) in full directly from a Categories field.
  
  The easiest option is to use [`array_map()`](http://php.net/manual/en/function.array-map.php) like this:
  
  ```
  'first' => $block->firstInstallment,
  'second' => $block->secondInstallment,
  'full' => $block->full,
  'grant' => array_map(function (CategoryModel $category) {
      return [
          'id' => $category->id,
          'title' => $category->title,
      ];
  }, $block->categoriesField->find()),
  
  ```
  
  Of course, if your individual category has a lot of fields, you can always create an external function in your `elementapi.php` file and use that for the `array_map` call, instead of inlining the function (the external function needs to live above the `return ['endpoints']...` part):
  
  ```
  function getCategoryData(CategoryModel $category)
  {
      return [
          'id' => $category->id,
          'title' => $category->title,
          ...
      ];
  }
  
  ...
  
  'grant' => array_map('getCategoryData', $block->categoriesField->find()),
  
  ```
  
  Also, there's the `$category->getAttributes()` method which can be handy if you want to return all stock attributes (i.e. `id`, `dateUpdated` etc – pretty much everything sans the title, for some reason):
  
  ```
  function getCategoryData(CategoryModel $category)
  {
      return array_merge($category->getAttributes(), [
          'title' => $category->title,
          'someCustomField' => $category->someCustomField,
      ];
  }
  
  ```
  
  > **Comments:**
  > **username_0**: Hey username_1, declaring the $matrixSections outside of the loop didn't make a difference. The only one of those options that returned anything was the ids option. Any other idea on this? Appreciate the reply none the less.  
  > **username_1**: You're positive that the `feeCategory` field for the block in question actually has enabled categories selected?  
  > **username_0**: I've added image to post to show that feeCategory has been added to the block.  
  > **username_1**: I had a case of stupid earlier – please see revised answer. I even tested it this time :)  
  > **username_0**: No worries. Appreciate the time anyway.  
  
  ---
  
  ### Answer by username_0 on 2016-07-28. Score: 0
  
  I'm sure the answer from username_1 also works but I thought the cleaner way would be the following.
  
  ```
  'fees.json' => [
          'elementType' => 'Entry',
              'criteria' => [
                  'title' => craft()->request->getQuery('title'),
                  'section' => 'fees',
              ],
              'transformer' => function(EntryModel $entry) {
              $matrixSections = [];
              foreach ($entry->fee as $block) {
              $items = [];
  
              foreach ($block->grantType as $itemEntry) {
                  $items[] = [
                  'title' => $itemEntry->title,
                  ];
              }
              $matrixSections[] = [
                  'first' => $block->firstInstallment,
                  'second' => $block->secondInstallment,
                  'full' => $block->full,
  
                  'items' => $items,
              ];
              }
              return [
                  'title' => $entry->title,
                  'menu' => [
                      'sections' => $matrixSections,
                  ]
              ];
          }
  ]
  
  ```
  
  
  ---
  
  