. Creating a simple form
2. Go to your sites base folder and paste input in "sites, all , modules, custom". Ok. We have our folder here.
3. This is a module called "input". We've called it input to make it easier to identify the difference between the name of the module and the name of the functions, many of which have form in it, but this module is all about the form API in Drupal. And we have several files here.
4. We have our "input dot module" file, our "input dot info" file and our "input dot admin inc" which we will go into a little later. And we have our "steps" directory. Go ahead and open up "input dot module", which should be blank.
5. And open up the first step which is called "simple-form". Go ahead and copy this code and paste it into your "dot module" file and save it. Up to this point, we've basically been talking about forms as Drupal forms.
6. But the forms in Drupal are built on the form API, which is the name that's generally used for all of the functionality and ways that you can hook into forms and build forms in Drupal. So when I talk about the form API, that's what it means. If you'd like a really good resource for all of the attributes and elements that can be used in forms, this URL is a great one to go to.
7. If you don't know what those are, you'll learn about it as we go. So this file contains a very simple form example. It has two parts.
8. In the first part, we define a page with our "hook_menu" function. So we have a function call "input_menu" and we're giving it a title of "Form API examples", a description and the "page call-back" is called "drupal_get_form". And this is an internal Drupal function that's used to get a render array that represents a form.
9. So we create a function, which is the second part down here, which returns that render array. The rest of the arguments here are the page arguments which includes the name of the function that's going to get passed to "drupal_get_form". And this is a function that should include a render array for a form.
10. And you see that that's the same name of the function that we have down here. And then we're just defining access for this page. Let's go ahead and take a look at the function that returns the render array to "drupal_get_form".
11. So you'll see inside of this function is some very simple code. We're taking a form array and we're adding two items to it. One called "color" and one called "submit".
12. And then we're returning that form. Now there's several attributes to this function that will be the same for every function that returns a render array to "drupal_get_form", one of which is the parameters that gets passed to it. The first one is "form", which as the form gets submitted and this function gets called again to rebuild the form, these items will get passed through that form variable up here.
13. "form_submit" includes a variety of information including the values that get submitted through the form when the form is submitted. And you'll notice that's it's being passed as a reference . you can tell by the ampere stamp at the beginning of the variable name, which means that any variations that we make to this variable during the course of the function will get added to it and passed to the parent function.
14. For every item that we're defining here, we're defining a number of attributes. And you can tell which items are attributes by a hash tag that prefixes them. So this title is an attribute.
15. "type", "required" and "description" are all attributes. In then our next example , we'll go ahead and look at something that has more of these attributes. But again if you want to learn more about all of the attributes, this URL up here is a great one to go to.
16. This should be pretty straight forward as you're looking at it. We're defining the title which is actually a label that appears by default above the input. The "type", which is the type of input, and in this case it is a text field which is the same as a text input.
17. But this could be a text area, check boxes, radios and a variety of others. "required" allows us to perform some very simple validation on this to check to make sure that the input isn't empty when it's submitted. And "description", which is a short description that will display below the input in smaller text.
18. Our next item that we're adding is the our "submit" button so we are calling it "submit" and we're giving it the "type" of submit which is a submit button, and the "value" which is the text that will display inside of the button. And finally we're returning this. So all we're doing is defining the inputs that are going to be added to this form.
19. We don't have to worry about defining about where the form will be submitted or any of the additional information that we might be used to adding when we create form in plain HTML. Nothing really will happen when we submit this but lets go ahead and take a look at it in action. So the first thing we need to do is install our module.
20. I'm going to scroll down to the bottom here. And select the "Input Examples of using the Form API" module. And click "save configuration".
21. I'm going to jump back to our code and we'll see this is the path that we're defining for our example. So I'm going to copy that path and paste it into the address bar at the browser. So this is what our form looks like when it gets rendered through Drupal.
22. We have our label, we have an asterisk , a red asterisk that is added whenever the required attribute is added to an input. We have our input itself, and then we have a short description followed by the submit button. Now if I submit this, the basic validation that we added will run and we'll get an error up here that says that the field is required.
23. If we add some information and click "submit", then it will pass, but because haven't added any processing, nothing will actually happen. In this example we're passing the form "build array" directly to the menu item. But if we wanted to add some additional information or code around our form, then we would want a separate function and we would want to embed the form inside of that function.
24. so let's take a look at an example of that.
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