Content Migrations in Craft

Creating Field Layouts with a Content Migration

We'll cover how to create a field layout in Craft CMS with a content migration.

The next thing we want to cre­ate for our con­tact form sec­tion is to cre­ate a lay­out field so we can cre­ate a pub­lish form in the con­tact sub­mis­sions sec­tion of the Craft project.

We’ll first cre­ate a migra­tion for this new field layout:

craft migrate/create add_field_layout

The first thing we need to do is get the fields that we already cre­at­ed because we are going to add them to the field layout. 

After that, we’ll fetch the default entry type that was cre­at­ed when we cre­at­ed the new Con­tact Form sec­tion in a pre­vi­ous migration.

From the entry type, we can retrieve the field lay­out using the getFieldLayout() method.

As part of the new lay­out, we’ll cre­ate a new tab and give it the name Con­tent Sub­mis­sions” using a new instance of FieldLayoutTab.

With the tab cre­at­ed, we use the setFields method to set the fields to the tab and then set the tab using setTabs() on the field layout.

Final­ly, we save the new lay­out using saveLayout via Craft::$app->fields->saveLayout($fieldLayout).

Content Migrations in Craft is made up of the following videos: