oleksandr masovets. forms in drupal. drupal camp kyiv 2011
DESCRIPTION
Доклад познакомит начинающих друпалеров с идеологией, архитектурой и основами работы с формами в Drupal. элементы форм валидация форм действия (CallBack) форм ahah действия (CallBack) форм рендер форм различия между Form API в Drupal 6 и 7 использование форм в собственных модулях tips and tricksTRANSCRIPT
Gold Sponsor ofDrupalCamp Kyiv 2011
Silver Sponsors ofDrupalCamp Kyiv 2011
Working with forms
• Form– Render– System form
• Form alter• Form validation• Form submit• Destination (redirect)
FormForm 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.
Form elements
Form element structureRequired 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()
Ahah\Ajax 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 ahah\ajax options to change behavior of the form element.
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.
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);
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.
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.
Links
• http://api.drupal.org• http://api.drupal.org/api/drupal/develope
r--topics--forms_api_reference.html/• http://drupal.org/node/751826