Programmatically building custom content types in Drupal 8/9

lakshmi , Credit to  volkotech-solutions Jun 04

In Drupal 8/9, Creating the custom content types in Drupal 8/9 programmatically using YAML files and configurations for dynamic generation and site integration.

In this blog, we will create a custom content type called "Car detail" with two default fields: title and body.

Create a custom module:

Modules in Drupal are located in the /modules folder. We'll create two custom and contrib folders inside the /modules folder. Additional modules from drupal.org will be stored in the contrib folder, and our custom modules will be stored in the custom folder.

Create a .info.yml file:

Let's name our custom module custom_content_type. Create the folder /modules/custom/custom_content_type. In this folder, we'll need to create the custom_content_type.info.yml file and add the following code.

name: Car Detail
type: module
description: 'Adds Car detail content type.'
core_version_requirement: ^8 || ^9
dependencies:
  - node

create a new content-type:

Create a file custom_content_type/config/install/node.type.custom_content_type.yml. This file will notify Drupal that a new content type should be created.

langcode: en
status: true
dependencies:
 enforced:
   module:
     - custom_content_type
name: 'Car Detail'
type: custom_content_type
description: 'Content type that can be used to add <em>Car information</em> page.'
help: ''
new_revision: false
preview_mode: 1
display_submitted: true

Add the body field:

Create a file custom_content_type/config/install/field.field.node.custom_content_type.body.yml. The body field will be added to our content type by using this file.

langcode: en
status: true
dependencies:
 config:
   - field.storage.node.body
   - node.type.custom_content_type
 module:
   - text
id: node.custom_content_type.body
field_name: body
entity_type: node
bundle: custom_content_type
label: 'Car Detail'
description: 'More specific information about the book.'
required: false
translatable: true
default_value: {  }
default_value_callback: ''
settings:
 display_summary: true
field_type: text_with_summary

Teaser view of content-type:

Create a file custom_content_type/config/install/core.entity_view_display.node.custom_content_type.teaser.yml. This file instructs Drupal on how to display the teaser for our custom content type.

langcode: en
status: true
dependencies:
   config:
       - core.entity_view_mode.node.teaser
       - field.field.node.custom_content_type.body
       - node.type.custom_content_type
   module:
       - text
       - user
id: node.custom_content_type.teaser
targetEntityType: node
bundle: custom_content_type
mode: teaser
content:
   body:
       label: hidden
       type: text_summary_or_trimmed
       weight: 101
       settings:
           trim_length: 600
       third_party_settings: {  }
   links:
       weight: 100
hidden: {  }

The default view of content-type:

Create a file custom_content_type/config/install/core.entity_view_display.node.custom_content_type.default.yml. This file instructs Drupal on how to display the default content of our custom content type.

langcode: en
status: true
dependencies:
   config:
       - field.field.node.custom_content_type.body
       - node.type.custom_content_type
   module:
       - text
       - user
id: node.custom_content_type.default
targetEntityType: node
bundle: custom_content_type
mode: default
content:
   body:
       label: hidden
       type: text_default
       weight: 101
       settings: {  }
       third_party_settings: {  }
   links:
       weight: 100
hidden: {  }

Display entity form:

Create a file custom_content_type/config/install/core.entity_form_display.node.custom_content_type.default.yml. This file instructs Drupal on how to display the form when creating a new node of our custom content type.

langcode: en
status: true
dependencies:
   config:
       - field.field.node.custom_content_type.body
       - node.type.custom_content_type
   module:
       - text
       - user
id: node.custom_content_type.default
targetEntityType: node
bundle: custom_content_type
mode: default
content:
   body:
       label: hidden
       type: text_textarea_with_summary
       weight: 101
       settings: {  }
       third_party_settings: {  }
   links:
       weight: 100
hidden: {  }

Let us take a closer look at what is contained in these YML files.

  • langcode: langcode is an abbreviation for language code. The current content language is used if the langcode is NULL.
  • dependencies: It is recommended that a custom module have an enforced dependency. If you do not include this in the module, Drupal will not remove the content type when you uninstall it.
  • name: Name of the Content-type example Car Details.
  • type: machine name of Content-type.
  • description: a short description of a content type.
  • new_revision: Allow admin to SET the "create a new revision" option.
  • preview_mode: Allow the author to "preview" the node option before submitting it.
  • id: This attribute represents field ID.
  • field_name: This attribute represent the name of the field.
  • entity_type: This field indicates which entity owns it.
  • bundle: This field belongs to the Custom content type.
  • label: Label the name of the field.
  • required: We can also make the fields required by setting the required value to true or false.
  • translatable: We can set the value true or false to make the content type translatable.
  • default_value: You can enter a value here to set the default text for the body field section.
  • display_summary: Displaying the body text's summarised value. We can enable or disable it by setting the field value to true or false.
  • field_type: This is one of the types of Text fields where the site administrator can configure their preferred text input type. As a result, we must pass the field value on field_type.
  • hidden: If you hide some of the fields from view, you can mention them here.

Enable a module:

For all possible ways to enable a module click here.

Using the Manage administrative menu, navigate to the Extend page in the list of modules, search the car details module and then select its checkbox. Scroll down to the bottom of the webpage, and then click Install.

Navigate to the /node/add

custom content type

Conclusion:

That's all! You have successfully created a custom content type programmatically in Drupal 8/9.

 

Comments