【问题标题】:how to add a button to drupal forms?如何在 drupal 表单中添加按钮?
【发布时间】:2023-12-28 20:22:01
【问题描述】:

我想在 drupal 创建表单中添加一个自定义按钮,因此如果用户单击它而不是提交按钮,则创建对象的工作流状态将更改为另一个状态(不是默认的第一个状态) 有什么建议吗?

【问题讨论】:

    标签: drupal forms button


    【解决方案1】:

    要修改由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
        }
    }
    

    进行必要的修改后,您可以简单地提交到创建表单的默认提交操作。

    【讨论】: