リンク付きのページを作成しようとしています (「クリックしてください」)。
クリックすると、別のページからフォームが取得され、現在のページに追加されます。
フォームを正しく取得していますが、フォームにjavascript/ajax
関連するものが失われているようです。
submit
フォームのあるページに、このフォームのカスタム Ajaxコールバックがあります (送信ボタンには class がありますajax-processed
)。正常に動作していますが、Ajax でフェッチすると、送信ボタンに class がありませんajax-processed
。
PHP コード:
<?php
function custom_preprocess_page(&$vars){
drupal_add_library('system', 'drupal.ajax');
drupal_add_library('system', 'drupal.form');
drupal_add_js(drupal_get_path('module','custom').'/custom.js', 'file');
}
/**
* Implements hook_menu
* @return string
*/
function custom_menu() {
$items['my-test/my_form'] = array(
'title' => 'Ajax test',
'description' => 'Page containing form',
'page callback' => '_custom_retrieve_webform',
'access callback' => TRUE,
);
$items['my-test/retrieve_form'] = array(
'title' => 'Ajax test',
'description' => 'Page where form should be loaded by ajax',
'page callback' => '_custom_retrieve_html',
'access callback' => TRUE,
);
return $items;
}
function _custom_retrieve_html(){
return '<div id="justadiv"><a class="clickme">'. t('click me'). '</a></div>';
}
function _custom_retrieve_webform(){
$node = node_load(12);
$submission = (object) array();
$enabled = TRUE;
$preview = FALSE;
$form= drupal_get_form('webform_client_form_12', $node, $submission, $enabled, $preview);
return '<div id="myform">'.drupal_render($form).'<div>';
}
function custom_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'webform_client_form_12') {
$nid = $form['#node']->nid;
$form['submitted']['gender']['#default_value'] = 'M';
$form['actions']['submit']['#ajax'] = array(
'callback' => 'mymodule_webform_js_submit',
'wrapper' => 'webform-client-form-' . $nid,
'method' => 'replace',
'effect' => 'fade',
);
//$form['#theme']=array('custom_web_form');
array_unshift($form['#theme'], 'custom_web_form');
}
}
function mymodule_webform_js_submit($form, $form_state) {
// define the $sid variable (submission id from webform)
$sid = $form_state['values']['details']['sid'];
// if we have a sid then we know the form was properly submitted, otherwise, we'll just return the existing $form array
if ($sid) {
dsm($sid);
$confirmation = array(
'#type' => 'markup',
'#markup' => 'sucess',
);
// return the confirmation message
return $confirmation;
}
else {
// return the form
return $form;
}
}
JavaScript コード:
(function($){
Drupal.behaviors.my_custom= {
attach:function(context,settings){
$('a.clickme').click(function(e){
$('#justadiv').load('/d7/my-test/ajax_loaded #webform-client-form-12', function(response, status, xhr) {
Drupal.attachBehaviors('#webform-client-form-12');
});
Drupal.attachBehaviors($('#webform-client-form-12')[0]);
});
}
}
})(jQuery)