Learn how to attach custom fields to the Product content type.
We’ll cover the following:
- How to attach fields
- How to manage fields
At this point, we’ve created our product content type, and now it’s time to attach a few fields to it. We already have a field attached, and that is the body field, which is automatically added when you create a new content type. If you don’t need the body field, you can just go ahead and delete it, but for our content type, we’ll use it.
Let’s go ahead and create a new field, which will store the price for the product. Go to ‘structure’, ‘content types’, and then click on ‘manage fields’ on the product row. Click on ‘add a field’. From the ‘add a new field’ drop-down, you select which field type you want to attach to the content type.
You can see there are a lot of different field types that can be attached. You have a basic Boolean field, which you can use to create a checkbox, and you have text fields which can be selected from down here. You also have number fields, which can be selected from here. Then you have more specific fields such as ‘date’, ’email’, and ‘link’.
The field types also offer a bit of validation. For example, if you were to use the email field type, you have to enter in an actual email address, and then you have the comments field, which adds commenting functionality to your content type. So it’s not just the basic field, it adds new functionality.
For the price field, I will select ‘number (float)’, then in the label field, I’ll enter in the field label, which will be ‘price’. Each field gets its own machine name which you can see to the right, and this machine name can be edited, but it cannot be changed after the field has been created. You can change the label, but you cannot change the machine name.
Once you have configured the label and the machine name, click on ‘save’ and continue. Then you’ll be redirected to the field settings page. Leave the allowed number of values, limited to one. We’ll cover it in more detail in another video, but essentially what it allows you to do is configure a field to allow multiple values. If this value is greater than one, you’ll see multiple fields where you can enter in multiple values, but for now we’ll leave it limited to one.
Then just click on ‘save field settings’. Once the field settings page has been saved, you’ll be redirected to the field edit page. From this page, you can change the label. This won’t change the machine name, it’ll just change the label. Just be aware of that. You can add in some help text, which will appear below the field. You can also make the field mandatory by checking ‘required field’.
Then if you scroll to the bottom of the page, you can set the default value of the field, from the default value field set. Each field will throw in its own settings, which can be configured. Because we are using a float field, we can also configure the minimum and maximum value, we can add in a prefix, and we can also add in a suffix.
Let’s go ahead and configure this field. I’ll scroll up. In the help text, I will add ‘product price’. Let’s also make the field mandatory by checking the required field checkbox. In ‘prefix’, I’ll put in a dollar sign. In ‘suffix’, I will add ‘AUD’, meaning Australian dollars. Everything looks pretty good. Then I’ll click on ‘save settings’.
Let’s create another field and this one will be used for the product summary. Again, click on ‘add field’ and from the ‘add a new field’ drop-down, scroll all the way to the bottom and then select text (plain, long).
Now, you may be asking, “What’s the difference between text (plain, long) and text (formatted, long)?” Both are text areas, but if it says ‘formatted’, that means a text area will be used and users can format text. They can make a bold, they can make an underlined. They can also add in a link if it’s allowed.
For this product summary which we are creating, I don’t want users to format text. So in this case, I’ll select text (plain, long). Then in the label field, I’ll enter in a product summary, and then I’ll click on ‘save’ and ‘continue’. I’ll leave the field settings as it is, and click on ‘save field settings’. Then in the help text I’ll enter in ‘enter in product summary’. Then I’ll click on ‘save settings’.
Now that we’ve created our two fields, let’s go ahead and create an actual product page. Go to ‘content’, ‘add content’, and then ‘product’. You should see the two new fields which we just created, ‘price’ and ‘product summary’. Let’s now fill in this form.
I’ll enter in ‘power boat’ into title, and let me copy over some random text. I’ll set the price to 1.99. Then let me copy some of this random text into the product summary. Let’s just put in one paragraph and then click on ‘save’. Here is our first product. We have the title, we have the body, and then if we scroll down, we can see price and product summary.