. Adding usability with the #states attribute
2. Ok. This file is identical to the previous one except we're added these lines right here in our "import_more_form" function. All we've done is added the "states" attribute to the "Computer stats" item in the form array.
3. This "states" attribute tells the form what different sort of actions are associated with different states. So in this, what we are going to do is make this visible only if the input that's called "computer" has a value of "yes". So here are the different parts of this.
4. First is the "state". In this case, we are making it "visible". So this is the state which we are going to make it, not the state in which it begins.
5. And we're going to make it visible if this item here and this element is a selector in jQuery and what we're doing is taking an input and we're passing it a attribute and a value that that input should have. So what we can do is use the name that we assign to it here. We assigned it the name of "computer" and we can use that here as the name because if you look at the HTML of the input itself, the name is the same as what we assigned it in the form array.
6. So we are taking this jQuery selector, and we'll cover jQuery selectors in another video, and we're assigning it an array. And the array contains this state in which this item should be in, in order to trigger this state in our current field. So, it's checking for a value of "yes" and if the value is"yes", then it will trigger this state.
7. Ok. Let's go ahead and save this and look at this in action so you can see it. Now save it.
8. I'm going to jump back to our form and refresh the page.
In our most popular collection, we build a strong foundation for developing in Drupal, covering the most commonly used APIs, building practical and interesting modules, and covering a tremendous amount of ground. Just a few of the topics we cover:
- How to build and manipulate forms with the Form API
- How to write jQuery to build a rich interface
- How to programmatically manipulate content and user data