oleksandr masovets. forms in drupal. drupal camp kyiv 2011

14
Forms in Drupal Oleksandr Masovets E-mail: [email protected] Skype: krasnyjdirex

Upload: vlad-savitsky

Post on 19-Jun-2015

1.565 views

Category:

Technology


5 download

DESCRIPTION

Доклад познакомит начинающих друпалеров с идеологией, архитектурой и основами работы с формами в Drupal. элементы форм валидация форм действия (CallBack) форм ahah действия (CallBack) форм рендер форм различия между Form API в Drupal 6 и 7 использование форм в собственных модулях tips and tricks

TRANSCRIPT

Page 1: Oleksandr Masovets. Forms in Drupal. Drupal Camp Kyiv 2011

Forms in Drupal

Oleksandr MasovetsE-mail: [email protected]

Skype: krasnyjdirex

Page 2: Oleksandr Masovets. Forms in Drupal. Drupal Camp Kyiv 2011

Gold Sponsor ofDrupalCamp Kyiv 2011

Page 3: Oleksandr Masovets. Forms in Drupal. Drupal Camp Kyiv 2011

Silver Sponsors ofDrupalCamp Kyiv 2011

Page 4: Oleksandr Masovets. Forms in Drupal. Drupal Camp Kyiv 2011

Working with forms

• Form– Render– System form

• Form alter• Form validation• Form submit• Destination (redirect)

Page 5: Oleksandr Masovets. Forms in Drupal. Drupal Camp Kyiv 2011

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.

Page 6: Oleksandr Masovets. Forms in Drupal. Drupal Camp Kyiv 2011

Form elements

Page 7: Oleksandr Masovets. Forms in Drupal. Drupal Camp Kyiv 2011

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()

Page 8: Oleksandr Masovets. Forms in Drupal. Drupal Camp Kyiv 2011

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.

Page 9: Oleksandr Masovets. Forms in Drupal. Drupal Camp Kyiv 2011

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.

Page 10: Oleksandr Masovets. Forms in Drupal. Drupal Camp Kyiv 2011

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);

Page 11: Oleksandr Masovets. Forms in Drupal. Drupal Camp Kyiv 2011

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.

Page 12: Oleksandr Masovets. Forms in Drupal. Drupal Camp Kyiv 2011

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.

Page 13: Oleksandr Masovets. Forms in Drupal. Drupal Camp Kyiv 2011

Links

• http://api.drupal.org• http://api.drupal.org/api/drupal/develope

r--topics--forms_api_reference.html/• http://drupal.org/node/751826

Page 14: Oleksandr Masovets. Forms in Drupal. Drupal Camp Kyiv 2011

Forms in Drupal

Oleksandr MasovetsE-mail: [email protected]

Skype: krasnyjdirex