Book Image

Drupal 10 Development Cookbook - Third Edition

By : Matt Glaman, Kevin Quillen
Book Image

Drupal 10 Development Cookbook - Third Edition

By: Matt Glaman, Kevin Quillen

Overview of this book

This new and improved third edition cookbook is packed with the latest Drupal 10 features such as a new, flexible default frontend theme - Olivero, and improved administrative experience with a new theme - Claro. This comprehensive recipe book provides updated content on the WYSIWYG (What You See Is What You Get) editing experience, improved core code performance, and code cleanup. Drupal 10 Development Cookbook begins by helping you create and manage a Drupal site. Next, you’ll get acquainted with configuring the content structure and editing content. You’ll also get to grips with all new updates of this edition, such as creating custom pages, accessing and working with entities, running and writing tests with Drupal, migrating external data into Drupal, and turning Drupal into an API platform. As you advance, you’ll learn how to customize Drupal’s features with out-of-the-box modules, contribute extensions, and write custom code to extend Drupal. By the end of this book, you’ll be able to create and manage Drupal sites, customize them to your requirements, and build custom code to deliver your projects.
Table of Contents (17 chapters)

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…

  1. Go to Structure and then Content types. Click on Add content type to begin creating a new content type.
  2. Enter Services as the name, and an optional description.
  3. Select Display settings and uncheck the Display author and date information checkbox. This will hide the author and submitted time from services pages.
  4. Click on the Save and manage fields button to save the new content type and manage its fields.
  5. By default, new content types have a Body field automatically added to them. We will keep this field in place.
  6. We will add a field that will provide a way to enter a marketing headline for the service. Click on Add field.
  7. 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.

  1. Click on Save field settings on the next form. On the following form, click on Save settings to finish adding the field.
  2. 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.