Creating a custom content type with custom fields
Drupal excels in the realm of content management by allowing different types of content. In this recipe, we will walk you through creating a custom content type. We will create a Services type that has some basic fields and can be used in a scenario that brings attention to a company’s provided services.
You will also learn how to add fields to a content type in this recipe, which generally goes hand in hand with making a new content type on a Drupal site.
How to do it…
- Go to Structure and then Content types. Click on Add content type to begin creating a new content type.
- Enter
Services
as the name, and an optional description. - Select Display settings and uncheck the Display author and date information checkbox. This will hide the author and submitted time from services pages.
- Click on the Save and manage fields button to save the new content type and manage its fields.
- By default, new content types have a Body field automatically added to them. We will keep this field in place.
- We will add a field that will provide a way to enter a marketing headline for the service. Click on Add field.
- Select Text (plain) from the dropdown and enter Marketing headline as the label.
Important note
The Text (plain) option is a regular text field. The Text (formatted) option will allow you to use text formats on the displayed text in the field.
- Click on Save field settings on the next form. On the following form, click on Save settings to finish adding the field.
- The field has now been added, and content of this type can be created.
How it works…
In Drupal, content entities can have different bundles. A bundle refers to a different type of that entity type. The word bundle comes from it being a bundle of fields since each bundle of a content entity type can have different fields. When working with nodes, they are synonymous with content, and bundles for nodes are referred to as content types.
When a content type is created, a default body field is created for it. This is performed by calling the node_add_body_field()
function in the node.module
file. It is a great reference point for those who wish to see the steps for programmatically defining a bundle field outside of the user interface.
Fields can only be managed or added if the Field UI
module is enabled. The Field UI
module exposes the Manage Fields, Manage Form Display, and Manage Display options for entities, such as Nodes, Blocks, and Taxonomy Terms.