Skip to main content
replaced http://craftcms.stackexchange.com/ with https://craftcms.stackexchange.com/
Source Link

I'm also using code that I modified from: How can I programmatically add a tab to entry type?How can I programmatically add a tab to entry type? so this guy deserves the credit.

I'm also using code that I modified from: How can I programmatically add a tab to entry type? so this guy deserves the credit.

I'm also using code that I modified from: How can I programmatically add a tab to entry type? so this guy deserves the credit.

Source Link

After hours I managed to fix it. I'm assuming I made an error where I was trying to copy the Fields in the Entry Type I literally just created. So it was copying over 0 fields.

I built the functions from scratch again and finally found the working code. For future reference or to help others, here is the snippet in question.

private function CopyEntryTypes($savedSection, $baseSection)
{
    $baseSectionEntryTypes = $baseSection->getEntryTypes();

    foreach ($baseSectionEntryTypes as $baseSectionEntryType) {
        $newEntryType = $this->createNewEntryType($baseSectionEntryType, $savedSection->id);
        //oke so the New Entry Type is saved, we should get a return statement from the function
        //now loop through the tabs/fields of the baseSection Entry Type.


        $currentLayout = $baseSectionEntryType->getFieldLayout();
        $currentTabs = $currentLayout->getTabs();

        // This will the array passed to assembleLayout
        $postedFieldLayout = array();

        // Loop through each tab and build our array
        foreach ($currentTabs as $tab) {
            $fields = $tab->getFields();
            foreach ($fields as $field) {
                $postedFieldLayout[$tab->name][] = $field->fieldId;
            }
        }

        //so now we have a collection of entry types, each of them have a tab-name which we can use to identify
        //the goal is to get 'all' the tabs into the new entry type.
        $requiredFields = array();

        // Assemble the layout
        $fieldLayout = craft()->fields->assembleLayout($postedFieldLayout, $requiredFields);
        $fieldLayout->type = ElementType::Entry;
        $newEntryType->setFieldLayout($fieldLayout);

        // Save it
        if (craft()->sections->saveEntryType($newEntryType))
        {
            Craft::log('Successfully appended fields.');
        }
        else
        {
            Craft::log('Could not append the fields.', LogLevel::Warning);
        }
    }
}

private function createNewEntryType($baseSectionEntryType, $savedSectionId)
{
    $newEntryType = new EntryTypeModel();
    $baseSectionEntryType->id = null;
    $baseSectionEntryType->sectionId = $savedSectionId;
    $values = $baseSectionEntryType->attributes;
    $newEntryType->setAttributes($values);

    if (craft()->sections->saveEntryType($newEntryType))
    {
        var_dump('Successfully saved Entry Type.');
    }
    else
    {
        var_dump('Could not save the Entry Type.');
    }

    return $newEntryType;
}

I'm also using code that I modified from: How can I programmatically add a tab to entry type? so this guy deserves the credit.