Join Tom Geller for an in-depth discussion in this video Adding fields to content types, part of Drupal 7 Essential Training.
- View Offline
In the video about creating content types, we made a content type called product and we will use that from here on out to make nodes about the products we offer at our fictional Olive Oil Company. But right now there's not much we can do with it and we can see that by going up to Add Content and clicking Product. It just has a title and a body and that's pretty much it. So we are going to extend this content type by adding fields for price, an image for the product, and the SKU identifier. That's the Stock Keeping Unit in merchant talk.
To do that, we go up to Structure and Content types, and then next to Product we click Manage Fields. To add a new field, we enter the information along this line under Add new field. I am going to add SKU as the Label, the field name, and this is the name that Drupal looks at internally as opposed to the label that you see when you look at the field itself, will be sku. That has to be all lowercase. And as a field type, I am going to choose Integer and the only option there is Text field.
Now this type of data to store is a very big list and each one has its own set of options. Rather than explain all of them here, I will tell you about them in a different video in this course which is about exploring field types and options. But for now we'll just click Save. Now when you add a field to a content type, you typically go through two screens. The first setting screen is for that field everywhere that it appears in your Drupal site and it usually doesn't have very many settings. In this case it has no field setting, so I will just say Save.
Then we come to the second page, which only affects the SKU, the field that we've just created, when it's in the Product content type. You see when you create a field you can reuse it in different content types. We won't be getting into that here. The important thing is to go through both screens. In our case, I am going to make it a required field and we have a minimum and maximum value. Now let's just say that our olive oil company keeps track of things by using a five digit number. Well, in that case, the minimum should be 10,000 and the maximum should be 999,999 and for Help Text, I am just going to add "If the item has no SKU use 99,999." The rest we'll leave as is.
But again, I'll come back and talk about some of these things later on. Click Save Settings. Now I said, I was going to add two other fields. One is a price and the other is an image. I will quickly add the price. Label will be Price, field name Price. That will be a Decimal type and again Text field is the only option and Save. Here we have a few more options in the first setting screen. We can choose how precise we want the decimal to be and how many digits to show to the right of the period.
In our case, we don't need to store a very precise number. However, the smallest precision we can have for decimal is 10, so that's fine. Number of digits to the right of the decimal, fortunately it's already on 2, which is what you get in money, and since we are in the United States, or at least I am, I will say Decimal point for the marker and Save those field settings. We will make this field required. We don't have a minimum or maximum. For the Help Text, I will just say "Enter as numbers and a period for the decimal point.
No need to include the dollar sign." Under Prefix, I am going to add a dollar sign and you will see how this comes out in just a few minutes. The rest I will leave as it is and then say Save. Finally, we are going to add an image. This is going to be a little bit different. I am going to call it Product photo, product_photo, and the Field type will be Image. We have only one option there as well, Image, and Save.
In the first screen of settings, I'm going to put in a default image. I will click Browse and I have one in my exercise files here, there's no-image-available, and say OK. The upload destination, we don't really need to talk about right now. Essentially, you have the option of having either public files, which are downloadable by anybody, or private files. That's getting into an area that's a little bit beyond this course. But for now we'll click Save field. On the next screen, the options are quite different because we are dealing with an image as opposed to a number.
I will leave things mostly as they are. In particular, I'm going to leave the image resolution by pixels as it is because I'll be showing you how to handle that in a later video about modifying image styles. However, I do want to make sure that people don't upload absolutely enormous files and fill up our server, so I will turn this into 2 MB. That should be big enough for most graphics. I will enable the Alt and Title fields and you'll see what those do when we create a node of this content type and the remainder I will leave as it is and click Save Settings.
So we have our product content type and we have the fields that we want. Let's take a look at what happens now when someone wants to create a product type node. As usual we would go up to Add Content and select Products. There is our Title, there is our Body and as we scroll down further, there are the three fields that we added. But there are few things that I think are not quite right with this. For one thing, we don't need all this space. We are going to have very short product descriptions.
Also, I don't think it should be called Body. We could call it something else like Product Description. That will be a little bit easier for people who are entering products later, who perhaps aren't as well trained. This way they'll know exactly what a product description is as opposed to a body. Furthermore, I think these things, the SKU, Price, and Photo, should come above because they are the first things you enter. So we'll go back to our content type by clicking Structure and Content types. Then next to Products at the bottom of the screen click once again Manage Fields.
The first thing I'll do is I will change the name of that Body field by going over and clicking Edit next to it. Instead of Body, it will be Product description. Instead of being 20 rows, it will just be 5 rows. I don't want people to enter a lot of text there. Go down and Save settings. Now, to move things around, I just grab these little compass icons and drag them as I like. SKU goes there, Price goes there, and I think I will leave the photo after it. Go down and save.
Now, we're ready to start adding products. I'll go up to Add Content and Product again, and I'll just add one as a sample. Product is Pure Grade A olive oil, 1 liter. The SKU, let's just say it's 15011. The price is $18, and notice we have our little bit of information here telling us how to do that. .00.
I will enter a quick description here, and I'll even put in a little bit of HTML. We know what HTML is permitted of course by this list here under the Filtered HTML text format. You can learn more about text formats in a separate video earlier in this course. So that's the product description. Then we have a place for a photo. Again, I will browse and go back to my exercise files, select it, and upload. Now, the alternate text is useful for those people who are looking at the site through a different kind of screen reader, that is, not a browser, or for people who are having problems with seeing.
So it's very useful to have. This will be once again, let me see. It was Pure Grade A olive oil. Photo of Grade A olive oil bottle. The title is just what people see when they hover over the image. So in this case, I am going to say 1 liter for $18. Scroll down and save, and there it is. It looks pretty good. We have our photo.
We have our price, our SKU, and our description. Of course we could have put all of this information into the Body field and just left it at that. But by making separate fields, we will later be able to play with the data in interesting ways, particularly when we start using views later in the series. But first we're going to take advantage of field settings to make things prettier. We don't really need to have the word product photo here for example and this could be in line with price as one line instead of two. You will find out how to fix all of these things and do a lot more in the video about adjusting field display.
- Comparing Drupal with other content management systems
- Understanding nodes
- Creating basic content
- Adding blogs, discussions groups, and polls
- Subscribing to RSS feeds
- Creating new content types
- Managing users, roles, and permissions
- Creating and modifying block regions
- Adding menus
- Extending site capabilities with modules