Learn how to categorize content by adding the required taxonomy fields to the product content type.
We’ll cover the following:
- How to create taxonomy term fields.
- Configure the widgets on the fields
- Configure the formatters on the fields
So far, we’ve created two vocabularies, product category, and product tags. But to actually categorise or tag a product, we’ll need to create two entity reference fields on the product content type. One for the product category and another field for the product tags. Let’s start things off by creating the product category field. So let’s go to structure, content types. And then click on manage fields on the product row and then click on add field.
And from the add a new field of dropdown, scroll down and select taxonomy term under reference, in label entering product category. And then click on save and continue. From the type of item to reference, make sure taxonomy is selected and leave the allowed number of values limited to one. And then click on save field settings. Scroll down to the reference type of fieldset, and make sure you check product category under vocabulary, and then click on save settings.
Now let’s create the product tags field. So click on add field again. And then select taxonomy term under reference again and in label entering product tags, and then click on save and continue. Again, make sure taxonomy term is selected from type of item to reference. But change allowed a number of values to unlimited because we want to add multiple tags. And then, click on save field settings and then scroll down to the reference type fieldset and make sure product tags is checked under vocabulary. And also, you want to check create reference entities if they don’t already exist. This means Drupal will automatically create the term if it doesn’t exist and then scroll down and click on save settings. Let’s now go to the product content form. So I’ll click on content and open that up in a new tab and then go to add content and then product.
And if we scroll down, we should see our two new fields, product category, and product tags. Let’s now change the widgets on these fields because product category needs to be a select list, and product tags needs to be the auto-complete tags style widget. So let’s jump back to the previous tab and go to manage form display. And let’s scroll down and move product category below price and move product tags below product category. And change the product category to a select list and then change product tags to autocomplete tags style and then scroll down and click on save. And then, if we jump back to the other tab and then refresh, we should see product category as a select list and product tags as an autocomplete widget. Now let’s go and edit an existing product. So I’ll go to content, and I’ll edit the Learn PHP product.
And from the product category, I will select tech, and I’ll tag this product with PHP. And then to add in another tag, I’ll put in a comma, and the second tag will be Web dev, and then I’ll scroll down and click on save. And if we view the product, so let me click on learn PHP. You’ll notice that we can’t see the product category or the product tags, and that is because we need to enable the fields on the full content view mode. So let’s jump back to the other tab and then go to manage display and then click on full content.
And then, if you scroll down, you can see product category and product tags right here. And let’s move up product category below body and product tags below product category. And let’s change the label to inline on both and then click on save. And now, if we jump back to our product page and refresh, we should see product category and product tags below the body field. And then, if you click on one of the terms, it’ll take you to the taxonomy term index page. And here, you can see the Learn PHP product, which has been categorised under tech.