

In the exampleform_unsubscribe() form, you've defined two form elements: the e-mail address and the submit element. E-mail address * Form API Elements and Default Properties The markup generated by Drupal for the example form_unsubscribe() form in Listing 16–24.
#DRUPAL 8 FORM API EXAMPLE CODE#
Rendered form based on the code from Listing 16–24. The resulting markup is shown in Listing 16–25.įigure 16-5. When rendered, the result looks like those in Figure 16–5. In Listing 16–24, you define a very simple form with two elements: a textfield for the e-mail address and a submit button. What's important to you is the structure of a form and how it's transformed from the $form array to actual markup. Of course, there is more to the process to make it functional, such as validating the form and saving the submitted values, but the rest is not your concern in the theme layer. The simple function shown in Listing 16–24 is all that is required to generate form markup. How Form Markup is Generatedįorms are generated by modules.

So, how do you know when to use one over the other? This section will explain how forms are generated and will present a couple of examples using each method. Forms can also use preprocess functions, process functions, and alter hooks. Although none of Drupal's forms ship with template files, they can easily be made to use them. One thing Drupal is famous for is the ability to accomplish a single task in many different ways.

While the process of theming forms is quite different from what most front-end developers are used to, we think you'll begin to appreciate the consistency and flexibility of theming Drupal's forms. This makes it really easy for modules to build forms and guarantees consistency among generated elements. Form markup is generated using Drupal's Form API. If you have any questions just leave a note in the Comments section below and I'll be glad to help (if I can).Theming forms is a little different than working with the usual template file or theme function. I hope this Drupal form textfield example has been helpful. The fields you can add are defined in the grid at this Drupal Form API reference page. You can add even more fields to your Drupal textfield definition. Hopefully those Drupal form fields are self-explanatory, but if not, leave a note in the Comments section below and I'll be glad to add more information here. '#description' => 'Enter your first name', Here are a few additional fields added to our first textfield example: $form = array( You can add quite a few more fields to your Drupal textfield definition to help control how the HTML textfield is rendered. (If you're not comfortable with creating a Drupal module or Drupal form, please see my A simple, first Drupal form example/tutorial. That code creates an HTML textfield with the label "First Name", as you can see here:
#DRUPAL 8 FORM API EXAMPLE HOW TO#
Drupal form textfield FAQ: Can you share an example of how to create an HTML textfield in a Drupal form (Drupal 6 or Drupal 7)?Īssuming that you're already comfortable with creating a Drupal form, you can create a Drupal form textfield as easily as this: # an html textfield for our drupal form
