Use Drupal Form Hooks to customize the Form

Source: Internet
Author: User
Tags php print contact form drupal

The Hooks (hook) that is most commonly used in Drupal usage or development is hook_form_alter. The content creation, contact form, Menu, and, form hooks are used for user registration.

Drupal Form Hooks
The hook in hook_form_alter is directly replaced with your module name.

The code is as follows: Copy code

/**
* Implements hook_form_alter ().
*/
Function custom_form_alter (& $ form, & $ form_state, $ form_id ){
Switch ($ form_id ){
Case 'user _ profile_form ':
// Doing something
Break;
}
}

Hook_form_FORM_ID_alter is a variant of hook_form_alter. It directly modifies a specific form.

The code is as follows: Copy code

/**
* Implements hook_form_FORM_ID_alter ().
*/
Function custom_form_user_profile_form_alter (& $ form, & $ form_state ){
If ($ form ['# user_category'] = 'Settings '){
If (variable_get ('resumable _ timezones', 1 )){
System_user_timezone ($ form, $ form_state );
}
Return $ form;
}
}

With the preceding two Hooks, you can easily add custom form elements to Drupal.


Each form can customize the elements in the front of theme. The elements of render are passed to the topic through variables.

The code is as follows: Copy code

/**
* Implements hook_theme ().
*/
Function custom_theme (){
Return array (
'User _ profile_form '=> array (
'Render element' => 'form ',
),
);
}

Customize the form element style.

The code is as follows: Copy code

Function theme_user_profile_form ($ variables ){
$ Form = $ variables ['form'];

$ Output = drupal_render ($ form ['info']);

$ Header = array (t ('factor '), t ('weight '));
Foreach (element_children ($ form ['factors']) as $ key ){
$ Row = array ();
$ Row [] = $ form ['factors'] [$ key] ['# title'];
$ Form ['factor'] [$ key] ['# title_display'] = 'invisable ';
$ Row [] = drupal_render ($ form ['factors'] [$ key]);
$ Rows [] = $ row;
}
$ Output. = theme ('table', array ('header' => $ header, 'rows '=> $ rows ));

$ Output. = drupal_render_children ($ form );
Return $ output;
}

Use hook_preprocess_FORM_ID to modify $ variables before theme form element.

The code is as follows: Copy code

Function custom_preprocess_user_profile_form (& $ variables ){
If ($ variables ['form'] ['actions]) {
// Change the button name
}
// Add new variable to theme form
}

The html element of the custom form can define a template for the form theme. Note that this will reduce the drupal performance, but the benefit is that you can customize html.

The code is as follows: Copy code

/**
* Implements hook_theme ().
*/
Function lixiphp_theme ($ existing, $ type, $ theme, $ path ){
Return array (
'User _ profile_form '=> array (
'Render element' => 'form ',
'Templates' => 'Templates/form/user-profile ',
),
);
}

The create user-profile.tpl.php file is under the templates/form directory.

The code is as follows: Copy code

<? Php
Print drupal_render ($ form ['form _ id']);
Print drupal_render ($ form ['form _ build_id ']);
Print drupal_render ($ form ['form _ token']);
?>
<Li class = "rows">
<? Php print drupal_render ($ form ['actions']);?>
</Li>

The custom form method described in this article is applicable to Drupal6, Drupal7, and drupal8.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.