<p>Not sure if this is the answer without playing, but try saving the plain text field before adding it to the super table:</p>

<pre><code>    // Create the super table field

    $superTable = new SuperTableField([
        'name' =&gt; 'supertablefield',
        'handle' =&gt; 'supertablefield',
        'groupId' =&gt; 1
    ]);

    Craft::$app-&gt;getFields()-&gt;saveField($superTable);


    // Create plain text field

    $field = new \craft\fields\PlainText([
        "name" =&gt; 'textfield',
        "handle" =&gt; 'textfield',
        "translationMethod" =&gt; "site",
        "multiline" =&gt; 0,
    ]);


    // Save the plain text field

    Craft::$app-&gt;getFields()-&gt;saveField($field);


    // Retrieve the plain text field 

    $plainTextField = Craft::$app-&gt;fields-&gt;getFieldByHandle("textfield")


    // Create and set the block

    $blockModel = new SuperTableBlockTypeModel();
    $blockModel-&gt;fieldId = $superTable-&gt;id;
    $blockModel-&gt;uid = StringHelper::UUID();
    $blockModel-&gt;setFields([$plainTextField]);
    $superTable-&gt;setBlockTypes([$blockModel]);
    SuperTable::$plugin-&gt;service-&gt;saveBlockType($blockModel);
</code></pre>

CC BY-SA 4.0
2020-06-02T17:19:59.813Z
Not sure if you could do `$plainTextField = Craft::$app->getFields()->saveField($field)` to make it cleaner.
dmatthams
CC BY-SA 4.0
2020-06-02T17:42:23.823Z
thanks! it works. i don't know why but when i create matrix there is no need to save field before setting field in block type -although this is another topic-
user9975
CC BY-SA 4.0
2020-06-02T17:14:56.797Z
2020-06-03T08:42:36.513Z
2020-06-03T08:42:36.513Z
dmatthams
<p>i am trying to create a super table field programmatically.
this is the code i tried.</p>

<p>i see super table field in field list but i can see fields inside of super table.</p>

<p>how can i do this?</p>

<pre><code>    //create super table field itself
    $superTable = new SuperTableField([
        'name' =&gt; 'supertablefield',
        'handle' =&gt; 'supertablefield',
        'groupId' =&gt; 1
    ]);
    Craft::$app-&gt;getFields()-&gt;saveField($superTable);
    //field inside super table
    $field = new \craft\fields\PlainText([
        "name" =&gt; 'textfield',
        "handle" =&gt; 'textfield',
        "translationMethod" =&gt; "site",
        "multiline" =&gt; 0,
    ]);
   //create and set block type
    $blockModel = new SuperTableBlockTypeModel();
    $blockModel-&gt;fieldId = $superTable-&gt;id;
    $blockModel-&gt;uid = StringHelper::UUID();
    $blockModel-&gt;setFields([$field]);

    $superTable-&gt;setBlockTypes([$blockModel]);
    SuperTable::$plugin-&gt;service-&gt;saveBlockType($blockModel);
</code></pre>

<plugin-development><plugin-supertable>
how to create Super Table field programmatically
# how to create Super Table field programmatically

(Asked by: username_0 on 2020-05-31)

i am trying to create a super table field programmatically.
this is the code i tried.
i see super table field in field list but i can see fields inside of super table.
how can i do this?
    //create super table field itself
    $superTable = new SuperTableField([
        'name' => 'supertablefield',
        'handle' => 'supertablefield',
        'groupId' => 1
    ]);
    Craft::$app->getFields()->saveField($superTable);
    //field inside super table
    $field = new \craft\fields\PlainText([
        "name" => 'textfield',
        "handle" => 'textfield',
        "translationMethod" => "site",
        "multiline" => 0,
    ]);
   //create and set block type
    $blockModel = new SuperTableBlockTypeModel();
    $blockModel->fieldId = $superTable->id;
    $blockModel->uid = StringHelper::UUID();
    $blockModel->setFields([$field]);

    $superTable->setBlockTypes([$blockModel]);
    SuperTable::$plugin->service->saveBlockType($blockModel);

**Tags:** plugin-development, plugin-supertable


---

## 1 answers

### Answer by username_1 on 2020-06-02. Score: 2 | Accepted answer

Not sure if this is the answer without playing, but try saving the plain text field before adding it to the super table:
    // Create the super table field

    $superTable = new SuperTableField([
        'name' => 'supertablefield',
        'handle' => 'supertablefield',
        'groupId' => 1
    ]);

    Craft::$app->getFields()->saveField($superTable);


    // Create plain text field

    $field = new \craft\fields\PlainText([
        "name" => 'textfield',
        "handle" => 'textfield',
        "translationMethod" => "site",
        "multiline" => 0,
    ]);


    // Save the plain text field

    Craft::$app->getFields()->saveField($field);


    // Retrieve the plain text field 

    $plainTextField = Craft::$app->fields->getFieldByHandle("textfield")


    // Create and set the block

    $blockModel = new SuperTableBlockTypeModel();
    $blockModel->fieldId = $superTable->id;
    $blockModel->uid = StringHelper::UUID();
    $blockModel->setFields([$plainTextField]);
    $superTable->setBlockTypes([$blockModel]);
    SuperTable::$plugin->service->saveBlockType($blockModel);

> **Comments:**
> **username_1**: Not sure if you could do `$plainTextField = Craft::$app->getFields()->saveField($field)` to make it cleaner.  
> **username_0**: thanks! it works. i don't know why but when i create matrix there is no need to save field before setting field in block type -although this is another topic-  

---

