Creating a Custom Content Type in Drupal 8
This page is a copy of Enable-by-default configuration in a Drupal 8 module. This should be considered deprecated.
Creating a custom content type has become quite straightforward thanks to the new Configuration API provided by Drupal 8.
Prerequisites
- Drupal 8.0.x is installed.
- You have a custom module (the module name used in this example is
foobar
).
Creating a Custom Content Type
As mentioned in the introduction, creating a custom content type is done by creating a few YAML files that contain all the necessary settings. In this example, we’ll create a content type called Car Brand, which will contain two default fields: body and title.
foobar/config/install/node.type.car_brand.yml
This file tells Drupal to create a new content type.
Note: We are adding an enforced dependency on the foobar
module. Without this, Drupal won’t delete the content type when the module is uninstalled. When a site builder decides this module is no longer needed, we also don’t want the content type lingering around.
# node.type.car_brand.yml langcode: en status: true dependencies: enforced: module: - foobar name: 'Car Brand' type: car_brand description: 'Content type that can be used to provide additional information on Car Brands' help: '' new_revision: false preview_mode: 1 display_submitted: true
foobar/config/install/field.field.node.car_brand.body.yml
This file adds the body field to our content type.
# field.field.node.car_brand.body.yml langcode: en status: true dependencies: config: - field.storage.node.body - node.type.car_brand module: - text id: node.car_brand.body field_name: body entity_type: node bundle: car_brand label: Body description: 'More specific information about the car brand.' required: false translatable: true default_value: { } default_value_callback: '' settings: display_summary: true field_type: text_with_summary
foobar/config/install/core.entity_view_display.node.car_brand.teaser.yml
This file tells Drupal how to display the teaser view mode of our custom content type.
# core.entity_view_display.node.car_brand.teaser.yml langcode: en status: true dependencies: config: - core.entity_view_mode.node.teaser - field.field.node.car_brand.body - node.type.car_brand module: - text - user id: node.car_brand.teaser targetEntityType: node bundle: car_brand mode: teaser content: body: label: hidden type: text_summary_or_trimmed weight: 101 settings: trim_length: 600 third_party_settings: { } links: weight: 100 hidden: { }
foobar/config/install/core.entity_view_display.node.car_brand.default.yml
This file tells Drupal how to display the default view mode of our custom content type.
# core.entity_view_display.node.car_brand.default.yml langcode: en status: true dependencies: config: - field.field.node.car_brand.body - node.type.car_brand module: - text - user id: node.car_brand.default targetEntityType: node bundle: car_brand mode: default content: body: label: hidden type: text_default weight: 101 settings: { } third_party_settings: { } links: weight: 100 hidden: { }
foobar/config/install/core.entity_form_display.node.car_brand.default.yml
This file tells Drupal how to display the form when creating a new node of our custom content type.
# core.entity_form_display.node.car_brand.default.yml langcode: en status: true dependencies: config: - field.field.node.car_brand.body - node.type.car_brand module: - text - user id: node.car_brand.default targetEntityType: node bundle: car_brand mode: default content: body: label: hidden type: text_textarea_with_summary weight: 101 settings: { } third_party_settings: { } links: weight: 100 hidden: { }
Enabling the Custom Content Type
Now that we have our configuration files in place, we need to inform Drupal of our new custom content type. This is done by reinstalling the module. If your module has not been enabled yet, simply enable it. If it is already enabled, uninstall it and then enable it again.
Once enabled, go to the “Create Content” page, and you should see the new Car Brand content type available for use.
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.