欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

修改表单的 form_submit form alter submit 修改 submit 对原来的submit 进行一些处理 有大用 有大大用 有大大大用

shiping1 的头像

How do I alter the form submission handler?


For some reason I need to replace some characters (if exist) in every single textfields in any form just right before they stored in database. In other words I need a hook in which I could get all submitted data and replace their characters.
I found this solution:

function hook_form_alter($form, $form_state, $form_id) {
  $form['#submit'] = 'my_custom_submission_function';
}


But this isn't a good idea since I have to take care of the rest of the submission progress too. I just need to replace some characters. Is there any hook to do this? What are the solutions?

正确答案如下

$form['#submit'] stores an array of form submit handlers. With the code you provided you're overwriting it by adding only your own. To get the result you want you should add your submit handler as the first element in the array like this:

用它 $form['#submit'] = array('shipingzhongcustomfive_custom_user_login_submit'); //这个就是覆盖以前的 submit 




//最好不用它吧 因为可能有问题  array_unshift($form['#submit'], 'mymodule_custom_submission');因为它会执行以前的submit


array_unshift($form['#submit'], 'mymodule_custom_submission'); 
//在原来的submit的前面 再加个 submit 就可以添加功能了,这个就是放在原来的submit之前执行

$form['#submit'][] = 'mymodule_custom_submission'  //这个就是放在原来的submit之后执行


用它 $form['#submit'] = array('shipingzhongcustomfive_custom_user_login_submit');
//最好不用它吧 因为可能有问题  array_unshift($form['#submit'], 'mymodule_custom_submission');

function mymodule_custom_submission($form, &$form_state) {
  global $user;
  if ($user->uid) {
    $form_state['redirect'] = 'user/'. $user->uid;
    return;
  }
}


Getting the right values in the right forms and replacing some characters is probably a bigger issue in itself. You may want to narrow down the forms/fields you need to change to make sure you don't break anything.
 


Perhaps it would be better to perform these modifications during validation instead of submission as suggested by Drupal (see form_set_value function athttps://api.drupal.org/api/drupal/includes!form.inc/function/form_set_value/7).

So in your hook_form_alter you could even add it to the end of existing validation functions if you like:

$form['#validate'][] = 'mymodule_custom_validation';

来自 http://drupal.stackexchange.com/questions/18641/how-do-i-alter-the-form-submission-handlerfunction my_module_form_alter(&$form, $form_state, $form_id) {  switch ($form_id) {    case 'my_form_id':    $form['#submit'] = array('my_module_node_form_submit_handler');         break;  }}


function my_module_node_form_submit_handler($form, &$form_state) {

//HOW CAN I PASS IN HIDDEN VARIABLES TO THE REDIRECT PAGE HERE THAT
//TAKE THE VALUE OF THIS FORM AND SOME ADDITIONAL PARAMTERS?
  $form_state['redirect']  = 'https://payflowlink.paypal.com';
}
来自 https://www.drupal.org/node/290462

function nameOfMyModule_form_alter(&$form, $form_state, $form_id)
{
   if ($form_id == 'taxonomy_form_term')
   {
   //Modify the Taxonomy form somehow
    $form['identification']['new_field_name'] = array(
      '#type' => 'textfield',
      '#title' => t('New Field'),
      '#default_value' => queryable_variables_get('new_field_name', 'tid', $form['#term']['tid']),  //QV's is not a built in drupal function.  Replace it with whatever you use to fill in the value.
      '#size' => 120,
      //'#maxlength' => 64,
      '#description' => t('Describe me u wierdo!'),
      );
      $form['#validate'][] = 'nameOfMyModule_functionName_validate'; //Be sure not to use 'form' for the 'functionName' part as it will confuse drupal.  Use a descriptive name.
      $form['#submit'][] = 'nameOfMyModule_functionName_submit';  //Be sure not to use 'form' for the 'functionName' part as it will confuse drupal.  Use a descriptive name.
   }
}function nameOfMyModule_functionName_validate($form, &$form_state){
   //Validation stuff here, set $error to true if something went wrong, or however u want to do this.  Completely up to u in how u set errors.
   if ($error)
   {
      form_set_error('new_field_name', 'AHH SOMETHING WRONG!');
   }
}

function nameOfMyModule_functionName_submit($form, &$form_state)
{
    //Save the new value.
    queryable_variables_set('new_field_name', $form_state['values']['new_field_name'], 'tid', $form['#term']['tid']);  // I used taxonomy as an example.  How you save data is completely up to u and this is nothing more than a simple example and shouldnt be used as a test.}?>// "customcontact" is name of module. // "form_alter" is part of drupal and needs to be put therefunction customcontact_form_alter(&$form, $form_state, $form_id) {


// only alter user contact forms. (see note 1)
if ($form_id == 'contact_mail_user') {

// add a field to the form (see note 2)
$form['fname'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
'#weight' => -99,
);

//validate field
$form['#validate'][] = 'customcontact_validate';

// submit field
// use array_unshift to submit this field before submitting the form.
//最好不用它吧 因为可能有问题

$form['#submit'] = array('customcontact_submit');

array_unshift($form['#submit'], 'customcontact_submit');
}
}

function customcontact_validate($form, &$form_state) {
if ($error) {
form_set_error('fname', 'AHH SOMETHING WRONG!');
}
}
function customcontact_submit($form, &$form_state) {
variable_set('fname', $form_state['values']['fname']);
}

// add the field to the email
function customcontact_mail_alter(&$message) {

// a variable which can be called anything gets the fname field
$field_fname = variable_get('fname', '');

// only alter user contact emails
if ($message['id'] == 'contact_user_mail') {

// change the email subject
$message['subject'] = 'Subject heading';

// add the extra field to the end of the email message
$message['body'][] = $field_fname;

}
}

// create the block
function customcontact_block($op = 'list', $delta = 0, $edit = array()) {

  if ($op == 'list') {
    // Generate listing of blocks from this module, for the admin/block page
    $block = array();
    $block[0]['info'] = t('Company contact form');
  }
  else if ($op == 'view') {
    if ($node = menu_get_object()) {
      // get id of creator of page
      $account = user_load($node->uid);
      // get contact page
      module_load_include('inc', 'contact', 'contact.pages');

      // the heading for the block when displayed on the node
      $block['subject'] = t('Contact %name array('%name' => $account->name);
      // put everything into the block content area
      $block['content'] = drupal_get_form('contact_mail_user', $account);
    }
  }
  return $block;
}



 

来自   https://api.drupal.org/api/drupal/developer!hooks!core.php/function/hook_form_alter/6

普通分类: