5

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:

enter image description here

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/<entryId:\d+>/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!

Cavell Blood
  • 693
  • 6
  • 21

1 Answers1

3

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
    ]
  ];
},
Brandon Kelly
  • 34,307
  • 2
  • 71
  • 137