2

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: image of the layout i'm trying to replicate

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.

That's looking like this: enter image description here

2 Answers 2

1

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.

1

I've had this issue previously before when working on a larger website, when you want to copy an entry type and all its field layouts, or even copy a section all its entry types and all their field layouts.

I've written a plugin that tackles this, check it out here:

Craft Cloner

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.