Using the UI to export the code for your custom fields

Last updated on
13 October 2016

Sometimes when you're pulling in a content type from a custom module, you'd also like to pull in fields specific to this content type. Being able to have the fields automatically created allows you to uninstall and reinstall in multiple sites without leaving excess fields behind and guaranteeing you won't forget to add them. There are two ways to add these fields to your codebase that we'll cover.

Prerequisites

  • Drupal 8.0.x installed
  • Have a custom module (the name of the module used in this example is foobar)
  • Have a custom content type in the module (Please follow the steps in the parent page. The content type name will be Car Brand)

Adding additional fields to our content type

There are two ways to go about doing this step. You can either use the UI to create the fields and export the configuration into files or you can just write the files yourself. I like using the first option just to make sure you don't miss anything that would be needed, however the fields are exported with 'field_' at the beginning rather than 'foobar_car_brand_' to associate them with our custom content type. We'll cover using the UI to create the fields and export the code to be used in your custom module on this page.

Using the UI to create fields

As stated before, this method is a little better for users who aren't very familiar with the requirements Drupal has for adding fields to a content type.

Enable the custom content type

If you didn't already enable the foobar module at the end of the parent page, go ahead and do so now. If you now go to the Create content page, you will see that you're able to create a new node of the content type "Car Brand".

/admin/structure/types/manage/car_brand/fields

Now that you have the module enabled you can go to the manage fields section of your content type. Add all the fields that you want. Just make sure one of the fields you add is "Brand Information" (field_brand_information) of the field type Text(formatted, long, with summary), and delete the Body field. Leave everything else as default.

Using the UI to export field configuration

/admin/config/development/configuration/single/export

Once you get your fields added, go to the Configuration Synchronization page. At the top there are three tabs. Select "Export" from those three. Then select "Single item" just below those tabs.

Get export configuration

Attached below are four images showing which items you need to export for the field "field_brand_information".

  • Field Storage
  • Field
  • Entity View Display
  • Entity Form Display

Underneath the configuration code block you'll see a filename that you should create within your custom module. Copy everything in the configuration code into the specified file except for the first line starting with "uuid: ".

Note: if you added any additional fields, you'll need to export the Field Storage and Field for each one of them. The same goes if you have created multiple displays such as a teaser. In this example we only show the default displays.

modules/custom/foobar/config/install/field.storage.node.field_brand_information.yml
Field Storage Export

modules/custom/foobar/config/install/field.field.node.car_brand.field_brand_information.yml
Field Export

modules/custom/foobar/config/install/core.entity_view_display.node.car_brand.default.yml
Entity View Display Export

modules/custom/foobar/config/install/core.entity_form_display.node.car_brand.default.yml
Entity Form Display Export

Uninstall and Re-enable the foobar module

Now that you have all of your field configurations in your codebase, it is time to uninstall the foobar module. Once you have uninstalled the module, go back and enable the module to see if it brings in your custom fields. If you now go to the Create content page, you will see that you're able to create a new node of the content type "Car Brand" and it will include our new field named "Brand Information" and any other fields you've created.