欢迎各位兄弟 发布技术文章
这里的技术是共享的
I'd like to offer to implement ability to call custom beforeSubmit handlers.
Hack:
form.inc - form_expand_ahah()
'button' => isset($element['#executes_submit_callback']) ? array($element['#name'] => $element['#value']) : FALSE,
//new - begin
//'before' => empty($element['#ahah']['before_submit']) ? array() : (is_scalar($element['#ahah']['before_submit']) ? array($element['#ahah']['before_submit']) : $element['#ahah']['before_submit']),
'before' => empty($element['#ahah']['before_submit']) ? array() : $element['#ahah']['before_submit'], // for better extensibility
//new - end
);
ahah.js - "Drupal.ahah = function(base, element_settings) {...}"
this.button = element_settings.button || { };
//new - begin
this.before = element_settings.before;
//new - end
ahah.js - "Drupal.ahah.prototype.beforeSubmit = function (form_values, element, options) {...}"
Drupal.ahah.prototype.beforeSubmit = function (form_values, element, options) {
//new - begin
for( var i in this.before )
if( eval(this.before[i] + '(form_values, element, options)') == false )
return false;
//new - end
Use example:
A form builder function
//...
'delete' => array(
'#type' => 'submit',
'#value' => t('Delete'),
'#ahah' => array(
'path' => '.../delete/'.$id,
'wrapper' => 'content',
'method' => 'replace',
'effect' => 'fade',
'before_submit' => array('our_custom_delete_before_submit_handler'),
),
),
//...
our JS function our_custom_delete_before_submit_handler()
function our_custom_delete_before_submit_handler(form_values, element, options)
{
return confirm('The whole content will be unrecoverably deleted! Are you sure?');
}
Comments
Comment#1
liquixis CreditAttribution: liquixis commentedUpdate:
Hackform.inc - form_expand_ahah()
ahah.js - "Drupal.ahah = function(base, element_settings) {...}"
ahah.js - new function - before "Drupal.ahah.prototype.beforeSubmit = function (form_values, element, options) {...}"
ahah.js - "Drupal.ahah.prototype.beforeSubmit = function (form_values, element, options) {...}"
ahah.js - "Drupal.ahah.prototype.success = function (response, status) {...}"
DocumentationThere are four types of callbacks:
before_submit - Executes before AHAH submit (if callback returns FALSE then the operation interrupt: next callbacks and AHAH submit are not executed )
after_submit - Executes immediately after AHAH submit
before_success - Executes immediately after successful AHAH submit and before it's default Drupal processing
after_success - Executes after successful AHAH submit and after it's default Drupal processing
Each callback has next arguments:
"args" (object) - AHAH data.
- in case of "before_submit" and "after_submit" this will be (in JS): {'form_values' : form_values, 'element' : element, 'options' : options}
- in case of "before_success" and "after_success" this will be (in JS): {'response' : response, 'status' : status}
"params" (object) - params defined in callback definition (
#ahah['<callback_type>'][<callback_index>]['params']
)In all callbacks you can set (override) "args" object items values to your values.
ExamplesThis will be useful in case of overriding successful AHAH submit result in "before_success" callback, e.g.:
function mymodule_submit_button_before_success(args, params) { args['response']['data'] = '<div class="my-content-wrapper">' + args['response']['data'] + '</div>'; }
Comment#2
neochief CreditAttribution: neochief commentedYou'd better make patch for 7.x for this, while there's a time left.
Comment#3
tostinni CreditAttribution: tostinni commentedIs anyone working on this currently ?
Thanks
Comment#4
dpearcefl CreditAttribution: dpearcefl commentedDoesn't look like it. No interest shown.
Comment#5
a.d2000 CreditAttribution: a.d2000 commentedI m in. This post is really helpful for me. I have CKEditor integrated in my custom form. I am using AHAH to submit my form but in AHAH submit i m not getting value of editor in $_POST variable.
So i am thinking to disable editor before ahah call & then re-enable after completion.
I tried above code for before_submit and it worked for me.
But still i think it is not better idea to hack in core
Thanks for that.