$ path = drupal_get_path ( 'module' , 'simple_ajax_popup' ) ;
drupal_add_js ( $ path . '/misc/fancybox/source/jquery.fancybox.pack.js' ) ;
drupal_add_css ( $ path . '/misc/fancybox/source/jquery.fancybox.css' ) ;
drupal_add_js ( $ path . '/misc/simple_ajax_popup.js' ) ;
drupal_add_js ( 'misc / jquery.form.js' ) ;
/ **
* Implements hook_menu ().
* /
function simple_ajax_popup_menu ( ) {
$ items = array ( ) ;
$ items [ 'test' ] = array (
'type' => MENU_SUGGESTED_ITEM ,
'title' => t ( 'Test page' ) ,
'page callback' => 'simple_ajax_popup_page' ,
'access arguments' => array ( 'access content' ) ,
) ;
$ items [ 'callback' ] = array (
'type' => MENU_CALLBACK ,
'page callback' => 'simple_ajax_popup' ,
'access arguments' => array ( 'access content' ) ,
) ;
return $ items ;
}
function simple_ajax_popup_page ( ) {
drupal_add_library ( 'system' , 'drupal.ajax' ) ;
return l (
'AJAX form' ,
'callback' ,
array (
'attributes' => array ( 'class' => array ( 'fancybox' , 'fancybox.ajax' ) ) ,
)
) ;
}
function simpleAjaxPopupFormProcess ( ) {
Drupal. attachBehaviors ( '#simple_ajax_popup_form' ) ;
jQuery ( '#simple_ajax_popup_form' ) . ajaxForm ( ) ;
}
/ **
* Return popup with form.
* /
function simple_ajax_popup ( ) {
$ form = drupal_render ( drupal_get_form ( 'simple_ajax_popup_form' ) ) ;
print $ form ;
drupal_exit ( ) ;
}
function simple_ajax_popup_form_submit ( $ form , & $ form_state ) {
$ form_state [ 'rebuild' ] = TRUE ;
watchdog ( 'ajax message' , $ _POST [ 'message' ] ) ;
$ commands = array ( ) ;
$ commands [ ] = ajax_command_replace (
'# simple-ajax-popup-form' ,
'<div class = "messages status"> The message has been added to the system log. </ div>'
) ;
ajax_deliver ( array ( '#type' => 'ajax' , '#commands' => $ commands ) ) ;
drupal_exit ( ) ;
}
$ form [ 'form_wrapper' ] [ 'submit' ] = array (
'#type' => 'submit' ,
'#value' => 'Submit.' ,
'#attributes' => array ( 'class' => array ( 'use-ajax-submit' ) ) ,
) ;
Source: https://habr.com/ru/post/147262/
All Articles