Answers.Body: <p>This should work</p>
  
  <pre class="lang-php prettyprint-override"><code>'api/globals.json' =&gt; function() {
      $langHandle = Craft::$app-&gt;request-&gt;getQueryParam('lang', 'en');
      return [
          'elementType' =&gt; GlobalSet::class,
          'criteria' =&gt; ['site' =&gt; $langHandle],
          'transformer' =&gt; function(GlobalSet $globalSet) {
              // start with -1 so the first one will be 0 if you start with a listHeader
              // this is your current index
              $index = -1;
              // the data you like to return
              $data = [];
              foreach ($globalSet-&gt;globals as $block) {
                  switch ($block-&gt;type-&gt;handle) {
                      case 'listHeader':
                          // new header, increase the index, store the title and an empty items array
                          $index++;
                          $data[$index]['title'] = $block-&gt;listTitle;
                          $data[$index]['items'] = [];
                          break;
                      case 'listItem':
                          // include the listItem to the current index as item
                          $data[$index]['items'][] = [
                              'link' =&gt; $block-&gt;listText,
                              'url' =&gt; $block-&gt;listUrl,
                          ];
                          break;
                  }
              }
              return [
                  'footerLinks' =&gt; $data,
              ];
          }
      ];
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-04-09T22:14:46.937Z
Answers.Text: Yes that does work. Thank you very much. Very simple solution!
Answers.UserDisplayName: user924248
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-04-09T14:00:57.830Z
Answers.LastActivityDate: 2019-04-09T14:00:57.830Z
Answers.UserDisplayName: Robin Schambach
Body: <p>I want to output all globals with one endpoint. This is trivial for the most part but I need to have some objects in a specific shape. </p>
  
  <p>I'm using a matrix block to handle the content. The main issue I have is I need the json for the <code>footerLinks</code> array to be:</p>
  
  <pre><code>{
      "data": [
          {
              "footerLinks": [
                  {
                      "title": "Legal information",
                      "items":[
                          {
                              "link": "Cookies and privacy",
                              "url": "/cookies-privacy"
                          }
                      ]
                  },
                  {
                      "title": "About Company",
                      "items":[
                          {
                              "link": "What we do",
                              "url": "https://someurl.com"
                          },
                          {
                              "link": "Contact us",
                              "url": "https://someurl.com"
                          },
                          {
                              "link": "Accessibility",
                              "url": "https://someurl.com"
                          }
                      ]
                  },
                  {
                      "title": "Statistics",
                      "items": [
                          {
                              "link": "Authority",
                              "url": "https://someurl.com"
                          },
  
                          {
                              "link": "Releases",
                              "url": "https://someurl.com"
                          },
                          {
                              "link": "News",
                              "url": "https://someurl.com"
                          }
                      ]
                  }
              ]
          }
      ]
  }
  </code></pre>
  
  <p>Here is the code I have so far:</p>
  
  <pre><code>        'api/globals.json' =&gt; function() {
              $langHandle = Craft::$app-&gt;request-&gt;getQueryParam('lang', 'en');
              return [
                  'elementType' =&gt; GlobalSet::class,
                  'criteria' =&gt; ['site' =&gt; $langHandle],
                  'transformer' =&gt; function(GlobalSet $globalSet) {
                      $footerBlocks = [];
                      foreach ($globalSet-&gt;globals as $block) {
                          switch ($block-&gt;type-&gt;handle) {
                              case 'listItem':
                                  $footerBlocks[] = [ 
                                      array(
                                          'link' =&gt; $block-&gt;listText,
                                          'url' =&gt; $block-&gt;listUrl,
                                      )
                                  ];
                              break;
                              case 'listHeader':
                                  $footerBlocks[] = [
                                      'title' =&gt; $block-&gt;listTitle,
                                  ];
                              break;
                          }   
                      }
                      return [
                          'footerLinks' =&gt; $footerBlocks,
                      ];
                  }
              ];
          }
  </code></pre>
  
  <p>And this is the current output:</p>
  
  <pre><code>{
    "data": [
      {
        "footerLinks": [
          {
            "title": "Legal information"
          },
          [
            {
              "link": "Cookies and privacy",
              "url": "/cookies-privacy"
            }
          ],
          {
            "title": "About"
          },
          [
            {
              "link": "What we do",
              "url": "https://someurl.com"
            }
          ],
          [
            {
              "link": "Contact us",
              "url": "/contact-us"
            }
          ],
          [
            {
              "link": "Accessibility",
              "url": "https://someurl.com"
            }
          ],
          {
            "title": "Statistics"
          },
          [
            {
              "link": "Authority",
              "url": "https://someurl.com"
            }
          ],
          [
            {
              "link": "Release calendar",
              "url": "https://someurl.com"
            }
          ],
          [
            {
              "link": "News",
              "url": "https://someurl.com"
            }
          ]
        ]
      }
    ],
  </code></pre>
  
  <p>And this is a screenshot of the matrix block:</p>
  
  <p><img src="https://user-images.githubusercontent.com/1015442/55803971-54ded600-5ad3-11e9-85ed-aee182225d9e.png" alt="image"></p>
  
  <p>Essentially I need to create a new object for each <code>listHeader</code> and <code>listItem</code> set and also wrap each set of <code>listItem</code> objects in an array called <code>items</code>. Currently it doesn't group anything together and outputs each individual <code>listItem</code> in an array.</p>
  
  <p>Any help would be great.</p>
  
Tags: <plugin-elementapi><json>
Title: Element api JSON output formatting
ThreadText: # Element api JSON output formatting
  
  (Asked by: username_0 on 2019-04-09)
  
  I want to output all globals with one endpoint. This is trivial for the most part but I need to have some objects in a specific shape.
  
  I'm using a matrix block to handle the content. The main issue I have is I need the json for the `footerLinks` array to be:
  
  ```
  {
      "data": [
          {
              "footerLinks": [
                  {
                      "title": "Legal information",
                      "items":[
                          {
                              "link": "Cookies and privacy",
                              "url": "/cookies-privacy"
                          }
                      ]
                  },
                  {
                      "title": "About Company",
                      "items":[
                          {
                              "link": "What we do",
                              "url": "https://someurl.com"
                          },
                          {
                              "link": "Contact us",
                              "url": "https://someurl.com"
                          },
                          {
                              "link": "Accessibility",
                              "url": "https://someurl.com"
                          }
                      ]
                  },
                  {
                      "title": "Statistics",
                      "items": [
                          {
                              "link": "Authority",
                              "url": "https://someurl.com"
                          },
  
                          {
                              "link": "Releases",
                              "url": "https://someurl.com"
                          },
                          {
                              "link": "News",
                              "url": "https://someurl.com"
                          }
                      ]
                  }
              ]
          }
      ]
  }
  
  ```
  
  Here is the code I have so far:
  
  ```
          'api/globals.json' => function() {
              $langHandle = Craft::$app->request->getQueryParam('lang', 'en');
              return [
                  'elementType' => GlobalSet::class,
                  'criteria' => ['site' => $langHandle],
                  'transformer' => function(GlobalSet $globalSet) {
                      $footerBlocks = [];
                      foreach ($globalSet->globals as $block) {
                          switch ($block->type->handle) {
                              case 'listItem':
                                  $footerBlocks[] = [ 
                                      array(
                                          'link' => $block->listText,
                                          'url' => $block->listUrl,
                                      )
                                  ];
                              break;
                              case 'listHeader':
                                  $footerBlocks[] = [
                                      'title' => $block->listTitle,
                                  ];
                              break;
                          }   
                      }
                      return [
                          'footerLinks' => $footerBlocks,
                      ];
                  }
              ];
          }
  
  ```
  
  And this is the current output:
  
  ```
  {
    "data": [
      {
        "footerLinks": [
          {
            "title": "Legal information"
          },
          [
            {
              "link": "Cookies and privacy",
              "url": "/cookies-privacy"
            }
          ],
          {
            "title": "About"
          },
          [
            {
              "link": "What we do",
              "url": "https://someurl.com"
            }
          ],
          [
            {
              "link": "Contact us",
              "url": "/contact-us"
            }
          ],
          [
            {
              "link": "Accessibility",
              "url": "https://someurl.com"
            }
          ],
          {
            "title": "Statistics"
          },
          [
            {
              "link": "Authority",
              "url": "https://someurl.com"
            }
          ],
          [
            {
              "link": "Release calendar",
              "url": "https://someurl.com"
            }
          ],
          [
            {
              "link": "News",
              "url": "https://someurl.com"
            }
          ]
        ]
      }
    ],
  
  ```
  
  And this is a screenshot of the matrix block:
  
  ![image](https://user-images.githubusercontent.com/1015442/55803971-54ded600-5ad3-11e9-85ed-aee182225d9e.png)
  
  Essentially I need to create a new object for each `listHeader` and `listItem` set and also wrap each set of `listItem` objects in an array called `items`. Currently it doesn't group anything together and outputs each individual `listItem` in an array.
  
  Any help would be great.
  
  **Tags:** plugin-elementapi, json
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-04-09. Score: 1 | Accepted answer
  
  This should work
  
  ```php
  'api/globals.json' => function() {
      $langHandle = Craft::$app->request->getQueryParam('lang', 'en');
      return [
          'elementType' => GlobalSet::class,
          'criteria' => ['site' => $langHandle],
          'transformer' => function(GlobalSet $globalSet) {
              // start with -1 so the first one will be 0 if you start with a listHeader
              // this is your current index
              $index = -1;
              // the data you like to return
              $data = [];
              foreach ($globalSet->globals as $block) {
                  switch ($block->type->handle) {
                      case 'listHeader':
                          // new header, increase the index, store the title and an empty items array
                          $index++;
                          $data[$index]['title'] = $block->listTitle;
                          $data[$index]['items'] = [];
                          break;
                      case 'listItem':
                          // include the listItem to the current index as item
                          $data[$index]['items'][] = [
                              'link' => $block->listText,
                              'url' => $block->listUrl,
                          ];
                          break;
                  }
              }
              return [
                  'footerLinks' => $data,
              ];
          }
      ];
  }
  
  ```
  
  > **Comments:**
  > **username_0**: Yes that does work. Thank you very much. Very simple solution!  
  
  ---
  
  