Learn how to reuse fields across different content types. We also look at what settings can and can’t be changed on reused fields.
So far we have covered how to attach fields and configure its widgets. Now I want to show you how to reuse a field, because the ability to reuse a field plays an important part in your site building process. Let’s start things off by creating a brand new content type called course and we’ll reuse the price field on this content type. Creating a content type should be easy because we have already created one.
Let’s go to structure, content types, and then click on add content type. And in the name field, I’ll enter in course, and in the description field I’ll add used for courses. And from the manage form settings, I’ll turn off preview. And from publishing options, I’ll uncheck promoted to front page. And from display settings, I’ll uncheck display author and date information. And from menu settings, I’ll uncheck main navigation.
Once everything has been configured, click on save and manage fields. Click on add field. And instead of clicking on the add a new field dropdown, click on reuse an existing field. From here, you can see all of the fields which can be reused on the content type. Let’s select the price field, and here you can see the machine name, field_price, and then let’s change the label to course price. You may notice that we can’t set or change the machine field name, and that is because we are reusing a field. The machine name has already been set.
Once you’ve changed the label, go ahead and click on save and continue. Once we are on the field edit page, let’s add in some help text and I’ll add in add course price here as the help text. And let’s scroll down and put in a prefix, which is the dollar sign and I’ll add in AUD as the suffix, and let’s just leave everything as it is, and then click on save settings.
And now I’ll open up another tab and I’ll pop it to the left and I’ll pop this one to the right. And then in the browser on the left, let’s go to add content, and we should see course, and here we can see our price field. When you reuse a field, there are a few things you should be aware of. If we edit the field which we just reused and then click on field settings, any options changed on this page will be changed across all the fields. If we were to change this allowed number of values to unlimited, let’s go ahead and do that now. Now, if we jump to the browser on the left and refresh, we can see now that the course price field allows us to add in an unlimited amount of values.
But if we go to content, add content, and then product, it has also changed the price field on the product content type, now we can add in an unlimited amount of values. Remember, we are reusing the same field across both content types. So let’s go back to the build settings page. Any settings on this page will apply to the price field across all of the content types. The breadcrumb on this page makes things a little confusing because looking at it, you would think this page is just for the course price and any settings on here will just affect the course price field. But in fact, if you change anything here, it’ll change it across all of the price fields.
If you need to have a price field with different field settings, then unfortunately you’ll need to create two separate fields. Let’s go ahead and change this back to a single value and then click on save field settings. Now, if we jump to the browser on the left, and take note, we are on the product content type and if we refresh, you will see that the price field has been changed and now it’s back to a single value.
Let’s go and edit the course price field for one last time. And I should mention that you can change any of these settings on the field edit page, and it’ll only affect the field instance on the course content type, and it won’t affect the price field on the product content type, that is why we can change the help text and this help text will only appear with the course price field, and it won’t change the price field on the product content type. You can change any of these options as much as you want. The ability to reuse fields is a great feature in Drupal, but just remember you can’t reuse a field and expect to change the field settings. Any configuration on the field settings is shared across all of the fields.