<p>Something like this should work:</p>

<pre><code>// Generate name and handle.
$globalSetName = "My Global Set";
$globalSetHandle = lcfirst(str_replace(" ","", ucwords($globalSetName)));

// Create a global set
$globalSet= new GlobalSetModel();
$globalSet-&gt;name = $globalSetName;
$globalSet-&gt;handle = $globalSetHandle;

// get all the fields from  fieldgroup
$fields = $fieldGroup-&gt;getFields();

// Get ids of the fields.
$fieldIds = array();
foreach ($fields as $field)
{
    $fieldIds[] = $field-&gt;id;
}

// Assemble the field layout
$globalSetLayout = craft()-&gt;fields-&gt;assembleLayout(
    array(
        'Content' =&gt; $fieldIds,
    )
);

// Set the field layout type.
$globalSetLayout-&gt;type = ElementType::GlobalSet;

// Set field layout on global set.
$globalSet-&gt;setFieldLayout($globalSetLayout);

// Save global set.
craft()-&gt;globals-&gt;saveSet($globalSet);
</code></pre>

CC BY-SA 3.0
2017-08-16T08:11:39.080Z
Works perfect thank you Brad, just needed to change `foreach ($fields $field)` to `foreach ($fields as $field)`
joep
CC BY-SA 3.0
2017-08-16T19:37:48.700Z
Oops... fixed!!
Brad Bell
CC BY-SA 3.0
2017-08-15T00:37:44.917Z
2017-08-16T19:37:37.083Z
2017-08-16T19:37:37.083Z
Brad Bell
<p>Can anyone suggest on how to programatically save fields to a GlobalSet?
I’ve been following this: <a href="https://craftcms.stackexchange.com/questions/6587/programatically-adding-fields-to-a-layout/18052#18052">Programatically Adding Fields to a Layout</a></p>

<p>But, doesn’t seem to work with a global?</p>

<p>I am doing the following :</p>

<ul>
<li>Create a new field group - works </li>
<li>Assign fields to field group - works</li>
<li>Create global set - works </li>
<li>Create field layout - works </li>
<li>Create field layout tab - works </li>
<li>Set fields to layout tab - nope</li>
</ul>

<p>.</p>

<pre><code>// get all the fields from company info fieldgroup
$Fields = $FieldGroup-&gt;getFields();
// Get field layout
$GlobalSetLayout = $GlobalSet-&gt;getFieldLayout();


// Create tab model
$GlobalSetLayoutTab = new FieldLayoutTabModel();
$layout = craft()-&gt;fields-&gt;getLayoutByType(ElementType::GlobalSet);
$GlobalSetLayoutTab-&gt;setLayout($layout);
$GlobalSetLayoutTab-&gt;setFields($Fields);

// Set the tab's fields
$GlobalSetLayoutTab-&gt;setFields($fields);

// setTabs() expects an array
$layoutTabs[] = $GlobalSetLayoutTab;

// Now, set the tabs back in the layout.
$layout-&gt;setTabs($layoutTabs);
</code></pre>

<p>Not sure if part of the issue is because Globals don’t exactly have a “tab”, but looking at others in the database it does seem to have one with the name “Content”</p>

<plugin-development><globals>
Saving fields to a GlobalSet
# Saving fields to a GlobalSet

(Asked by: username_0 on 2017-08-07)

Can anyone suggest on how to programatically save fields to a GlobalSet?
I’ve been following this: Programatically Adding Fields to a Layout
But, doesn’t seem to work with a global?
I am doing the following :

Create a new field group - works 
Assign fields to field group - works
Create global set - works 
Create field layout - works 
Create field layout tab - works 
Set fields to layout tab - nope

.
// get all the fields from company info fieldgroup
$Fields = $FieldGroup->getFields();
// Get field layout
$GlobalSetLayout = $GlobalSet->getFieldLayout();


// Create tab model
$GlobalSetLayoutTab = new FieldLayoutTabModel();
$layout = craft()->fields->getLayoutByType(ElementType::GlobalSet);
$GlobalSetLayoutTab->setLayout($layout);
$GlobalSetLayoutTab->setFields($Fields);

// Set the tab's fields
$GlobalSetLayoutTab->setFields($fields);

// setTabs() expects an array
$layoutTabs[] = $GlobalSetLayoutTab;

// Now, set the tabs back in the layout.
$layout->setTabs($layoutTabs);

Not sure if part of the issue is because Globals don’t exactly have a “tab”, but looking at others in the database it does seem to have one with the name “Content”

**Tags:** plugin-development, globals


---

## 1 answers

### Answer by username_1 on 2017-08-15. Score: 2 | Accepted answer

Something like this should work:
// Generate name and handle.
$globalSetName = "My Global Set";
$globalSetHandle = lcfirst(str_replace(" ","", ucwords($globalSetName)));

// Create a global set
$globalSet= new GlobalSetModel();
$globalSet->name = $globalSetName;
$globalSet->handle = $globalSetHandle;

// get all the fields from  fieldgroup
$fields = $fieldGroup->getFields();

// Get ids of the fields.
$fieldIds = array();
foreach ($fields as $field)
{
    $fieldIds[] = $field->id;
}

// Assemble the field layout
$globalSetLayout = craft()->fields->assembleLayout(
    array(
        'Content' => $fieldIds,
    )
);

// Set the field layout type.
$globalSetLayout->type = ElementType::GlobalSet;

// Set field layout on global set.
$globalSet->setFieldLayout($globalSetLayout);

// Save global set.
craft()->globals->saveSet($globalSet);

> **Comments:**
> **username_0**: Works perfect thank you Brad, just needed to change `foreach ($fields $field)` to `foreach ($fields as $field)`  
> **username_1**: Oops... fixed!!  

---

