Chapter 4. Taxonomy

One of the Drupal features new Drupal users under-use and misunderstand is Taxonomy. New Drupal users are overwhelmed with all of the other features and functions provided by the platform, and they bypass what may be one of the most powerful and useful features that Drupal has to offer. In this chapter you will create and use taxonomy terms to categorize content so that visitors can easily find information related to a specific topic.

Taxonomy Overview

At the simplest level, taxonomy provides a mechanism for categorizing content on your site. One of the benefits of a library is that the librarians have taken the time to examine every book, movie, periodical, and music item that they have on the shelves in their building. The librarians, or the publishers, have painstakingly thought about how to categorize each item so that it easy to locate. Not only is it easy to locate an item in the library, it is also easy to locate items that are similar to the item that you are interested in. For example, you could walk into a library and locate the section for all items related to web design. In that section, you could find books on topics ranging from using Photoshop to create graphics to programming in PHP, all thanks to the work of the librarians and the use of a system to categorize similar items.

Think of how painful it would be to walk into a library where everything is thrown on shelves without regards to subject, title, author, or category. Within a few minutes of searching haphazardly around the aisles for specific book, you would likely either leave out of sheer frustration or would ask a librarian, who would hopefully have some idea of where the book might be. Taxonomy is a lot like categorizing items that are in a library, but for the content that resides on your site. If you categorize the content, you make it easier for people to find what they're looking for, and you also make it easier for people to find things that are similar to an item that that they may be looking at.

In Drupal taxonomy is divided into two elements: a vocabulary and term. A vocabulary is simply a group of related terms, where a term is a word or phrase that describes a distinct aspect of a vocabulary. Another way to think of a vocabulary is as a category and a term as a "tag," which is a common tool used on blogs to categorize content. The benefits of Drupal's taxonomy over just tagging content is the ability to associate multiple terms (tags) as a single vocabulary, making it possible to display a list of all content for a vocabulary (multiple tags) instead of just a single tag.

Let's take a look at a real-world example. Let's say the site that we are creating is one that is focused on sports news. Our targeted audience is people who like to follow what is happening with their favorite teams. If we think about how people might want to search and navigate content on our site, we might think of organizing the content by the type of sport, for example:

  • Football

  • Baseball

  • Basketball

  • Hockey

  • Soccer

People may also think about sports by team:

  • Ravens

  • Trailblazers

  • Lakers

  • Raiders

  • Yankees

In this example, we would define a vocabulary for type of sport and assign terms to that vocabulary for each sport listed. We could also create another vocabulary for teams, and assign each of the teams listed to a term associated with that vocabulary. We could then use Drupal's taxonomy feature to associate each piece of content authored on the site with a sport (such as basketball) and a team (such as the Trailblazers). We could then use taxonomy's built-in features to list all content items that are associated with specific vocabulary on a page.

Creating Vocabularies

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 (e.g., types of sports) then a single vocabulary is likely all that you will need. If the subjects are not related (e.g., a general news website with stories about various technologies, health care, jobs, and entertainment), then you may need several vocabularies, one per subject area.

Once you've identified at least one vocabulary, click on the Structure link in the admin menu at the top of any page on your site. 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. See Figure 4-1.

The Tags vocabulary

Figure 4-1. The Tags vocabulary

To add a new vocabulary, click on the "Add vocabulary" link at the top of the list, revealing the form shown in Figure 4-2. In the Name field, enter Type of Sport, and enter a brief description in the Decryption field.

Creating a new vocabulary

Figure 4-2. Creating a new vocabulary

Once you've entered the values in both fields, click Save, which results in the list of vocabulary items to be displayed with your new vocabulary shown in the list. See Figure 4-3.

Your newly created vocabulary

Figure 4-3. Your newly created vocabulary

The next step is to create a list of terms that are associated with the "Type of Sport" vocabulary. To create terms, click on the "Add terms" link for the vocabulary that you created. Clicking on that link reveals the form shown in Figure 4-4. Enter Basketball as the name of the term, and enter a brief description that expands on the meaning behind the term.

Adding terms

Figure 4-4. Adding terms

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 on the Link tab at the top of the page to see the complete list of terms for the vocabulary, shown in Figure 4-5.

The complete list of terms

Figure 4-5. The complete list of terms

At this point we've created a vocabulary and the terms that we will use to categorize content. We now have to identify which content types will use this vocabulary as a method for categorizing content, and configure our vocabulary so that it will appear on the content creation screens for those content types.

Assigning a Taxonomy Vocabulary to a Content Type

Providing content authors with the ability to assign one of the terms to a new piece of content requires that a site administrator updates the all of the content types to include a reference to the vocabulary that we wish to link to content on our site.

The first step is to identify all the content types that you want to associate with the new vocabulary. You may decide that all content types will use the vocabulary to categories the content created on your site, or you may decide the vocabulary is only appropriate for one or a few content types. For example, you might want to restrict which content types would be used if you had a vocabulary that listed terms for event venues (e.g., cafeteria, gym, courtyard, soccer field, and so on). That vocabulary may only be appropriate for a calendar event content type and not your generic page content type.

As an example, let's update the "Generic page" content type on the test site to incorporate the ability to tag content with the type of sport vocabulary. The first step is to click on the Structure link in the admin menu at the top of each page of your site. Click on the "Content types" link to get to the page that lists the available content types (shown in Figure 4-6).

Available content types

Figure 4-6. Available content types

On the list of content types, you'll see a link for managing the fields associated with this content type. We'll learn in Chapter 10 how to add several types of fields to our content type (e.g., you may wish to add a file upload, an additional text box to collect specific information, check boxes, radio buttons, or a select list to expand the content collected when someone uses that content type). For now we will concentrate on adding the taxonomy vocabulary to our content type so that an author can select one of the types of sports terms.

Click on the "Manage fields" link to expose the form used to add our vocabulary (see Figure 4-7). On this form, you will notice a section dedicated to adding a new field. Enter "Type of Sport" in the Label field, enter "type_of_sport" as the internal field name that Drupal will use to reference this field, and choose the field type that you wish to use to display the list of terms From the list of options choose "Select list" if you have a long list of terms in the vocabulary. If you have a short list of terms, "Check boxes/radio buttons" is a better option as Drupal displays all of the options on the screen rather than having to scroll through a very short select list. The last option is to render a text box where the author can enter the term. This option automatically begins to search the list of terms when the author enters the first character, and lists all terms that meet the characters that the author entered. If no term is found that matches the word entered by the author, a new taxonomy term is created. This option makes it easy to expand the list of terms by allowing authors to create a new term when the author creates a new article. If you have a tightly controlled list of terms and don't want authors to have the ability to expand upon the list (such as a list of job titles that are set by the HR department), you will not want to use this option. For our example, use the "Select list" option, as we don't want authors to have the ability to add new types of sports to our vocabulary.

Managing fields

Figure 4-7. Managing fields

Click on the Save button. You'll next see a screen where you can specify the vocabulary that provides the source for the list of terms that will appear in the select list (see Figure 4-8). For this example, select "Type of Sport" and click on "Save field settings."

Determining field settings

Figure 4-8. Determining field settings

The next screen you see after clicking save allows you to:

  • Change the label that will be displayed next to this field.

  • Specify whether the taxonomy term is a required field (whether the author must select from one of the items before saving the piece of content).

  • Provide help text.

  • Specify a default value that should be used in the case where the author does not select value.

  • Specify how many values an author can select. If you used the radio buttons/checkbox widget type, selecting 1 will render the list as radio buttons. Selecting more than 1 will result in the list being rendered as checkboxes.

  • You can change the vocabulary that will be used as the source of the list of values that will be displayed as options to the author.

After changing any of the values on this page, click Save.

Selecting a Taxonomy Term when Creating Content

Based on our actions in the previous step, creating a new content item using the Basic page content type will now present the author with a list of values that they can select from to categorize the content they are authoring. To test this feature, click on any of the "create content" links that we have described previously (such as the create content link in the gray bar near the top of the page). From the list of content types listed, click on the "Basic page" content type. When the Basic page creation page is displayed, notice that there is a new select list field where the author can select the type of sport to assign to this content item (see Figure 4-9).

Creating a basic page

Figure 4-9. Creating a basic page

Clicking on Save results in Drupal displaying our new page with a new field, "Associated Sport," with Basketball listed as the option that was selected.

To demonstrate the power of taxonomy, create two additional pages using the same taxonomy term you selected in the first example. On the final content item that you created, once you have saved it, click on the term that you used. In Figure 4-10 you would click on Basketball.

A new content item assigned to the Basketball taxonomy term

Figure 4-10. A new content item assigned to the Basketball taxonomy term

The result of clicking on the term is a page that lists all other pages that were created and assigned to the Basketball taxonomy term (see Figure 4-11).

All pages assigned to the term Basketball

Figure 4-11. All pages assigned to the term Basketball

Drupal automatically renders all of the articles that are associated with the selected term. The list is sorted in date/time order, with the most recently added basic page listed at the top of the list. You will also notice an RSS feed icon at the bottom of the page. Drupal also created an RSS feed for all the pages that are associated with this taxonomy term. Clicking on the RSS icon will render the list as a standard feed.

Creating Human- and Search-Engine-Friendly Lists

By default, Drupal creates URLs for lists of content that are related to taxonomy terms, as shown in Figure 4-12.

Drupal-created URLs

Figure 4-12. Drupal-created URLs

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 on the Structure menu at the top of the page. On the Structure page, click on the Taxonomy link. On the Taxonomy page, click on the "List terms" link 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 on the Edit tab. In the URL alias field (shown in Figure 4-13), 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, "basketball." 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 example above the URL would be http://localhost/basketball (see Figure 4-14).

Setting the URL alias for a taxonomy term

Figure 4-13. Setting the URL alias for a taxonomy term

Hierarchical Terms

What if you need to define a hierarchical structure of taxonomy terms, say, for example "basketball." You need the ability to further categorize basketball content by:

  • Basketball

    • High School

    • College

      • Division 1

      • Division 2

      • Division 3

    • NBA

      • Eastern Conference

      • Central Conference

      • Western Conference

Fortunately, Drupal provides a simple mechanism for creating a hierarchical structure of taxonomy terms. To update our example, return to the "Add term" link next to the vocabulary for "Types of Sports." Click on the Structure menu at the top of the page, and on the Structure page, click on the Taxonomy link. Click on the "Add term" link next to the vocabulary where you wish to add a new term, which reveals the term creation screen. Begin by adding the term for High School. Enter High School as the term name and then click on the Relationships link at the bottom of the form. In the list of terms, select Basketball as the parent term and then click Save. Continue the process by entering College and NBA, also selecting Basketball as the parent term. To create the third level of the hierarchy, enter Division 1 as the term name and, for the relationship selected, "College" as the parent. Continue the process until you've created all of the terms listed above. The resulting structure should look something like the list shown in Figure 4-14.

The resulting list

Figure 4-14. The resulting list

We now have the ability to assign taxonomy terms to content items at the child level as well as at the parent level.

The resulting Page that is created using this method now shows the associated sport as "High School." Clicking on High School would render a list of all pages that are associated with the term "High School" under "Basketball."

This approach provides a finer level of granularity for categorizing content.

Assigning More Than One Vocabulary

There may come a time when categorizing content by a single vocabulary represents a constraint that you must overcome to address a complex requirement for content categorization. Fortunately Drupal does not constrain you on how many vocabularies you can assign to a content type. Simply follow the steps we performed earlier in this chapter to add a second field to the content type. Simply select a different vocabulary as the source for the values that you wish to present to the author.

Summary

Hopefully this chapter shed some light on the power and simplicity of taxonomy. I suggest that you start using taxonomy on your first site, because the more you use it the more comfortable you will be with its capabilities and the power that it brings to the content you deliver to your visitors. I will continue to leverage taxonomy throughout the rest of this book as I cover other advanced Drupal features.

..................Content has been hidden....................

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