Extra Block Types (EBT) - New Layout Builder experience❗

Extra Block Types (EBT) - styled, customizable block types: Slideshows, Tabs, Cards, Accordions and many others. Built-in settings for background, DOM Box, javascript plugins. Experience the future of layout building today.

Demo EBT modules Download EBT modules

❗Extra Paragraph Types (EPT) - New Paragraphs experience

Extra Paragraph Types (EPT) - analogical paragraph based set of modules.

Demo EPT modules Download EPT modules

Scroll

Programming Custom Fields into Your Content Type

12/04/2025, by Ivan

Sometimes, when extracting a content type from a custom module, you may want to include fields associated with that content type. Automatically creating fields allows you to remove and reinstall on multiple sites without leaving behind unnecessary fields and ensures you don’t forget to add them. There are two ways to add these fields to your codebase, which we’ll cover here.

Prerequisites

  • Drupal 8.0.x is installed.
  • You have a custom module (the module name used in this example is foobar).
  • You have a custom content type in the module (please follow the instructions on the parent page. The content type name will be Car Brand).

Adding Additional Fields to the Content Type

There are two ways to do this. You can use the UI to create fields and export the configuration into files, or you can write the files manually. I prefer the first option to ensure you don’t miss any required details, although fields are exported with a field_ prefix instead of foobar_car_brand_ to associate them with our custom content type. This page will walk you through manually coding fields into your custom module.

Manually Coding Fields in Your Module

As noted earlier, this method requires a bit more knowledge and precision about what the field should do and how it should look. Each added field must have two YAML files that hold the configuration data. They are named: field.field.node.car_brand.field_brand_information.yml and field.storage.node.field_brand_information.yml.

Remove the Custom Content Type

If you’ve already enabled the foobar module as described on the previous page, go ahead and uninstall it now.

foobar/config/install/field.storage.node.field_brand_information.yml

This file tells Drupal to create our field.

# field.storage.node.field_brand_information.yml
langcode: en
status: true
dependencies:
  module:
    - node
    - text
id: node.field_brand_information
field_name: field_brand_information
entity_type: node
type: text_with_summary
settings: {  }
module: text
locked: false
cardinality: 1
translatable: true
indexes: {  }
persist_with_no_fields: false
custom_storage: false

foobar/config/install/field.field.node.car_brand.field_brand_information.yml

This file tells Drupal to attach the field to our content type.

# field.field.node.car_brand.field_brand_information.yml
langcode: en
status: true
dependencies:
  config:
    - field.storage.node.field_brand_information
    - node.type.car_brand
  module:
    - text
id: node.car_brand.field_brand_information
field_name: field_brand_information
entity_type: node
bundle: car_brand
label: 'Brand Information'
description: 'More specific information about the car brand'
required: false
translatable: false
default_value: {  }
default_value_callback: ''
settings:
  display_summary: false
field_type: text_with_summary

foobar/config/install/core.entity_form_display.node.car_brand.default.yml | foobar/config/install/core.entity_view_display.node.car_brand.default.yml

These two files must be updated from the previous page to include any new fields you've added. I’ve included two example fields: Additional_field_1 and Additional_field_2 so you can see what it looks like with multiple fields.

Note: If you don't want to add additional fields, remove the dependencies and configuration related to them from the code below.

# core.entity_view_display.node.car_brand.default.yml
langcode: en
status: true
dependencies:
  config:
    - field.field.node.car_brand.field_brand_information
    - field.field.node.car_brand.field_additional_field_1
    - field.field.node.car_brand.field_additional_field_2
    - node.type.car_brand
  module:
    - file
    - text
    - user
_core:
  default_config_hash: Nfnv6VMugBKl6EOqi_U0I_LQ1ZQpbNDN3a9GXHWUBz4
id: node.car_brand.default
targetEntityType: node
bundle: car_brand
mode: default
content:
  field_brand_information:
    weight: 101
    label: above
    settings: {  }
    third_party_settings: {  }
    type: text_default
  field_additional_field_1:
    weight: 103
    label: above
    settings:
      link_to_entity: false
    third_party_settings: {  }
    type: string
  field_additional_field_2:
    weight: 102
    label: above
    settings: {  }
    third_party_settings: {  }
    type: file_default
  links:
    weight: 100
hidden: {  }
# core.entity_form_display.node.car_brand.default.yml
langcode: en
status: true
dependencies:
  config:
    - field.field.node.car_brand.field_brand_information
    - field.field.node.car_brand.field_additional_field_1
    - field.field.node.car_brand.field_additional_field_2
    - node.type.car_brand
  module:
    - file
    - path
    - text
_core:
  default_config_hash: qZE-qJ04DTTNggVVQdVOPQmpE_I69GQ_LqB32kXivVg
id: node.car_brand.default
targetEntityType: node
bundle: car_brand
mode: default
content:
  created:
    type: datetime_timestamp
    weight: 2
    settings: {  }
    third_party_settings: {  }
  field_brand_information:
    weight: 7
    settings:
      rows: 9
      summary_rows: 3
      placeholder: ''
    third_party_settings: {  }
    type: text_textarea_with_summary
  field_additional_field_1:
    weight: 6
    settings:
      size: 60
      placeholder: ''
    third_party_settings: {  }
    type: string_textfield
  field_additional_field_2:
    weight: 8
    settings:
      progress_indicator: throbber
    third_party_settings: {  }
    type: file_generic
  path:
    type: path
    weight: 5
    settings: {  }
    third_party_settings: {  }
  promote:
    type: boolean_checkbox
    settings:
      display_label: true
    weight: 3
    third_party_settings: {  }
  sticky:
    type: boolean_checkbox
    settings:
      display_label: true
    weight: 4
    third_party_settings: {  }
  title:
    type: string_textfield
    weight: 0
    settings:
      size: 60
      placeholder: ''
    third_party_settings: {  }
  uid:
    type: entity_reference_autocomplete
    weight: 1
    settings:
      match_operator: CONTAINS
      size: 60
      placeholder: ''
    third_party_settings: {  }
hidden: {  }

Enable the Custom Content Type

Now you can enable the foobar module. Once enabled, go to the “Create Content” page and you should be able to create a new “Car Brand” node, which will include our new Brand Information field.

We’ll look at adding these fields via the UI on the next page.

Drupal’s online documentation is © 2000-2020 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.