Skip to content
Tutorials Center
My learning

Topics

Lesson 11 of 37 ยท Content Types and Fields

Content Types and Fields

Create Drupal content types and add fields: text, number, date, boolean and list fields, required values, cardinality, default values and machine names.

A content type is a template for one kind of content. When an editor chooses "Add content > Event", the form they see, the data stored and the page shown to visitors all come from the Event content type. Building good content types is the core craft of a Drupal site builder: get the model right and listings, search, APIs and permissions fall into place.

This lesson walks through planning and creating a content type and its fields, shows the configuration it produces, and gives you a runnable stand-in for the form validation logic.

Plan before you click

Start on paper. For a college site you might sketch an Event:

Field Type Notes
Title Base field Always present
Summary Text (plain, long) Required, short teaser
Date Date and time Required, can have an end date
Place Text (plain) Optional
Capacity Integer Optional, minimum 1
Online Boolean Checkbox
Level List (text) Beginner, Intermediate, Advanced

Ask three questions for every field: Who edits it? Where is it shown? Do I need to search or filter by it? A value you want to filter by deserves its own field, not a place inside a big text box.

Creating a content type in the UI

  1. Go to Structure > Content types > Add content type (/admin/structure/types/add).
  2. Give it a Name ("Event"). Drupal suggests a machine name (event). Machine names are permanent in practice: they appear in config files, code and URLs, so choose carefully.
  3. Set the description, whether to create new revisions by default, whether to display author and date, and the default publishing options.
  4. Save, then use Manage fields to add fields.

Adding a field

Under Manage fields > Create a new field:

  1. Choose a field type (Text, Number, Boolean, Date, Reference, List, Link, Email and more).
  2. Name the field; the machine name gets the field_ prefix (for example field_event_date).
  3. Configure field storage settings: maximum length, whether values are integers or decimals, and the allowed number of values (cardinality: 1, a limit, or unlimited).
  4. Configure the field instance: label, help text, required, default value, and constraints such as minimum and maximum.

Storage settings are shared if the field is reused on other content types, so some can no longer be changed after content exists. Plan carefully.

Reusing fields

You can add an existing field to another content type (Re-use), which gives both the same storage. Reuse suits a generic "Summary" or "Image" field. Do not reuse a field when its meaning differs, even if the type matches.

The resulting configuration

Everything you click creates config. The instance of the date field on the Event type is exported like this:

YAML
langcode: en
status: true
dependencies:
  config:
    - field.storage.node.field_event_date
    - node.type.event
  module:
    - datetime
id: node.event.field_event_date
field_name: field_event_date
entity_type: node
bundle: event
label: 'Date and time'
description: ''
required: true
translatable: false
default_value: {  }
field_type: datetime

Because it is plain YAML, you can review changes in a pull request, as described in Configuration Management Basics.

Form validation, the idea

When an editor saves a node, Drupal validates each field: required fields must have a value, numbers must respect their min and max, dates must be valid. These rules come from the field definitions, so they apply everywhere the entity is saved, not just in one form. The stand-in below models a content type definition and validates a submission against it.

JS Stand-in: validating content against field definitions

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The first call prints an empty list (valid). The second shows every problem at once, as Drupal does.

Cardinality: one value or many

A field can hold several values. A "Speakers" field with unlimited cardinality shows an "Add another item" button. In code a multi-value field is a list of items:

PHP
foreach ($node->get('field_speakers') as $item) {
  echo $item->value;
}

Choosing cardinality is a modeling decision: if each value needs its own sub-fields (a speaker with name, photo and bio), a single text field is not enough. You model the speaker as its own entity and reference it, which is the topic of Taxonomy and Entity References.

Machine names and renaming

You can change a field's label any time. You cannot change its machine name in the UI, because it is part of the database table names and every config reference. If you must rename, create a new field, migrate the data and delete the old one.

Default values and help text

Use default values to save editor time ("Online" unchecked) and help text to answer questions before they are asked: "Enter the start time in the venue's local time." Short, concrete help text reduces support requests more than any manual.

Seeing the data model as a diagram

flowchart LR
    E["Content type: event"] --> A["title (base field)"]
    E --> B["field_summary: text"]
    E --> C["field_event_date: datetime"]
    E --> D["field_capacity: integer"]
    E --> F["field_level: list"]

One content type, several typed fields.

A second stand-in: building the edit form

Drupal builds the editor's form from the same definitions. This stand-in generates a simple text outline of the form for a given content type, showing why a model change instantly changes the form.

JS Stand-in: generating a form from field definitions

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the machine name of a field like 'Event date' usually?

    Machine names are lowercase with underscores and, for fields you create, start with field_.
  2. 2. Where do you add fields to a content type?

    Structure > Content types > (type) > Manage fields is where fields are added and configured.
  3. 3. What does cardinality mean for a field?

    Cardinality is the allowed number of values: one, a limit or unlimited.
  4. 4. Why is a value you want to filter by best stored in its own field?

    Separate fields can be filtered, sorted and exposed in listings and APIs, unlike data buried in free text.

Summary

  • A content type is a bundle of the node entity type with its own fields, form and display.
  • Plan fields on paper; choose permanent machine names carefully.
  • Each field has storage settings (type, cardinality) and instance settings (label, required, default).
  • Validation and forms derive from field definitions; config can be exported as YAML.

Next: Field Types, Widgets and Formatters