Drupal 8 Site Building - Modeling Data with Fields

April 5, 2016
2
3

Site builders, accustomed to working with Drupal 7 or Drupal 6, are able to do much more with Drupal 8 right out of the box. There are many excellent enhancements you should know more about.

Because new Drupal adopters have the essential functionality of the most popular contributed modules to use, I think this is going to make Drupal easier to discover and learn. Content editors will also benefit (See Content editing enhancements in Drupal 8, if you haven't already reviewed that. It covers the main changes in the admin UI, too).

From the point of view of structuring content types - site builders have increased responsibilities. As new applications like Flipboard come along, and new information design trends such "cards," site builders are going to be more and more concerned about how their data will appear and be reused in other contexts, and how relevant their content appears in search engines hungry for structured data. Drupal 8 is way ahead of the game in helping you make the most of your data.

You might be curious about how adopting Drupal 8 will change the site building experience, so let's start by focusing on one of the most important tasks you'll undertake: modeling data.

Entities and Fields in Drupal 8

Probably the first changes site builders will notice in Drupal 8 are the changes to content types and fields: the bread and butter of site building in Drupal. However, the field changes affect not only content types, but any entity that can have fields. (Still confused about Entities? Check out this intro to entities on Drupal.org.)

When you go to edit an existing content type, Article, you’ll notice some differences in the tabs. No more comment fields and display tab. And we have a new tab: "Manage form display."

d7 to d8 comparison of content type tabs

Curious what this means? I'll dig into these changes in this blog post:

  • New fields in Drupal 8
  • Managing fields in Drupal 8
  • Form modes in Drupal 8
  • Comment form configuration
  • Step-by-step tutorial to add a field in Drupal 8, so you can see some improvements that have been made to the experience
  • Getting the most out of your data with Schema.org and RESTful web services

New fields in Drupal 8!

There are new fields available in Drupal 8.

  • Datetime (This doesn't include the recurring date functionality from the Date field module. You’ll need to go to contrib modules for that.)
  • E-mail
  • Entity reference - Link to users, terms, content, etc. with an auto-complete field.
  • Link
  • Telephone

These new modules might be some of the first modules you go to enable. As you do, you’ll notice the new handy module filter.

  • Go to Menu > Extend > Filter to search for a specific module.
  • The filter works only by name, not the description.

Drupal 8 module filter

Managing fields in Drupal 8

The experience of creating content types, taxonomy, and new users looks very similar to someone coming from Drupal 7. In Drupal 7, on the "manage fields" page, you added fields and arranged how the form appears, and which widgets content editors would use.

D7 maneg fields

In Drupal 8 these two actions had to be separated. Manage fields only controls how the data is modeled; that's where you configure which fields and data types make up your new content type. Here you delete and add fields, and they are listed A-Z.

D8 manage fields

This change was to make way for form modes.

Form modes in Drupal 8

With Drupal 8 you can have multiple forms for adding content types, taxonomy terms, users, and so on.

Go to Administration > Structure > Display modes > Form modes to see the form modes available.

Form mode default

Out of the box, there is one example form mode - this is for the User form, an ideal use case. Say for example you want to lower barriers for membership, and present new users with as few fields as possible on registration: a username and email address. Later, after they join, you'd like to give them the option to edit their full profile and add a picture and configure other settings. In this case you'd have two "form modes" one for registration and one for editing your profile.

  1. Go to Administration > Configuration > People > Accounts > Manage form display.
  2. Click to expand "Custom display settings"
  3. Enable the "Form Register" form mode.
    enable register form mode
  4. Then you can move fields to be hidden during registration.
    register form changes

Comment forms in Drupal 8

First let’s just review how things were in Drupal 7. In Drupal 7 you could add fields to comments on specific content types. You couldn’t add comments to things such as users, taxonomy terms or any other entities or bundles.

Drupal 7 comment fields

In Drupal 7, you could disable comments on content types, but an author with the “Administer content” permission could override that setting and enable comments on a Basic page. This was because comments were part of the node entity type. All the comments settings were part of the initial content type configuration.

comment configuration

In Drupal 8 you'll configure Comment forms independent of a specific content type. This means you can have a comment form type for articles, and another for forum replies. Nice! It also means you can reuse a comment form settings and ensure consistency across your site. Comments are added in the same way other fields are, so there isn’t a separate configuration tab for “Comment fields” and “Comment display”.

comment field reuse

This also implies that if you don’t add a Comment field to a content type, you can’t just override that per-item. That’s cool!

The comment form settings include:

  • Default value: Open, Closed, Hidden.
  • Comment form settings: Threads, Comments per page, Allow title, Show reply, Preview settings.

Now that the comment form settings are similar to the settings for any other field, I think this is going to make site building easier to learn in Drupal 8.

comment field settings

Step-by-step tutorial: Add a field

To test out a new field, add an “Email” field to a content type.

  1. Go to Menu > Structure > Content types > [select content type] > Manage fields.
  2. Add the email field.
  3. You’ll notice on the next step where configure global settings for the field, you can now set an arbitrary number of values. In Drupal 7, you could only set 1-10 “limited” and then unlimited. So you can have 15 or 35 as a limited number! Nice move, D8 team :)
    Arbitrary limited items allowed
  4. Now you can Manage display of the field in the form. For example, you can now add placeholder text!
    Form display - placeholder
  5. Test the new field. Your placeholder text disappears as soon as the user starts adding content.
    placeholder config
  6. As soon as you submit, the form will get validated. In the case of the email field, it will run a check if the content is formatted correctly and give an error on the screen. Even the new errors look pretty snazzy.
    d8-email-validation.png

Getting the most out of your data with Schema.org and RESTful web services

Site builders in Drupal 8 should be thinking more about how the structured content types they create will produce data that can be used and reused in many ways, and how it will appear in search engine results or how it might be used by other applications. Drupal 8 provides two new key tools.

1. RESTful web services for site builders [#1975444] In Drupal 7, getting Drupal to output something other than HTML was quite difficult. Now Drupal can be used as a method for processing data which can be reused by more than just browsers. This will open up potential for builders to more easily integrate with other systems and applications. For site builders it's going to be important to consider from the start the potential for the data they model to be used and reused in other ways than just through their site and browsers.

2. Schema.org integration for site builders [#2034127] Drupal 8 provides Schema.org RDF mappings for the default content types, articles and pages. For example, this provides search engines with meta data indicating which text is the "author" of an article and what is the "date" and what is a "comment." This will greatly improve search engine results and the use and re-use of data.

Right now there is no UI for adding new RDF mappings for your own custom content types. You could, however, copy the core mappings for the article content type rdf.mapping.node.article.yml and create your own for your own content type, such as rdf.mapping.node.restaurant.yml

rdf mappings example from article

Schema.org provides clear descriptions of each of the attributes for many things, including Restaurant http://schema.org/Restaurant for example. In fact it might make sense as one of the first steps of content modeling in Drupal, go check Schema.org first.

If you'd like to know more about the State of schema.org in Drupal 8, please read this by my colleague, scor, Stephane Corlosquet https://groups.drupal.org/node/309513

Sign-up for our Developer Blog Newsletter

Thanks!

Add comment

By submitting this form, you accept the Mollom privacy policy.

confusion re fields & modules

In an otherwise great post, I was confused by the section
New fields in Drupal 8!
where you discussed new fields like Email & then suddenly you talk about modules.
In the 1st part, I had assumed, the discussion was about new fields w D8 out-of-the-box,
Latter, it become clear that these fields needed a module to be included.

thx for writing these tutorials!! gve

The ability to model business

The ability to model business objects was what turned me on to Drupal 6, with CCK. It expands Drupal's range from Web sites to information systems.

In fact, I rewrote the intro to business information systems course I teach. Rather than using MS Access, students use Drupal to build simple business systems. (https://bis.cybercour.se/)