<p>So it looks like you must get the correct block type in order for this to create new rows in the SuperTable.</p>

<p>First get the block type using the <code>SuperTable</code> service:</p>

<pre><code>       //you'll need to reference this in your code
       use verbb\supertable\SuperTable;

       $field = Craft::$app-&gt;getFields()-&gt;getFieldByHandle('siteCustom');
       $blockType = SuperTable::$plugin-&gt;service-&gt;getBlockTypesByFieldId($field-&gt;id)[0];
</code></pre>

<p>Then use that same blockType id to set the type field:</p>

<pre><code>       $globalSet = Craft::$app-&gt;globals-&gt;getSetByHandle("myGlobals");  

       $globalSet-&gt;setFieldValues([
              'siteName' =&gt; "The Website",
              'siteCustom' =&gt; [
                   'new1' =&gt; [
                          'type' =&gt; $blockType-&gt;id,
                          'fields' =&gt; [
                              'firstField' =&gt; "some value",
                              'secondField' =&gt; "some other value"
                           ]
                   ]
               ]
       ]);

       Craft::$app-&gt;globals-&gt;saveSet($globalSet);
</code></pre>

<p>If you need to add more than one value to <code>siteCustom</code> you can do so with the same syntax, just make sure you increment the number after <code>new</code> each time (so <code>new2</code>, <code>new3</code>, and so on).</p>

CC BY-SA 4.0
2023-05-16T14:04:02.773Z
That answer aged pretty well.  Still works for Craft CMS 4, though I had to replace `SuperTable::$plugin->service` with `SuperTable::$plugin->getService()`. And in my special case I had to pass in a context to `Craft::$app->getFields()->getFieldByHandle('fieldHandle', false)` (I just passed `false`) because I am dealing with a SuperTable field within a MatrixBlock instead of a standalone SuperTable field. Works like a charm! Thanks.
Arvid
CC BY-SA 4.0
2018-06-12T09:11:20.367Z
2018-10-03T13:17:27.297Z
2018-10-03T13:17:27.297Z
Piaras Hoban
<p>I'm trying to setup some Global fields in a migration.</p>

<p>I'm using the SuperTable Field from verbb which is similar to a matrix field.</p>

<p>I'd like to add several rows during migration but can't seem to get the combination right in order to set the field values.  Can someone help me out, what I've got so far is the following:</p>

<pre><code>   $globalSet = Craft::$app-&gt;globals-&gt;getSetByHandle("myGlobals");  

   $globalSet-&gt;setFieldValues([
            'siteName' =&gt; "The Website",
            'siteCustom' =&gt; [
                'new1' =&gt; [
                    'type' =&gt; 'superTableBlockType',
                    'fields' =&gt; [
                        'firstField' =&gt; "some value",
                        'secondField' =&gt; "some other value"
                    ]
                ]
            ]
        ]
   );

   Craft::$app-&gt;globals-&gt;saveSet($globalSet);
</code></pre>

<matrix><php><plugin-supertable>
Set SuperTableFields programatically
# Set SuperTableFields programatically

(Asked by: username_0 on 2018-06-12)

I'm trying to setup some Global fields in a migration.
I'm using the SuperTable Field from verbb which is similar to a matrix field.
I'd like to add several rows during migration but can't seem to get the combination right in order to set the field values.  Can someone help me out, what I've got so far is the following:
   $globalSet = Craft::$app->globals->getSetByHandle("myGlobals");  

   $globalSet->setFieldValues([
            'siteName' => "The Website",
            'siteCustom' => [
                'new1' => [
                    'type' => 'superTableBlockType',
                    'fields' => [
                        'firstField' => "some value",
                        'secondField' => "some other value"
                    ]
                ]
            ]
        ]
   );

   Craft::$app->globals->saveSet($globalSet);

**Tags:** matrix, php, plugin-supertable


---

## 1 answers

### Answer by username_0 on 2018-06-12. Score: 2 | Accepted answer

So it looks like you must get the correct block type in order for this to create new rows in the SuperTable.
First get the block type using the SuperTable service:
       //you'll need to reference this in your code
       use verbb\supertable\SuperTable;

       $field = Craft::$app->getFields()->getFieldByHandle('siteCustom');
       $blockType = SuperTable::$plugin->service->getBlockTypesByFieldId($field->id)[0];

Then use that same blockType id to set the type field:
       $globalSet = Craft::$app->globals->getSetByHandle("myGlobals");  

       $globalSet->setFieldValues([
              'siteName' => "The Website",
              'siteCustom' => [
                   'new1' => [
                          'type' => $blockType->id,
                          'fields' => [
                              'firstField' => "some value",
                              'secondField' => "some other value"
                           ]
                   ]
               ]
       ]);

       Craft::$app->globals->saveSet($globalSet);

If you need to add more than one value to siteCustom you can do so with the same syntax, just make sure you increment the number after new each time (so new2, new3, and so on).

> **Comments:**
> **username_1**: That answer aged pretty well.  Still works for Craft CMS 4, though I had to replace `SuperTable::$plugin->service` with `SuperTable::$plugin->getService()`. And in my special case I had to pass in a context to `Craft::$app->getFields()->getFieldByHandle('fieldHandle', false)` (I just passed `false`) because I am dealing with a SuperTable field within a MatrixBlock instead of a standalone SuperTable field. Works like a charm! Thanks.  

---

