<p>Here is a solution I found. Define an empty array, then loop over it, providing whatever keys you want to return from the Super Table fields. Simply by looping over <code>$block-&gt;columnTextBlocks</code> (the handle of my Super Table field), Element API returns the information for the fields within the Super Table. </p>

<pre><code> case 'twoColumnText':

    $SuperTableRows = [];

    foreach ($block-&gt;columnTextBlocks as $row){
        $SuperTableRows[] = [
            'fieldHandle1' =&gt; $row-&gt;fieldHandle1,
            'fieldHandle2' =&gt; $row-&gt;fieldHandle2,
            'fieldHandle3' =&gt; $row-&gt;fieldHandle3
        ];
    }

    $contentBlocksArray[] = [
        'twoColumnText' =&gt; [
            'columnTextBlocks' =&gt; $SuperTableRows,
        ]
    ];

break;
</code></pre>

<p>If you need additional information about the Super Table fields, such as Placeholder text, field id's, etc. you can add a generic call to the Super Table field like so (using the same names as my previous code example):</p>

<pre><code>$contentBlocksArray[] = [
    'twoColumnText' =&gt; [
        'generic' =&gt; $block-&gt;columnTextBlocks,
        'columnTextBlocks' =&gt; $SuperTableRows,
    ]
];
</code></pre>

<p>Now in Element API, the "generic" object includes a customFields array for each field within the Super Table. This only becomes available when you loop over the Super Table field, otherwise it would be <code>null</code>.</p>

<p>Here are some pictures of what the data from Element API looks like.</p>

<p><a href="https://i.sstatic.net/9LsVT.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/9LsVT.png" alt="Return Super Table fields data from Element API"></a></p>

<p><a href="https://i.sstatic.net/x7nza.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/x7nza.png" alt="Complete field data from Super Table field item in Element API"></a></p>

<p>Here is a simplified version:</p>

<pre><code>'transformer' =&gt; function(Entry $entry) {
   $SuperTableRows = [];

   foreach ($entry-&gt;superTableFieldHandle as $row){
     $SuperTableRows[] = [
       'fieldHandle1' =&gt; $row-&gt;fieldHandle1,
       'fieldHandle2' =&gt; $row-&gt;fieldHandle2
     ];
   }


   return [
     'SuperTableRows' =&gt; $SuperTableRows
   ];
 },
</code></pre>

CC BY-SA 4.0
2018-05-05T19:00:15.433Z
2018-08-30T14:31:44.910Z
2018-08-30T14:31:44.910Z
andrewmenich
<p>I am building a feed using Element API. I am currently looping through Matrix blocks to get various data (based on the help from <a href="https://craftcms.stackexchange.com/questions/13118/how-do-you-use-the-element-api-plugin-to-output-matrix-blocks-for-an-entry?rq=1">this post</a>). One of the matrix blocks contains a Super Table field, with various fields inside of that. Here is the relevant section of my Element API config file.</p>

<pre><code>'transformer' =&gt; function(Entry $entry) {
  $contentBlocksArray = [];

     foreach ($entry-&gt;contentBlocks as $block){
       switch($block-&gt;type-&gt;handle) {

         case 'introText':
           $contentBlocksArray[] = [
             'introText' =&gt; [
               'body' =&gt; $block-&gt;body,
             ]
            ];
            break;

          case 'twoColumnText':
             $contentBlocksArray[] = [
               'twoColumnText' =&gt; [
                  'columnTextBlocks' =&gt; [
                      'icon' =&gt; $block-&gt;icon,
                      'text' =&gt; $block-&gt;text
                  ]
                ]
              ];
              break;
</code></pre>

<p>The first case in this switch works as expected. 'introText' is the Matrix box and 'body' is a plaintext field inside that block. The second case is where I'm having trouble. 'twoColumnText' is the Matrix block and 'columnTextBlocks' is a Super Table field with two plaintext fields. The api returns a response, but both 'icon' and 'text' = null. </p>

<p>I have also tried calling some of the functions as demonstrated in Super Tables docs <a href="https://verbb.io/craft-plugins/super-table/docs/guides/developer-examples" rel="nofollow noreferrer">here</a> but no luck. I am by no means a php / laravel / etc developer, so I might be missing something that would be obvious to such a developer.</p>

<p>I suspect it's something similar to retrieving images where I have to loop through the Super Table fields, but I don't know what that syntax would look like. </p>

CC BY-SA 4.0
2018-05-04T20:27:35.483Z
Is it a static field? If so have you tried `$block->columnTextBlocks->icon`? If it's not a static field I can create an answer with how to loop through the rows
Robin Schambach
CC BY-SA 4.0
2018-05-04T20:50:35.933Z
@RobinSchambach They weren't originally, but I changed them to static and it still didn't work.
andrewmenich
<plugin-elementapi><plugin-supertable>
Using Element API to output Super Table fields
# Using Element API to output Super Table fields

(Asked by: username_0 on 2018-05-04)

I am building a feed using Element API. I am currently looping through Matrix blocks to get various data (based on the help from this post). One of the matrix blocks contains a Super Table field, with various fields inside of that. Here is the relevant section of my Element API config file.
'transformer' => function(Entry $entry) {
  $contentBlocksArray = [];

     foreach ($entry->contentBlocks as $block){
       switch($block->type->handle) {

         case 'introText':
           $contentBlocksArray[] = [
             'introText' => [
               'body' => $block->body,
             ]
            ];
            break;

          case 'twoColumnText':
             $contentBlocksArray[] = [
               'twoColumnText' => [
                  'columnTextBlocks' => [
                      'icon' => $block->icon,
                      'text' => $block->text
                  ]
                ]
              ];
              break;

The first case in this switch works as expected. 'introText' is the Matrix box and 'body' is a plaintext field inside that block. The second case is where I'm having trouble. 'twoColumnText' is the Matrix block and 'columnTextBlocks' is a Super Table field with two plaintext fields. The api returns a response, but both 'icon' and 'text' = null. 
I have also tried calling some of the functions as demonstrated in Super Tables docs here but no luck. I am by no means a php / laravel / etc developer, so I might be missing something that would be obvious to such a developer.
I suspect it's something similar to retrieving images where I have to loop through the Super Table fields, but I don't know what that syntax would look like.

**Tags:** plugin-elementapi, plugin-supertable
> **Comments:**
> **username_1**: Is it a static field? If so have you tried `$block->columnTextBlocks->icon`? If it's not a static field I can create an answer with how to loop through the rows  
> **username_0**: @RobinSchambach They weren't originally, but I changed them to static and it still didn't work.  

---

## 1 answers

### Answer by username_0 on 2018-05-05. Score: 4 | Accepted answer

Here is a solution I found. Define an empty array, then loop over it, providing whatever keys you want to return from the Super Table fields. Simply by looping over $block->columnTextBlocks (the handle of my Super Table field), Element API returns the information for the fields within the Super Table. 
 case 'twoColumnText':

    $SuperTableRows = [];

    foreach ($block->columnTextBlocks as $row){
        $SuperTableRows[] = [
            'fieldHandle1' => $row->fieldHandle1,
            'fieldHandle2' => $row->fieldHandle2,
            'fieldHandle3' => $row->fieldHandle3
        ];
    }

    $contentBlocksArray[] = [
        'twoColumnText' => [
            'columnTextBlocks' => $SuperTableRows,
        ]
    ];

break;

If you need additional information about the Super Table fields, such as Placeholder text, field id's, etc. you can add a generic call to the Super Table field like so (using the same names as my previous code example):
$contentBlocksArray[] = [
    'twoColumnText' => [
        'generic' => $block->columnTextBlocks,
        'columnTextBlocks' => $SuperTableRows,
    ]
];

Now in Element API, the "generic" object includes a customFields array for each field within the Super Table. This only becomes available when you loop over the Super Table field, otherwise it would be null.
Here are some pictures of what the data from Element API looks like.


Here is a simplified version:
'transformer' => function(Entry $entry) {
   $SuperTableRows = [];

   foreach ($entry->superTableFieldHandle as $row){
     $SuperTableRows[] = [
       'fieldHandle1' => $row->fieldHandle1,
       'fieldHandle2' => $row->fieldHandle2
     ];
   }


   return [
     'SuperTableRows' => $SuperTableRows
   ];
 },


---

