The field system is what makes creating content in Drupal so robust. With Drupal 8 some of the most used contributed field types have been merged into Drupal core as their own module. In fact, Entity reference is no longer a module but part of the main Field API now.
This recipe is actually a collection of mini-recipes to highlight the new fields: Link
, Email
, Telephone
, Date
, and Entity reference
!
The standard installation profile does not enable all of the modules that provide these field types by default. For this recipe you will need to manually enable select modules so you can create the field. The module that provides the field type and its installation status in the standard profile will be highlighted.
Each recipe will start off expecting that you have enabled the module, if needed, and to be at the Manage fields form of a content type and have clicked on Add field and provided a field label. The recipes cover the settings for each field.
This section contains a series of mini recipes, showing how to use each of the new core field types.
The Link field is provided by the Link module. It is enabled by default with the standard installation profile. It is a dependency of the Menu UI, Custom Menu Links, and Shortcut module.
rel="nofollow"
or if the link should open in a new window.The Email field is provided by core and is available without enabling additional modules:
mailto:
link.The Telephone field is provided by the Telephone module. It is not enabled by default with the standard installation profile:
tel: link
with an optional replacement title for the link.The Date field is provided by the Datetime module. It is enabled by default with the standard installation profile.
The Entity Reference field is part of core and is available without enabling additional modules. Unlike other fields, Entity Reference appears as a grouping of specific items when adding a field. That is because you must pick a type of entity to reference!
You will notice there are two groups: content and configuration. Drupal uses configuration entities. Even though configuration is an option, you may not benefit from referencing those entity types. Only content entities have a way to be viewed. Referencing configuration entities would fall under an advanced use case implementation.
When working with fields in Drupal 8, there are two steps. When you first create a field you are defining a base field to be saved. This configuration is a base that specifies how many values a field can support and any additional settings defined by the field type. When you attach a field to a bundle it is considered a field storage and contains configuration unique to that specific bundle. If you have the same Link field on the Article and Page content type, the label, link type, and link text settings are for each instance.
Each field type provides a method for storing and presents a specific type of data. The benefit of using these fields comes from validation and data manipulation. It also allows you to utilize HTML5 form inputs. By using HTML5 for telephone, e-mail, and date the authoring experience uses the tools provided by the browser instead of additional third party libraries. This also provides a more native experience when authoring with mobile devices.
Having Drupal 8 released with new fields was a large feature and we will cover some additional topics.
Each of the recipes covers a field type that was once part of the contributed project space. These projects provided more configuration options than are found in core at the time of writing this module. Over time more and more features will be brought into core from their source projects.
For instance, the Datetime module is based on the contributed date project. However not all of the contributed project's features have made it to Drupal core. Each minor release of Drupal 8 could see more features moved to core.
Using a View with an Entity Reference field is covered in Chapter 3, Displaying Content through Views. Using a View, you can customize the way results are fetched for a reference field.