For a current project, I am attempting to automate the creation of a section.
I am using a default_section as the base of all the others. This section will contain a couple of entries, and a couple of entry types.
My goal right now is to copy over the Entry Types in its totality. Right now, I'm struggling with the field layout that I have set for the Entry Type
Let's say that the first Entry Type has the following field layout:
And this is the code that I am using
private function copyEntryTypes($savedSection, $entryTypes)
{
foreach ($entryTypes as $entryType) {
$copiedEntryType = $entryType;
$newEntryType = new EntryTypeModel();
$copiedEntryType->id = null;
$copiedEntryType->sectionId = $savedSection->id;
$values = $copiedEntryType->attributes;
$newEntryType->setAttributes($values);
$currentLayout = $copiedEntryType->getFieldLayout();
$currentTabs = $currentLayout->getTabs();
$postedFieldLayout = array();
foreach ($currentTabs as $tab) {
$fields = $tab->getFields();
foreach ($fields as $field) {
echo '<pre>' . var_export($field->attributes) . '</pre>';
$postedFieldLayout[$tab->name][] = $field->fieldId;
}
}
$requiredFields = array();
$fieldLayout = craft()->fields->assembleLayout($postedFieldLayout, $requiredFields);
$fieldLayout->type = ElementType::Entry;
$newEntryType->setFieldLayout($fieldLayout);
//TODO: New Entry Types are succesfully being copied. Now all I need is to move over the fieldset
if (craft()->sections->saveEntryType($entryType))
{
var_dump('Successfully appended fields.');
}
else
{
var_dump('Could not append the fields.');
}
}
return true;
}
//note: this code is WIP, so it's not super clean, sorry.
The result is that the fields are being looped through correctly, I can var_dump them and find the correct ID's. The problem is that the resulting EntryType I have made does not contain the layout I am trying to copy over.