×

Warning message

The subscription service is currently unavailable. Please try again later.

Drupal 7 Tutorial: Core Fields Structure

Drupal 7 tutorial: Core fields structure

Over the last several tutorials, we've added various different fields to content types. Now that we familiarize ourselves with adding and configuring fields-- it's time to take a step back and look at some of the important best practices to assure we're getting the most out of this powerful system. Drupal core provides us with a basic, but very useful set of field types. If you're a programmer or have worked with databases before-- you'll lightly recognize Drupal's core fields as a parallel standard database field types. But if you haven't spent much time working with databases-- these field types might seen somewhat convoluted. A quick overview from a 20,000 foot perspective should help you better understand what options are available and how they relate to each other.

In general, there are just five categories of field types in core.

  • Text: for storing alpha numeric data-- such as body copy or persons name.
  • Number: for storing things that we might want to do math upon.
  • Booleans: which is just a fancy way of saying something that has only two states, such as "yes or no" or "true or false."
  • Terms: for categorizing content, and files: for documenting things such as images.

In prior versions of the field system, what is called CCK prior to Drupal 7, you edit fields by specifying what you want to store in the database. This is great for people who are familiar with databases, but not as intuitive for non-developers. In Drupal 7, you specify something called "the field type" that automatically creates a database store structures and provides the appropriate widgets for entering the data. To gain a better idea of how fields works, let's take a look at how these three layers are organized for Drupal's core field types. 

The first category of field is text. Text fields are very open, and can store virtually anything you can type into your web browser. There are four types of these fields: text, list of subtype text, long text, and long text with summary. The first two are great for shorter snips of content. The latter types can store very large amounts of copy. Use a text field for short free form input of alpha numeric strings-- such as a persons name or a street address. Use a text list when you want to let users select between a controlled set of alpha numeric options-- such as selecting from a list of colors or specifying what country they live in. The long text field is best for when you think you might have to input something longer than 255 characters. It automatically gives you a mutli-line text area input versus a single line input. The long text with summary is really a specialty field. It was primary included to support teasers for known bodies. But there are times where this function might come in handy, particularly if you want to select a way for people to provide a synopsis of text that they have entered.

Number fields provide a way of entering numeric data. This is particularly useful for any data that we might want to do math on, or put numeric parameters around such as as minimum or maximum amounts. There are actually five types of numeric fields. Integers and lists of subtype integers, which are used to store whole numbers and floats, decimals, and lists of subtype float for storing numbers of decimal values. Using integer type for free form input of whole numbers-- such as the quantity of an item you might want to order or an account number. Use the list variant when you want to limit the values used to a predetermined list such as the expiration month and year for a credit card. Use floats or decimals for free form input of a number that might have a decimal value. Floats are used whenever the decimal precision is unknown such as the mileage of a run or a bike ride. People might want to enter at whole miles, 10th of a mile, quarter miles or even using some other type of precision. Use decimals where the precision is fixed-- such as a price in U.S. dollars with a number of cents is fixed to two decimal points. List of floats are used pretty rarely in practice, but might come in handy for certain unusual use cases. For example, you might want to enable people to enter in their heighth allowing half inches. So the list might be: half an inch, 1 inch, 1.5, and so on. One useful option I should note about numeric lists is they can be used with alpha numeric options. This might at first seem confusing. Why not just use a text list instead? What the numeric list is doing, is it's storing a number reference to an alpha numeric option and this can be very useful in certain situations to help you standardize your data. So let's say for example that you're creating a survey where the answers are ratings 1 through 5. But instead of having people just selecting "number" there could be text qualifiers such as three might be average, four might be above average, and five might be excellent.

Booleans are the simplest field type. They only have two values, on and off. You can label the on and off values to be whatever you want. Let's say for example you're building a form for ordering a sandwich you might want to have options such as mayo or no mayo. One of the interesting uses of Booleans is building single item check boxes-- such as the ubiquitous "I agree to these terms" acceptance box found on many websites when license and software are using licensed items. Term references allow us to associate terms with an entity. They can be structured as a pre-defying list or can be used free form using a text field to enter multiple comma separate values. This is often called free tagging. In many ways, this field is very similar to integer lists, and often they can be used interchangeably. But there are some differences though, term references have more options as their tied to Drupal's taxonomy system. Learn more about how taxonomy and term references work in a later video. File fields enable us to attach fields from our local computer to a piece of content. The file field allows for uploading virtually any kind of file-- and the image field provides special options specific to images. Both of these are covered in more depth in previous videos. In this tutorial, we took a look at how Drupal's core fields are organized. These fields are relatively basic compared to the more specialized fields that you'll find in contrib modules. Yet they are the most used and form the foundation for extending content in most Drupal sites. It's vital to know what types are available and the best options to use in each situation. In a future video, we'll be doing a review of advanced fields, available and contrib.