Learn how to manage the” allowed number of values” option on fields.
When we created the price field or any field, in fact, we are able to change the allowed number of values option. And I mentioned that we would look at this option in more detail in another video. Well, this is the video.
To start off, let’s go ahead and edit the price field and then click on Field Settings. And here you can see that we have the allowed number of values option, and it’s limited to one. This error message here telling us the settings can’t be changed is a little misleading because we can change it and it’s appearing because we have created a product page. Let’s go ahead now and edit the product which we created in a previous video. What I’ll do is I will click on Content and open that up in a new tab and I’ll pop it to the left and then pop this field settings to the right and let’s open up our product and let’s go ahead and edit the product.
When the allowed number of values option is limited to one, it means the field can only have a single value, and you can see that the price field only allows you to add a single value or price. But if we change the option to unlimited and then click on Save, and then if we refresh the browser on the left, we can now add unlimited values on the price field. And all we need to do is click on, Add Another Item to display another form element. Let me go ahead and create a few values and you can also reorder the values as well. If you have a requirement where a field has to store multiple values and they need to be reordered, then look at simply adjusting the allowed number of values. Once you have added your values and ordered them, simply scroll down and then click on Save. And if we scroll down and here you can see the five values from the price field.
What happens when you want to change the allowed number of values after content has been created? If there’s no content, then you can change it all you want. But if content has been created, then things get a little tricky. Let’s go back and edit the product. And here you can see that we have five values in the price field, and let’s jump to the right and then edit the price field and then click on Field Settings and let’s change the option back to one and then click on Save Field Settings. Straightaway you’ll notice that we get a second error message, which is there is one entity with two or more values in this field. The one entity is the product on the left.
We can’t change the allowed number of values if there is a field that has more than one value, but let’s go ahead now and change the value from one to five because the price field has five values and click on Save Settings. This time it allowed us to save the settings because the price field has five values. Now, if we were to have 20 other product pages and one of the fields had six values, well, then we would get an error message telling us that the setting can’t be changed. But in this example, we only have one product with five values. That’s why we were able to change it. Let’s go back and edit the allowed number of values again. And if we change it to four, we’ll get the same error because there’s a field with five values. But if we were to simply remove a value and then scroll down and click on Save, and now if we jump back to the right and then click on Save Field Settings again, it’ll allow us to save it because the price field only has four values.
Let’s go ahead and edit the product again, and let’s remove the other three. And then let’s edit our price field one last time and let’s set it back to one value. Just remember, that when you have content, Drupal won’t allow you to change the allowed number of values if it finds a field that has more values, than you want to limit. For example, if you want to limit the values to 10, and it finds a field with 11, 12, or 14 values, it won’t allow you to change it. To get around this you will need to edit your content and remove the extra values. If you want to change it to 10 and you have a field with 11 values, then you’ll need to remove one of those values.