Drupal 8 – Customizing Content Types

dr

At this point we could create a new content item using our Event content type. However, the Event content type only has two fields, an Event Title and a Body. Our requirements call for an event description, start date, end date, venue/address, type of seating, assistance available at the venue, type of event and the ability to provide a file that can be downloaded by the site visitor (e.g., a program). Drupal automatically creates a Body field, which we will use for the event description.

The next step is to add the Start Date field. Click the “Add field” button. The next step is to select what type of field to add or alternatively what existing field to add to this content type. A previously created field may reside in another content type and instead of recreating that field, we can reuse the definition by selecting that field from the list of existing fields. A lot of drupal developers use custom content types fields.

After clicking Date, you will see a new field on the screen where you will enter the label for the new field. This label is displayed on the content editing form so the author knows which field they are entering a value for, and is displayed to the end user when the content item is rendered on the site. Since we’re creating the start date for the event, enter “Start Date” into the Label field and click the “Save and continue” button.

The next step in the field creation process is to set the format for our date field, either date and time or just the date, and the number of allowed values. Since events typically have a time associated with them, we’ll leave the value set to “Date and time” for the “Date type” select list. We’ll also leave the “Allowed number of values” option set to “Limited” and “1” because our events only occur once.

You will see the “Allowed number of values” configuration option on every field that you create. You may have situations where you want the author to have the ability to create multiple values for this field. For example, you might have the same event occurring several times in the future. By setting the “Allowed number of values” higher than 1, the content editing form will display the same number of entry fields as you select in the “Allowed number of values” select list. If you change Limited to Unlimited, the author can create as many values as they need to, with an “Add another” button appearing below the field to enable the author to create a new entry. Think about how the field will be used and then set the “Allowed number of values” option. There may be cases where 1 is not the best solution. For this example, however, we’ll leave the value as 1. Click the “Save field settings” button to continue to the next step.

Drupal 8 – Defining and Creating Custom Content Types

d88

While you could publish all of this information in a Basic page or an Article, providing the features for sorting, filtering, making values required, calculating, and structuring how a content item is rendered on a page would be extremely difficult. Fortunately, Drupal’s ability to define custom content types makes all of the above possible, and provides many more features that you will find invaluable over time.

A custom content type is defined by you, the Drupal administrator, over and above the Basic page and Article content types. The ability to create custom content types is included in Drupal 8 core. To demonstrate the power and flexibility of custom content types, let’s create a new custom content type for capturing information about upcoming events. An event could be a concert, a play, a class, a game, or any other activity that is scheduled in advance. Custom content types are useful in responsive design.

When authoring information about an event, you may want to include the following details:

The name or title of the event
The date and time when the event begins
The date and time when the event ends
The venue or address where the event will be held
A description of the event
The price for attending the event

As you will see in a few moments, Drupal provides a simple-to-use administrator’s interface for creating and modifying custom content types. As soon as you define a custom content type, it is immediately available to those users who have the proper privileges to author, edit, publish, and delete that specific content type (Drupal provides the ability to restrict access to custom content types by user role).

Creating a custom content type takes two basic steps: sitting down and listing the types of information you want to collect, and building the custom content type using Drupal’s custom content type administration screens.

For this example, let’s create a custom content type for an event that includes the types of information listed in the previous section. To get started, click the Manage link at the top of the page and the Structure link in the submenu. On the Structure page click “Content types.”

To begin the process, do the following:

Fill in the name of the content type, which in our case is “Event.” The text below the Name field provides a set of guidelines that you should follow when creating a name for a new content type.
Provide a description of how this content type should be used, such as “A content
Type used to capture the details about upcoming events.”
Change the “Title field label” from just Title to “Event Title,” making it more descriptive and intuitive to the author who will be using this template for authoring event information.
Leave the “Preview before submitting” setting as Optional.
Provide a brief explanation of the submission guidelines for this content type. This is an optional value, and may not apply to your content type. For our Event content type, we will use “Please fill out all required fields before submitting the event” as the submission guidelines. You can choose to use or ignore this field when creating new content types.

Drupal 8 – Human and Search Engine Friendly Lists

drupp

Drupal automatically renders all of the articles that are associated with the selected term. The list is sorted by default in date/time order, with the most recently added article listed at the top of the list. You may change the order in which the articles appear by modifying the default view. You will also notice an RSS feed icon at the bottom of the page. RSS feeds are an industry standard approach for delivering lists of content to external sources, such as news reader applications. Drupal also created an RSS feed for all the pages that are associated with this taxonomy term. Clicking the RSS icon will render the list as a standard feed.

The structure of the URL is “taxonomy/term/X,” where X is the “term ID” of the taxonomy term that you are referencing. While Drupal understands what this refers to, a human and, more important, a search engine wouldn’t have a clue what this URL was related to other than looking at the title of the list and the content of the list. A simple remedy is to provide a URL alias. You can provide a URL alias by editing the taxonomy term and entering a value in the field that is shown on the form for adding a URL alias. To navigate back to your taxonomy term list, click the Manage link at the top of the page and select Structure. Search engine and human friendly lists work like a charm with responsive design.

On the Structure page, click the Taxonomy link. On the Taxonomy page, click the “List terms” button that is associated with the vocabulary where the term you wish to supply a URL alias resides. Locate the term you wish to update and click the Edit tab. In the “URL alias” field enter a descriptive URL that is easily understood by humans and search engines. For consistency and simplicity, use all lowercase letters with hyphens between words. For our example, we’re using a single word, “football.” Click the Save button after entering your URL alias. In the address bar of your browser, enter the URL to your site followed by the URL alias that you just created for your taxonomy term. In the preceding example, the URL would be http://localhost/football.

Drupal 8 – Vocabularies

drupp

The first step in using taxonomy is to identify and create the vocabularies that you will use to categorize content on your website. Depending on the focus of your site and the breadth of subjects that you cover, you may only need a single vocabulary or you may need several vocabularies. There isn’t a “correct” answer, nor is there a formula that you can use to determine how many vocabularies your site will need. The best approach is to think about the content that you will include and the subjects that the content will cover.

If the subjects are all related (for example, types of sports), then a single vocabulary is likely all that you will need. If the subjects are not related (for example, a book-related website where books may be categorized by author, subject, publisher, and targeted audience), then the use of several vocabularies may be necessary. It’s up to you, the site creator, to define the structure that best suits the purpose of your site, how you want content structured, and how you want visitors to access that content.

Once you’ve identified at least one vocabulary, click the Manage link in the admin menu at the top of any page on your site and then click the Structure link. You’ll see a list of options that includes a link to Taxonomy. Click that to reveal a page that lists all of the vocabularies that have already been defined for your site. By default, Drupal creates a vocabulary called Tags as a default generic “container” for terms.

To add a new vocabulary, click the “Add vocabulary” link at the top of the list, revealing the form. In the Name field, enter “Type of Sport,” and enter a brief description in the Description field. The Description field is an optional field and by default is not displayed on the administrative interface for taxonomy. However, you may want to use this field when rendering lists of content as a description about the content that is contained in the list. In responsive web design, the vocabularies come in pretty handy for displaying structured content.

Once you’ve entered values in both fields, click Save. Drupal then displays the list of terms associated with your vocabulary. Since this is a new vocabulary, the list is empty.The next step is to create a list of terms that are associated with the “Type of Sport” vocabulary. To create terms, click the “Add term” button for the vocabulary that you created. Clicking that link reveals the form.

After entering the term and a description, click Save. Drupal then redisplays the form to enable you to enter another term. To practice, create terms for other sports, such as baseball, football, and soccer. Once you’ve completed the process of entering the terms associated with your vocabulary, click “Type of Sport” in the breadcrumb to return to the Edit Vocabulary page, and then click the List tab at the top of the page to see the complete list of terms for the vocabulary.

Drupal 8 – Freeform Taxonomy

d88

While many of us may not be able to define the word “taxonomy,” the reality is that we use taxonomy on a daily basis as a means for categorizing “things” in our lives. If you open the doors to your kitchen pantry, you might find an orderly assemblage of food items: all of your spices on the top shelf, canned food on the second shelf, pastas and other boxed foods on the third shelf, and cereal boxes on the fourth. Categorizing your food items and putting things away in an orderly fashion so that you can easily find food items when you need them to prepare a meal is in its simplest form the use of taxonomy. Without this “kitchen taxonomy system,” you may have everything you need to make dinner jammed randomly in the pantry, but finding it may be a challenge, leading to frustration and a phone call to the local pizza delivery restaurant when you’re not able to find the ingredients you need to make a meal.

In Drupal, taxonomy is divided into two general capabilities: tagging and structured taxonomy. Both are powerful solutions and can be used simultaneously on your site. Tagging is a simplified yet powerful use of the taxonomy system, enabling content authors to enter keywords that describe the content in a text field on the content editing form. As an example of tagging content, an author who writes an article about alternative energy could use keywords, or tags, such as “solar,” “wind,” and “geothermal” to categorize the article. The keywords created by the author are typically displayed as hyperlinks at the end of the article and can be used by site visitors to locate other content tagged with the same keywords. All Drupal developers are aware of the power of taxonomy.

Tagging is freeform, meaning it’s up to the author to define what words they want to use to classify their content. A common issue with using tagging as an approach to categorize content is that different people use different words to refer to the same concept. For example, an article about rain might be tagged with the word “rain” by one author, “precipitation” by another author, and “drizzle” by a third. Site visitors trying to find articles about the general concept of rain would have a difficult time finding the ones tagged with words other than “rain.” Another common problem is misspellings. If an author tags an article about rain with “reign,” then site visitors are going to have a hard time, using taxonomy, to find that article using the word “rain.”

Drupal 8 – User Account Settings

dru

Before creating your first user account, it is advisable that you visit the user account settings page and review or modify the general user account configuration settings, of which there are many.

To access the settings page choose Manage ➤ Configuration (assuming you are logged in as the administrator), and, on the Configuration page, locate the section titled “People.” Click the Account Settings link, which will take you to the page that you will use to set various configuration parameters for user accounts:

Contact settings: you may enable individual users on the site to have a personal contact form. This feature is enabled by default; to disable it, simply uncheck the box.
Anonymous users: the name used to identify anonymous users: In most cases, leaving the default value is appropriate, which is “Anonymous.”

Administrator role: what role to associate with administrator capabilities: The role selected becomes the default role assigned as the administrator of new modules that you install on the system. Using the default value “administrator” is an appropriate action.
Registration and cancellation: defines several attributes about user account registration: Who can register accounts: if only administrators can create accounts, select the first option “Administrators only.” If any visitor to your website can create their own account, select the “Visitors” option. If visitors can request an account but an administrator must approve that request before the account is active. This option is selected by default. For demonstration purposes I’ll select the first option, so that only administrators may create user accounts. Several Drupal 8 developers share more info on their blogs.

“Require e-mail verification,” is a good option to leave checked. This option requires that the user responds to a Drupal-generated e-mail that asks them to confirm their account. This helps to avoid “bot”-created user accounts, as most bots do not have the intelligence or capabilities to respond to user account verification e-mails.

The password strength indicator is a helpful tool to indicate how strong a person’s password is. A weak password may be easily hacked, whereas a strong password is harder to crack. It’s a good tool to enable to help keep your Drupal site secure. When cancelling a user account: a set of options allows you to define what happens to content on your site that was created by this user when you disable that person’s account in the future. In most cases the default option, “Disable the account and keep all content,” meets the needs of a majority of websites. You may, however, decide that one of the other options is more appropriate for your site.

Personalization: defines whether users can add signatures to their profiles. I will leave the default values for our test site; however, you may wish to enable or disable signatures depending on whether you wish to provide those capabilities to users. The e-mail address in the “Notification e-mail address” field is the e-mail address that will appear on all user account–centric e-mails that are generated by Drupal during the registration and password recovery process. By default, the site’s e-mail address defined during the installation process is the value that will be used; however, you may override the default by entering a valid e-mail address in the “Notification e-mail address” field.

Drupal 8 – Users, Roles, Permissions

dru

Now that your site is up and running, you have a couple of decisions to make. First, will you have any administrators on the site other than yourself? Second, will your site be open to everyone, or will users need to log in to view content and other features? In this chapter, I cover how Drupal treats visitors to your site, and how you as a site administrator can configure Drupal’s user account features to restrict the capabilities of those who have user accounts on your system.

Controlling who has the ability to do what on your website is performed through Drupal’s security features. Drupal’s security features provide the ability to define who has the ability to view, create, update, delete, and participate through a combination of individual user accounts, user roles, and permissions.

Users (or site visitors) in Drupal 8 are divided into two general categories: anonymous users and authenticated users. Anonymous users are individuals who visit your website and do not log in using a user ID and password. With Drupal, you have the ability to support anonymous users, and you also have the ability to restrict what an anonymous user can do on your site. Authenticated users are visitors to your site who log in using a unique user ID and password. I’ll cover how user IDs and passwords are created shortly, but understanding the difference between the two categories of users is important. A lot of Drupal developers post useful tutorials about handling users.

Roles are a Drupal mechanism that allows you, the site administrator, to define categories of authenticated users of your website. You may define roles on your website that are department specific (e.g., a role each for human resources, purchasing, sales, marketing, and customer service), roles that are functionally oriented (e.g., content authors, content reviewers, content publishers), roles that are associated with a specific section of your website (e.g., products, support, sales, homepage), or any other definition that you can dream up. Roles are simply a way of putting authenticated users into categories, where categories are associated with specific permissions. Any authenticated user of your website may be assigned to none, one, or more than one role (e.g., you may have a user who is assigned roles of sales department, content author, and products).

Permissions in Drupal are a mechanism for controlling what a user assigned to a specific role can do. There are dozens of permissions that you can enable or revoke for each user role you have defined. Examples of permissions that you might set for a specific role include: the ability to create a new page, the ability to create a new article, the ability to edit any article regardless of who authored it, the ability to search content on the website, and the ability to add a new user account. The combination of permissions that you set for each role defines the capabilities that a user assigned to that role can do on your website once they have successfully logged in. When you combine user roles with permissions and individual user accounts, you end up with a highly configurable solution for securing access to key features and content on your website.