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 .
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.
Latest articles
- 01.03.26IT / Уроки PHP Уроки простыми словами. Урок 3. Все операторы PHP с примерами, с выводом работы кода на экран.
- 28.02.26IT / Уроки PHP Уроки простыми словами. Урок 2. Типы данных в PHP с примерами.
- 27.02.26IT / Уроки PHP Уроки простыми словами. Урок 1. Коротко о языке веб-программирования PHP. Основы синтаксиса.
- 26.02.26IT / Database Errors when migrating from MySQL 5.6 to 5.7 and how to fix them - database dump import failed with an error or INSERT does not work. Disabling STRICT_TRANS_TABLES strict mode or using IGNORE
- 31.01.26IT / Misc Convert office files DOC, DOCX, DOCM, RTF to DOCX, DOCM, DOC, RTF, PDF, HTML, XML, TXT formats without loss and markup changes
Popular sections
Eqsash (Tools)
Share this
Subscribe to
YouTube
Books
Am (Мессенджер)
Общение смайликами со звуком.
Не работает видео? Смотреть в VK- Download APK-файл (Android);
- Веб-версия (и PWA);
- Page проекта.
4003