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.

Advertisements

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.