Михаил Крайнюк. form api: ajax-commands
DESCRIPTION
DrupalCafe#9@Novosibirsk https://vk.com/drupalcafe9 В данном докладе описываются методы использования AJAX-инструментов, встроенных в Drupal, при работе с формами. Form API позволяет не только строить формы, но и добавлять AJAX обработку для них, без написания JS-кода (используя только язык PHP). Получаемые обработчики надёжны, безопасны и просты в использовании. А результат, предоставляемый ими, поражает воображение. ----- Сайт сибирского сообщества друпаллеров ДрупалСиб drupalsib.ru Группа сибирского сообщества друпаллеров Вконтакте vk.com/drupalsib Партнер Группа компаний И20 i20.bizTRANSCRIPT
![Page 1: Михаил Крайнюк. Form API: AJAX-commands](https://reader030.vdocument.in/reader030/viewer/2022020110/5594abe01a28ab8d0e8b461c/html5/thumbnails/1.jpg)
Form API
AJAX-commands
![Page 2: Михаил Крайнюк. Form API: AJAX-commands](https://reader030.vdocument.in/reader030/viewer/2022020110/5594abe01a28ab8d0e8b461c/html5/thumbnails/2.jpg)
OK
Form API
![Page 3: Михаил Крайнюк. Form API: AJAX-commands](https://reader030.vdocument.in/reader030/viewer/2022020110/5594abe01a28ab8d0e8b461c/html5/thumbnails/3.jpg)
OK
OK
AJAX
AJAX
![Page 4: Михаил Крайнюк. Form API: AJAX-commands](https://reader030.vdocument.in/reader030/viewer/2022020110/5594abe01a28ab8d0e8b461c/html5/thumbnails/4.jpg)
- Toyota
- Renault
- Porsche
- Выберите -
Добро пожаловать.
- PorscheПоздравляем!
Вам в подарок:
Porsche 911 Carrera
![Page 5: Михаил Крайнюк. Form API: AJAX-commands](https://reader030.vdocument.in/reader030/viewer/2022020110/5594abe01a28ab8d0e8b461c/html5/thumbnails/5.jpg)
$form['offer_type'] = array(
'#type' => ‘select',
'#title' => t('Select please'),
'#options' => array( ... ),
'#ajax' => array(
'callback' => 'name_of_function',
'wrapper' => 'html-id-of-wrapper',
),
);
Form Builder
![Page 6: Михаил Крайнюк. Form API: AJAX-commands](https://reader030.vdocument.in/reader030/viewer/2022020110/5594abe01a28ab8d0e8b461c/html5/thumbnails/6.jpg)
1. Form builder
2. Form validate
3. Form submit
+
4. AJAX-callback
custom_form_validate
custom_form
custom_form_submit
name_of_function
![Page 7: Михаил Крайнюк. Form API: AJAX-commands](https://reader030.vdocument.in/reader030/viewer/2022020110/5594abe01a28ab8d0e8b461c/html5/thumbnails/7.jpg)
function name_of_function($form, $form_state) {
$html = ‘<div>’ . t(‘Hello!’) . ‘</div>’;
return $html;
}
1. HTML
Hello!
![Page 8: Михаил Крайнюк. Form API: AJAX-commands](https://reader030.vdocument.in/reader030/viewer/2022020110/5594abe01a28ab8d0e8b461c/html5/thumbnails/8.jpg)
function name_of_function($form, $form_state) {
return $form;
}
2. Renderable array
OK
![Page 9: Михаил Крайнюк. Form API: AJAX-commands](https://reader030.vdocument.in/reader030/viewer/2022020110/5594abe01a28ab8d0e8b461c/html5/thumbnails/9.jpg)
function name_of_function($form, $form_state) {
$cmd = array();
$cmd[] = ajax_command_replace(NULL, $output);
$cmd[] = ajax_command_prepend(NULL, theme('status_messages'));
return array(
'#type' => 'ajax',
'#commands' => $cmd,
);
}
3. Commands
Hello!
Готово!
![Page 10: Михаил Крайнюк. Form API: AJAX-commands](https://reader030.vdocument.in/reader030/viewer/2022020110/5594abe01a28ab8d0e8b461c/html5/thumbnails/10.jpg)
AJAX-commandsajax_command_before
ajax_command_changed
ajax_command_css
ajax_command_dataajax_command_html
ajax_command_insert
ajax_command_invoke
ajax_command_prepend
ajax_command_remove
ajax_command_replace
ajax_command_restripe
ajax_command_settings
ajax_command_after
ajax_command_alert
ajax_command_append
![Page 11: Михаил Крайнюк. Form API: AJAX-commands](https://reader030.vdocument.in/reader030/viewer/2022020110/5594abe01a28ab8d0e8b461c/html5/thumbnails/11.jpg)
Нужно больше?
![Page 12: Михаил Крайнюк. Form API: AJAX-commands](https://reader030.vdocument.in/reader030/viewer/2022020110/5594abe01a28ab8d0e8b461c/html5/thumbnails/12.jpg)
Собственные AJAX-commands
(function($) {
Drupal.ajax.prototype.commands.my_func = function(ajax, response, status) {
document.title = response.title;
}
} (jQuery));
$cmd = array();
$cmd[] = array(‘command‘ => ‘my_func', ‘title‘ => $page_title . ‘!!!‘);
$cmd[] = ajax_command_replace(NULL, $output);
return array(
'#type' => 'ajax',
'#commands' => $cmd,
);
![Page 13: Михаил Крайнюк. Form API: AJAX-commands](https://reader030.vdocument.in/reader030/viewer/2022020110/5594abe01a28ab8d0e8b461c/html5/thumbnails/13.jpg)
23 / 05 / 2014