<p>You’re off to a great start here, in the way that you are first assembling the $menuBlocks array, and then finally returning the main array with the <code>title</code> and <code>menu</code> keys. For the section items, you just need to take the exact same approach:</p>

<pre class="lang-php prettyprint-override"><code>'transformer' =&gt; function(EntryModel $entry) {
  // Note: you should also make sure you define the $menuBlocks
  // array, to avoid a PHP error in case there are 0 blocks.
  $menuBlocks = [];

  foreach ($entry-&gt;menu as $block) {
    // Now start by defining an $items array:
    $items = [];

    // And loop through your sectionItems field just like
    // you are doing with the menu field.
    // (Note - you can’t name the individual item entries "$entry",
    // because that would override the other $entry variable.)
    foreach ($block-&gt;sectionItems as $itemEntry) {
      $items[] = [
        'title' =&gt; $itemEntry-&gt;title,
        // ...
      ];
    }

    $menuBlocks[] = [
      'id' =&gt; $block-&gt;id,
      'menu_id' =&gt; $block-&gt;fieldId,
      'name' =&gt; $block-&gt;sectionName,
      'description' =&gt; $block-&gt;sectionDescription,

      // Now insert that whole $items array here:
      'items' =&gt; $items,
    ];
  }

  return [
    'title' =&gt; $entry-&gt;title,
    'menu' =&gt; [
      'sections' =&gt; $menuBlocks
    ]
  ];
},
</code></pre>

CC BY-SA 3.0
2016-07-22T14:25:55.257Z
2016-07-22T14:25:55.257Z
Brandon Kelly
<p>I'm using a Matrix field for menu section blocks that each have a name, section and menu items. The menu items for that menu section are assigned using an Entries fieldtype that displays entries from food and beverage entries. Here's a screenshot of the field setup in case that helps explain it better:</p>

<p><a href="https://i.sstatic.net/bZSfo.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/bZSfo.png" alt="enter image description here"></a></p>

<p>What I'm trying to do is generate a menu endpoint that has the individual sections and within each section display the item data. The json response I'm looking for should be similar to this structure:</p>

<pre class="lang-javascript prettyprint-override"><code>
        "menu": {
          "id": 1,
          "sections": [
            {
              "id": 1,
              "name": "Heavy",
              "description": "All our heavy draft beers.",
              "items": [
                {
                  "id": 2,
                  "name": "Samuel Adams Utopias",
                },
                {
                  "id": 3,
                  "name": "Samuel Adams Utopias",
                },
              ]
            },
            {
              "id": 2,
              "name": "Light",
              "description": "All our light draft beers.",
              "items": [
                {
                  "id": 2,
                  "name": "Samuel Adams Utopias",
                },
                {
                  "id": 3,
                  "name": "Samuel Adams Utopias",
                },
              ]
            },
          ]
        }
</code></pre>

<p>I can accomplish the section structure just fine, but I can't get the data from the sectionItems field which is an Entries type. This is what I have minus the items call.</p>

<pre class="lang-php prettyprint-override"><code>
        'api/v1/locations/&lt;entryId:\d+&gt;/menu.json' =&gt; function($entryId) {
          return [
            'elementType' =&gt; 'Entry',
            'criteria' =&gt; [
              'id' =&gt; $entryId,
              'type' =&gt; 'location'
            ],
            'first' =&gt; true,
            'transformer' =&gt; function(EntryModel $entry) {
              foreach ($entry-&gt;menu as $block) {
                $menuBlocks[] = [
                  'id' =&gt; $block-&gt;id,
                  'menu_id' =&gt; $block-&gt;fieldId,
                  'name' =&gt; $block-&gt;sectionName,
                  'description' =&gt; $block-&gt;sectionDescription,
                  'items' =&gt; $block-&gt;sectionItems,
                ];
              }

              return [
                'title' =&gt; $entry-&gt;title,
                'menu' =&gt; [
                  'sections' =&gt; $menuBlocks
                ]
              ];
            },
          ];
        },
</code></pre>

<p>Any help would be greatly appreciated!</p>

<plugin-elementapi>
Retrieve entry data from an Entries field in a Matrix Block
# Retrieve entry data from an Entries field in a Matrix Block

(Asked by: username_0 on 2016-07-21)

I'm using a Matrix field for menu section blocks that each have a name, section and menu items. The menu items for that menu section are assigned using an Entries fieldtype that displays entries from food and beverage entries. Here's a screenshot of the field setup in case that helps explain it better:

What I'm trying to do is generate a menu endpoint that has the individual sections and within each section display the item data. The json response I'm looking for should be similar to this structure:

        "menu": {
          "id": 1,
          "sections": [
            {
              "id": 1,
              "name": "Heavy",
              "description": "All our heavy draft beers.",
              "items": [
                {
                  "id": 2,
                  "name": "Samuel Adams Utopias",
                },
                {
                  "id": 3,
                  "name": "Samuel Adams Utopias",
                },
              ]
            },
            {
              "id": 2,
              "name": "Light",
              "description": "All our light draft beers.",
              "items": [
                {
                  "id": 2,
                  "name": "Samuel Adams Utopias",
                },
                {
                  "id": 3,
                  "name": "Samuel Adams Utopias",
                },
              ]
            },
          ]
        }

I can accomplish the section structure just fine, but I can't get the data from the sectionItems field which is an Entries type. This is what I have minus the items call.

        'api/v1/locations//menu.json' => function($entryId) {
          return [
            'elementType' => 'Entry',
            'criteria' => [
              'id' => $entryId,
              'type' => 'location'
            ],
            'first' => true,
            'transformer' => function(EntryModel $entry) {
              foreach ($entry->menu as $block) {
                $menuBlocks[] = [
                  'id' => $block->id,
                  'menu_id' => $block->fieldId,
                  'name' => $block->sectionName,
                  'description' => $block->sectionDescription,
                  'items' => $block->sectionItems,
                ];
              }

              return [
                'title' => $entry->title,
                'menu' => [
                  'sections' => $menuBlocks
                ]
              ];
            },
          ];
        },

Any help would be greatly appreciated!

**Tags:** plugin-elementapi

---

## 1 answers

### Answer by username_1 on 2016-07-22. Score: 3 | Accepted answer

You’re off to a great start here, in the way that you are first assembling the $menuBlocks array, and then finally returning the main array with the title and menu keys. For the section items, you just need to take the exact same approach:
'transformer' => function(EntryModel $entry) {
  // Note: you should also make sure you define the $menuBlocks
  // array, to avoid a PHP error in case there are 0 blocks.
  $menuBlocks = [];

  foreach ($entry->menu as $block) {
    // Now start by defining an $items array:
    $items = [];

    // And loop through your sectionItems field just like
    // you are doing with the menu field.
    // (Note - you can’t name the individual item entries "$entry",
    // because that would override the other $entry variable.)
    foreach ($block->sectionItems as $itemEntry) {
      $items[] = [
        'title' => $itemEntry->title,
        // ...
      ];
    }

    $menuBlocks[] = [
      'id' => $block->id,
      'menu_id' => $block->fieldId,
      'name' => $block->sectionName,
      'description' => $block->sectionDescription,

      // Now insert that whole $items array here:
      'items' => $items,
    ];
  }

  return [
    'title' => $entry->title,
    'menu' => [
      'sections' => $menuBlocks
    ]
  ];
},


---

