Change form data after submit in drupal

梦想与她 提交于 2019-12-08 04:07:24

问题


in some content type form I added a checkbox. When this checkbox is checked I want to remove some of the submitted data.

To do so I created a custom module (my_module.module):

function my_module_form_alter(&$form, &$form_state) {
    // ...
    $form['#submit'][] = 'my_module_form_alter_submit';
}

function my_module_form_alter_submit($form_id, $form_values) {
    drupal_set_message(t('Submit Function Executed!'));
}

How can I tell this module to refer only to the form of a certain containt type? And how can I remove data when it is submitted?


回答1:


Assuming you are altering a node edit form, you can either conditionally add the submit callback such as (in your hook_form_alter):

if(isset($form['#node']) && $form['type']['#value'] == 'page') {
    $form['#submit'][] = 'my_module_form_alter_submit';
}

or, you could check the $form argument in the submit callback in a similar fashion.

You are missing the third argument to the hook_form_alter which should be $form_id, and your submit callback should take the arguments such as:

function my_module_form_alter_submit($form, &$form_state) { ... }

See also:

http://api.drupal.org/api/drupal/developer%21hooks%21core.php/function/hook_form_alter/6

http://api.drupal.org/api/drupal/developer%21topics%21forms_api.html/6




回答2:


To remove data after the submit, in your form_alter function, just use unset() on the form field. unset($form['my-field']);



来源:https://stackoverflow.com/questions/9235164/change-form-data-after-submit-in-drupal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!