Skip to content

Instantly share code, notes, and snippets.

@Anubarak
Created September 5, 2018 11:10
Show Gist options
  • Save Anubarak/5e080f527f784d05cfc32a41c900e3d9 to your computer and use it in GitHub Desktop.
Save Anubarak/5e080f527f784d05cfc32a41c900e3d9 to your computer and use it in GitHub Desktop.
$sectionHandle = 'character';
$entryTypeHandle = 'character';
$newEntryTypeHandle = 'newType3';
$newEntryTypeName = 'A new Type3';
/** @var Section $section */
$section = Craft::$app->getSections()->getSectionByHandle($sectionHandle);
$entryTypes = $section->getEntryTypes();
$entryType = ArrayHelper::filterByValue($entryTypes, 'handle', $entryTypeHandle);
if(\count($entryType) === 1){
/** @var EntryType $entryType */
$entryType = $entryType[0];
$newEntryType = new EntryType([
'sectionId' => $entryType->sectionId,
'handle' => $newEntryTypeHandle,
'name' => $newEntryTypeName
]);
$oldLayout = $entryType->getFieldLayout();
$fieldLayout = new FieldLayout([
'tabs' => $oldLayout->getTabs(),
'fields' => $oldLayout->getFields(),
'type' => $oldLayout->type
]);
if(!Craft::$app->getFields()->saveLayout($fieldLayout)){
die('Couldn`t save layout');
}
echo 'new layout ID ' . $fieldLayout->id . '<br>';
$newEntryType->setFieldLayout($fieldLayout);
$success = Craft::$app->getSections()->saveEntryType($newEntryType);
echo "<pre>";
var_dump($success);
var_dump($newEntryType->getErrors());
echo "</pre>";
die();
}
die('no entry type found');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment