form api в drupal 7
TRANSCRIPT
![Page 2: Form api в drupal 7](https://reader030.vdocuments.site/reader030/viewer/2022012405/559c1ca21a28ab14158b4665/html5/thumbnails/2.jpg)
План доклада
1. Что такое Form API, в чем преимущества использования.
2. Процесс построения и обработки формы.
3. Обзор основных свойств и элементов форм.
4. Создание собственного элемента.5. Создание динамических форм.
Form API в Drupal 7
![Page 3: Form api в drupal 7](https://reader030.vdocuments.site/reader030/viewer/2022012405/559c1ca21a28ab14158b4665/html5/thumbnails/3.jpg)
1. Что дает нам FORM API.
Формы создаются в виде структурированных массивов.
Любую форму можно изменять. К любой форме можно добавлять
функции валидации и обработки. Предоставляется встроенная
защита от ряда уязвимостей. Для элементов генерируется
стандартный HTML.
Form API в Drupal 7
![Page 4: Form api в drupal 7](https://reader030.vdocuments.site/reader030/viewer/2022012405/559c1ca21a28ab14158b4665/html5/thumbnails/4.jpg)
2. Процесс построения и обработки формы.
![Page 5: Form api в drupal 7](https://reader030.vdocuments.site/reader030/viewer/2022012405/559c1ca21a28ab14158b4665/html5/thumbnails/5.jpg)
Пример формы
Form API в Drupal 7
![Page 6: Form api в drupal 7](https://reader030.vdocuments.site/reader030/viewer/2022012405/559c1ca21a28ab14158b4665/html5/thumbnails/6.jpg)
function example_nameform($form, &$form_state){
$form['user_name'] = array(
'#title' => t('Your Name'),
'#type' => 'textfield',
'#description' => t('Enter your name.'),
);
$form['submit'] = array( '#type' => 'submit', '#value' => t('Submit') ); return $form;}
Пример формы
Form API в Drupal 7
![Page 7: Form api в drupal 7](https://reader030.vdocuments.site/reader030/viewer/2022012405/559c1ca21a28ab14158b4665/html5/thumbnails/7.jpg)
Функция валидации (validate)
function example_nameform_validate($form, &$form_state){
if (!check_user_name($form_state['values']['user_name'])) { form_set_error('user_name', t('Invalid user name.')); }
}
Form API в Drupal 7
![Page 8: Form api в drupal 7](https://reader030.vdocuments.site/reader030/viewer/2022012405/559c1ca21a28ab14158b4665/html5/thumbnails/8.jpg)
Функция отправки (submit)
function example_nameform_submit($form, &$form_state){
$name = $form_state['values']['user_name']; example_nameform_save($name, $color);
}
Form API в Drupal 7
![Page 9: Form api в drupal 7](https://reader030.vdocuments.site/reader030/viewer/2022012405/559c1ca21a28ab14158b4665/html5/thumbnails/9.jpg)
Обработка форм в Drupal
Инициализация drupal_get_form()
Сбор всех элементов формы
Поиск #validate #submit
Изменения модулями (alter)
Построение формы form_builder()
Выполнение #after_build
Form API в Drupal 7
![Page 10: Form api в drupal 7](https://reader030.vdocuments.site/reader030/viewer/2022012405/559c1ca21a28ab14158b4665/html5/thumbnails/10.jpg)
Обработка форм в Drupal
Отправлена ли форма?
drupal_render() Выполнить #validate
Вернуть html Выполнить #submit
Перенаправить браузер
ДаНет
Form API в Drupal 7
![Page 11: Form api в drupal 7](https://reader030.vdocuments.site/reader030/viewer/2022012405/559c1ca21a28ab14158b4665/html5/thumbnails/11.jpg)
Изменение существующих форм
hook_form_FORM_ID_alterfunction example_form_user_login_block_alter(&$form, &$form_state $form_id){ $form['actions']['submit']['#value'] = t('Sign in');}
hook_form_alterfunction example_form_alter(&$form, &$form_state, $form_id){ if ($form_id == 'user_login_block'){ $form['actions']['submit']['#value'] = t('Sign in'); }}
Form API в Drupal 7
![Page 12: Form api в drupal 7](https://reader030.vdocuments.site/reader030/viewer/2022012405/559c1ca21a28ab14158b4665/html5/thumbnails/12.jpg)
3. Обзор основных свойств и элементов форм
Form API в Drupal 7
![Page 13: Form api в drupal 7](https://reader030.vdocuments.site/reader030/viewer/2022012405/559c1ca21a28ab14158b4665/html5/thumbnails/13.jpg)
Свойства формы и элементов
Свойства корневогоэлемента формы
#action#method#redirect
Свойствадобавляемые ко всемэлементам
#description#attributes#required#tree#parents
Form API в Drupal 7
![Page 14: Form api в drupal 7](https://reader030.vdocuments.site/reader030/viewer/2022012405/559c1ca21a28ab14158b4665/html5/thumbnails/14.jpg)
Свойства, доступные во всех элементах
#type #access #after_build #array_parents #attached #default_value #disabled #elements_validate #parents #post_render
#prefix #pre_render #process #states #suffix #theme #theme_wrappers #title #weight
Form API в Drupal 7
![Page 15: Form api в drupal 7](https://reader030.vdocuments.site/reader030/viewer/2022012405/559c1ca21a28ab14158b4665/html5/thumbnails/15.jpg)
Стандартные элементы формы
Form API в Drupal 7
![Page 16: Form api в drupal 7](https://reader030.vdocuments.site/reader030/viewer/2022012405/559c1ca21a28ab14158b4665/html5/thumbnails/16.jpg)
Стандартные элементы формы
Form API в Drupal 7
![Page 17: Form api в drupal 7](https://reader030.vdocuments.site/reader030/viewer/2022012405/559c1ca21a28ab14158b4665/html5/thumbnails/17.jpg)
4. Создание собственного элемента формы
$form['phone_number'] = array( '#type' => 'example_phone_element', '#title' => t('Discrete phone number'), '#default_value' => array('areacode' => '044',
'extension' => '9999999'), '#description' => t('A phone number'),);
Form API в Drupal 7
![Page 18: Form api в drupal 7](https://reader030.vdocuments.site/reader030/viewer/2022012405/559c1ca21a28ab14158b4665/html5/thumbnails/18.jpg)
Создание собственного элемента формы HOOK_element_info
function example_element_info(){ $types['example_phone'] = array(
'#input' => TRUE, '#process' => array('example_phone__process'), '#element_validate' => array('example_phone_validate'), '#theme_wrappers' => array('example_inline_element'), ); return $types; }
Form API в Drupal 7
![Page 19: Form api в drupal 7](https://reader030.vdocuments.site/reader030/viewer/2022012405/559c1ca21a28ab14158b4665/html5/thumbnails/19.jpg)
Возможные значения элемента по умолчанию:
#input #process #after_build #validate #element_validate
#pre_render #post_render #submit #title_display
Form API в Drupal 7
![Page 20: Form api в drupal 7](https://reader030.vdocuments.site/reader030/viewer/2022012405/559c1ca21a28ab14158b4665/html5/thumbnails/20.jpg)
#process функция элемента
function example_phone_process($element, &$form_state, $form){
$element['#tree'] = TRUE;
$element['areacode'] = array( '#type' => 'textfield', '#size' => 3, '#maxlength' => 3, '#value' => $element['#value']['areacode'], '#required' => TRUE, '#prefix' => '(', '#suffix' => ‘)', );
Form API в Drupal 7
![Page 21: Form api в drupal 7](https://reader030.vdocuments.site/reader030/viewer/2022012405/559c1ca21a28ab14158b4665/html5/thumbnails/21.jpg)
#process функция элемента
$element['extension'] = array( '#type' => 'textfield', '#size' => 7, '#maxlength' => 7, '#value' => $element['#value']['extension'], ); return $element;}
Form API в Drupal 7
![Page 22: Form api в drupal 7](https://reader030.vdocuments.site/reader030/viewer/2022012405/559c1ca21a28ab14158b4665/html5/thumbnails/22.jpg)
Валидация элемента.
function example_phone_validate($element, &$form_state) {
if (!areacode_valid($element['#value']['areacode'])) { form_error($element['areacode'], t('The area code is
invalid.')); } if (!areacode_valid($element['#value']['extension'])) form_error($element['extension'], t('The extension is
invalid.')); }
}
Form API в Drupal 7
![Page 23: Form api в drupal 7](https://reader030.vdocuments.site/reader030/viewer/2022012405/559c1ca21a28ab14158b4665/html5/thumbnails/23.jpg)
5. Создание динамических форм.
Form API в Drupal 7
![Page 24: Form api в drupal 7](https://reader030.vdocuments.site/reader030/viewer/2022012405/559c1ca21a28ab14158b4665/html5/thumbnails/24.jpg)
Свойство #states
![Page 25: Form api в drupal 7](https://reader030.vdocuments.site/reader030/viewer/2022012405/559c1ca21a28ab14158b4665/html5/thumbnails/25.jpg)
Свойство #states
![Page 26: Form api в drupal 7](https://reader030.vdocuments.site/reader030/viewer/2022012405/559c1ca21a28ab14158b4665/html5/thumbnails/26.jpg)
Свойство #statesarray( 'visible' => CONDITIONS, 'checked' => OTHER_CONDITIONS,)
array( 'visible' => array( JQUERY_SELECTOR => REMOTE_CONDITIONS, JQUERY_SELECTOR => REMOTE_CONDITIONS, ... ),)
'#states' => array( 'visible' => array( ':input[name="foo"]' => array('checked' => TRUE), ), ), Form API в Drupal 7
![Page 27: Form api в drupal 7](https://reader030.vdocuments.site/reader030/viewer/2022012405/559c1ca21a28ab14158b4665/html5/thumbnails/27.jpg)
Свойство #states
Состояния элемента формы
disabled required optional visible invisible checked unchecked expanded collapsed
Значения для условий
empty filled checked unchecked expanded collapsed value
Form API в Drupal 7
![Page 28: Form api в drupal 7](https://reader030.vdocuments.site/reader030/viewer/2022012405/559c1ca21a28ab14158b4665/html5/thumbnails/28.jpg)
Создание динамических форм
Form API в Drupal 7
![Page 29: Form api в drupal 7](https://reader030.vdocuments.site/reader030/viewer/2022012405/559c1ca21a28ab14158b4665/html5/thumbnails/29.jpg)
Пример Ajax формы
Form API в Drupal 7
![Page 30: Form api в drupal 7](https://reader030.vdocuments.site/reader030/viewer/2022012405/559c1ca21a28ab14158b4665/html5/thumbnails/30.jpg)
Установка ajax параметров $form['changethis'] = array( // other select options '#ajax' => array( 'callback' => 'ajax_example_callback', 'wrapper' => 'replace_textfield_div', ), ); $form['replace_textfield'] = array( // other textfield options '#prefix' => '<div id="replace_textfield_div">', '#suffix' => '</div>', ); if (!empty($form_state['values']['changethis'])) { $form['replace_textfield']['#description'] = t( "Say why you chose '@value'", array('@value' =>$form_state['values']['changethis']) ); } Form API в Drupal 7
![Page 31: Form api в drupal 7](https://reader030.vdocuments.site/reader030/viewer/2022012405/559c1ca21a28ab14158b4665/html5/thumbnails/31.jpg)
Ajax callback
function ajax_example_callback($form, $form_state) {
return $form['replace_textfield'];
}
Form API в Drupal 7
![Page 32: Form api в drupal 7](https://reader030.vdocuments.site/reader030/viewer/2022012405/559c1ca21a28ab14158b4665/html5/thumbnails/32.jpg)
Этапы обработки ajax
Произошло событие#ajax['event']
ajax запрос по пути #ajax['path'] либо
‘system/ajax’
Обработка ответа
Html заполняет #ajax['wrapper'].
Массив ajax команд выполняется
![Page 33: Form api в drupal 7](https://reader030.vdocuments.site/reader030/viewer/2022012405/559c1ca21a28ab14158b4665/html5/thumbnails/33.jpg)
Параметры #ajax
#ajax['callback'] #ajax['effect'] #ajax['event'] #ajax['keypress'] #ajax['method'] #ajax['path'] #ajax['prevent'] #ajax['progress'] #ajax['trigger_as'] #ajax['wrapper']
Form API в Drupal 7
![Page 34: Form api в drupal 7](https://reader030.vdocuments.site/reader030/viewer/2022012405/559c1ca21a28ab14158b4665/html5/thumbnails/34.jpg)
Ajax commands
function ajax_example_callback($form, $form_state) {
$commands = array(); $commands[] = ajax_command_alert("Alert requested);
return array( '#type' => 'ajax', '#commands' => $commands );
}
Form API в Drupal 7
![Page 35: Form api в drupal 7](https://reader030.vdocuments.site/reader030/viewer/2022012405/559c1ca21a28ab14158b4665/html5/thumbnails/35.jpg)
Информация по темеhttp://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html
http://drupal.org/project/examples
http://drupalace.ru/lesson/sozdanie-svoego-elementa-formy-v-drupal-7-rasshiryaem-forms-api
http://api.drupal.org/api/drupal/includes%21ajax.inc/group/ajax/7
http://api.drupal.org/api/drupal/includes%21ajax.inc/group/ajax_commands/7
http://api.drupal.org/api/drupal/includes%21common.inc/function/drupal_process_states/7
http://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_element_info/7
Form API в Drupal 7