You are here

使用hook_form_alter()修改表单

g089h515r806 的头像
Submitted by g089h515r806 on 星期二, 2009-08-18 16:07

 

使用hook_form_alter(),你可以修改任何表单。你只需要知道表单的ID就可以了。这里有两种方式可以用来修改表单。
 
修改任意表单
    让我们修改一下登录表单,它位于用户登录区块和用户登录页面中。
 
function formexample_form_alter(&$form, &$form_state, $form_id) {
    // This code gets called for every form Drupal builds; use an if statement
    // to respond only to the user login block and user login forms.
    if ($form_id == 'user_login_block' || $form_id == 'user_login') {
        // Add a dire warning to the top of the login form.
        $form['warning'] = array(
            '#value' => t('We log all login attempts!'),
            '#weight' => -5
        );
        // Change 'Log in' to 'Sign in'.
        $form['submit']['#value'] = t('Sign in');
    }
}
    由于$form是通过引用传递过来的,所以在这里我们对表单定义拥有完全的访问权,并且可以做出任何我们想要的修改。在例子中,我们使用表单默认元素(参看本章后面的“标识文本”一节)添加了一些文本,并接着修改了提交按钮的值。

老葛的Drupal培训班 Think in Drupal

Drupal版本:

评论