Доклад познакомит начинающих друпалеров с идеологией, архитектурой и основами работы с формами в Drupal.
<ul>
<li> элементы форм</li>
<li> валидация форм</li>
<li> действия (CallBack) форм</li>
<li> ahah действия (CallBack) форм</li>
<li> рендер форм</li>
<li> различия между Form API в Drupal 6 и 7</li>
<li> использование форм в собственных модулях</li>
<li> tips and tricks</li>
</ul>
4. Working with forms
• Form
– Render
– System form
• Form alter
• Form validation
• Form submit
• Destination (redirect)
5. Form
Form call function: drupal_get_form($form_id);
•$form_id - the unique string identifying the desired form. If a
function with that name exists, it is called to build the form array.
drupal_get_form() does the following:
•Starts the entire form-building process by getting the $form from
the builder function.
•Translates the $form['name'] items into actual form elements.
•Performs any validation and "clean-up" that needs to be done, and
calls custom validation functions if declared.
•Submits the form if a submit function is declared, and the form has
been submitted.
•Calls any custom theming functions that have been declared.
•Returns an HTML string which contains the actual form.
7. Form element structure
Required parameters:
$form[‘element name’] = array (
‘#type’ = ‘element type’,
);
Every element automatically has these default values:
•#description = NULL
•#attributes = array()
•#required = FALSE
•#tree = FALSE
•#parents = array()
8. AhahAjax callback for an
element
• Using ‘#ahah’(D6) or ‘#ajax’(D7)
attributes to builds a form element with
a set of #ahah#ajax properties.
• Using ‘#ajax[‘callback’]’(D7) or
‘#ahah[‘path’]’(D6)#ajax[‘path’]’(D7) to
locate ajax function.
• Using additional ahahajax options to
change behavior of the form element.
9. Theming forms
• Adding '#theme' attributes to the form
and/or elements.
• Including any markup directly as an
element in the $form array.
• Break out any markup into a separate
theme function.
10. Form alter
Perform alterations before a form is rendered:
hook_form_alter(&$form, &$form_state, $form_id);
OR
hook_form_FORM_ID_alter(&$form, &$form_state);
11. Form validation
• Adding ‘#validate’ attribute (an array with list of
custom validation functions) to the form.
• Adding ‘#element_validate’ attributes (an array with
list of custom validation functions) to the form
elements.
• Using myform_validation($form, &$form_state)
function.
• The validation functions must use form_error()
or form_set_error() functions to flag the element as
having an error if the validation fails.
12. Form submit and redirect
• Adding ‘#submit’ attribute (an array with list of custom
submit functions) to the form.
• Using myform_submit($form, &$form_state) function.
• To determine where the user should be sent after the form
is processed, the _submit function can place a path or URL
in $form_state['redirect'] which will be the target of a
drupal_goto.
• Every form is redirected after a submit. If nothing stored
in $form_state['redirect'], the form will simply be redirected
to itself after a submit.