Saving fields to a GlobalSet 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-&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);


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”
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-&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);