如何为drupal表单添加按钮?

sah*_*mad 0 forms drupal button

我想为drupal创建表单添加一个自定义按钮,所以如果用户单击它而不是提交按钮,创建的对象的工作流状态将更改为另一个状态(不是默认的第一个状态)任何建议?

Nit*_*dra 7

要修改drupal生成的默认表单,您必须向模块添加一个form_alter挂钩.你可以通过定义一个modulename_form_alter假定模块名称的函数来实现modulename.drupal系统传递了form数组和form_state数组,您可以使用它来覆盖默认行为.在您的情况下,完整的功能看起来像这样.

function modulename_form_alter(&$form, $form_state, $form_id) {
    if($form_id == 'what you want') {
        $form['buttons']['another_button'] = array(
            '#type'   => 'submit',
            '#value'  => 'Add to another state',
            '#submit' => array('modulename_custom_form_submit')
        );
    }
}

function modulename_custom_form_submit($form, &$form_state) {
    if($form_state['values']['another_button'] == 'Add to another state') {
        //Do your thing
    }
}
Run Code Online (Sandbox Code Playgroud)

完成必要的修改后,您只需提交创建表单的默认提交操作即可.