Applying new Drupal 8 core field types

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!

Getting ready

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.

How to do it…

This section contains a series of mini recipes, showing how to use each of the new core field types.

Link

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.

  1. The Link field type does not have any additional field level settings that are used across all bundles.
  2. Click Save field settings to begin customizing the field for this specific bundle.
  3. Using the Allowed link type setting, you can control whether provided URLs can be external, internal, or both. Selecting Internal or Both will allow linking to content by autocompleting the title.
  4. The Allow link text defines if a user must provide text to go along with the link. If no text is provided, then the URL itself is displayed.
  5. The field formatter for a Link field allows you to specify rel="nofollow" or if the link should open in a new window.

The e-mail field

The Email field is provided by core and is available without enabling additional modules:

  1. The Email field type does not have any additional field level settings that are used across all bundles.
  2. Click Save field settings to begin customizing the field for this specific bundle.
  3. There are no further settings for an Email field instance. This field uses the HTML5 e-mail input, which will leverage browser input validation.
  4. The field formatter for an Email field allows you to display the e-mail as plain text or a mailto: link.

The Telephone field

The Telephone field is provided by the Telephone module. It is not enabled by default with the standard installation profile:

  1. The Telephone field type does not have any additional field level settings that are used across all bundles.
  2. Click Save field settings to begin customizing the field for this specific bundle.
  3. There are no further settings for a Telephone field instance. This field uses the HTML5 e-mail input, which will leverage browser input validation.
  4. The field formatter for a Telephone field allows you to display the telephone number as a plain text item, or using the tel: link with an optional replacement title for the link.

Date

The Date field is provided by the Datetime module. It is enabled by default with the standard installation profile.

  1. The Date module has a setting that defines what kind of data it will be storing: date and time, or date only. This setting cannot be changed once field data has been saved.
  2. Click Save field settings to begin customizing the field for this specific bundle.
  3. The Date field has two ways of providing a default value. It can either be the current date or a relative date using PHP's date time modifier syntax.
  4. By default, Date fields use the HTML5 date and time inputs, resulting in a native date and time picker provided by the browser.
  5. Additionally, Date fields can be configured to use a select list for each date and time component:
    Date
  6. The default date field formatter display uses Drupal's time formats to render the time format. These are configured under Configuration and Regional and language in the Date and time formats form.
  7. Dates and times can be displayed as Time ago to provide a semantic display of how far in the future or past a time is. The formats for both are customizable in the display settings.
  8. Finally, dates and times can be displayed using a custom format as specified by the PHP date formats.

The Entity Reference

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!

  1. The interface allows you to select a Content, File, Image, Taxonomy term, User, or Other. Selecting one of the predefined options will preconfigure the field's target entity type.
  2. When creating an Entity Reference field using the Other choice, you must specify the type of item to reference. This option cannot be changed once data is saved.

    Note

    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.

  3. Click Save field settings to begin customizing the field for this specific bundle.
  4. The Entity Reference field has two different methods for allowing users to search for content: using the default autocomplete or a View.
  5. Depending on the type of entity you are referencing, there will be different entity properties you may sort the results based on.
  6. The default field widget for an Entity Reference field is to use autocomplete, however there is the option to use a select list or checkboxes for the available options.
  7. The values of an Entity Reference field can display the referenced entity's label or the rendered output. When rendering a label it can be optionally linked to the entity itself. When displaying a rendered entity you may choose a specific view mode.

How it works…

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.

There's more…

Having Drupal 8 released with new fields was a large feature and we will cover some additional topics.

Upcoming updates

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.

Views and Entity Reference

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.

See also

  • Chapter 3, Displaying Content through Views, providing an entity reference result view
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset