Sign in Registration
ruen

Creating forms in Drupal using the Form API, a simple example

To obtain various data from users on the site, various forms are used. These forms can be any type of form: text fields, lists, radio buttons, checkboxes, etc. It is not surprising that such a powerful system as Drupal has a special API for convenient and fast creation of forms, it is called the Form API .

drupal-form-api

What is the Drupal Form API ? This is a whole separate topic, which should be understood by any more or less Experienced Drupal Developer . This includes various functionality for working with fields: creating, checking, sending, modifying, and more. Creating forms manually in Drupal is not the way to go, you should use the Form API .

How to create a form in Drupal ? Let's say we have a mymodule module in which we need to create a form at / form-test . Here's an example of a simple form:

  // implementation of hook hook_menu
function mymodule_menu () {
$ items = array ();

$ items ['form-test'] = array (
// title
'title' => 'Sample form',
// instruction to use Drupal forms
'page callback' => 'drupal_get_form',
// function that creates the form
'page arguments' => array ('mymodule_get_form'),
// allow access to all
'access callback' => true,
);

return $ items;
}

function mymodule_get_form ($ form, & $ form_state) {
// fill the form array with data
// array key is the name of the field, by which you can later get the value of the field

$ form ['textfield'] = array (
// field header
'#title' => 'Enter text',
// field description
'#description' => 'Description for the text field',
// field type
'#type' => 'textfield',
// is it required to fill
'#required' => true,
);

$ form ['file'] = array (
'#title' => 'Select file',
'#description' => 'Enter a description for the file',
'#type' => 'file',
'#name' => 'files []',
);

$ form ['textarea'] = array (
'#title' => 'Enter your message',
'#description' => 'Description for the message',
'#type' => 'textarea',
);

// you can place any html if you specify markup
$ form ['html'] = array (
'#markup' => '& lt; div> & lt; span> Any HTML can be placed & lt; / span> & lt; / div>',
);

$ form ['submit'] = array (
'#type' => 'submit',
'#value' => 'Submit',
);

return $ form;
}

function mymodule_get_form_validate ($ form, & $ form_state) {
// an example of checking, if the textarea field is empty, then an error is set on this field
if ($ form_state ['values'] ['textarea'] == '') {
form_set_error ('textarea', 'Fill in the field');
}
}

function mymodule_get_form_submit ($ form, & $ form_state) {
// create an array for data that can be used later as you like
$ data = array ();
foreach ($ form_state ['values'] as $ field) {
// fill the array with data
$ data [] = $ field;
}

// set the message and redirect the form to the main page of the site
drupal_set_message ('Data received and processed successfully');
$ form_state ['redirect'] = '';
}
 

As you can see, a form in Drupal can have three functions:

  • a function to create a form, for example mymodule_get_form ;
  • form validation function, for example, mymodule_get_form_validate - you can check form values ​​for correctness in it;
  • a function for submitting or submitting a form, for example, mymodule_get_form_submit - in this function you can do whatever is necessary with the received data.

Thus, it was covered, what is the Form API in Drupal , as well as how to create a simple form using this toolkit.

Comments (0)
For commenting sign in or register.
Оставить заявку

Latest articles

Popular sections

Eqsash (Tools)

Android app - VK LAST USER ID, отучитель от зависимости и т.д.:
Available on Google Play

Amessage (Communication)

Login to the web version
Android app:
Available on Google Play

Share this

Subscribe to

YouTube

Books

IT notes - In simple language about the most necessary things (HTML, CSS, JavaScript, PHP, databases, Drupal, Bitrix, SEO, domains, security and more), PDF, 500 p.