Drupal 7 custom form development highlights and examples

Source: Internet
Author: User
Tags define definition drupal

Three key points are summarized:

After the page is submitted and processed by # submit, redirect is required to jump to another page.
If the destination parameter exists in the url path, the page will jump directly to the url indicated by destination, which cannot be controlled.
How to implement multiple steps forms multiple steps, or how to obtain submitted values in a form after the form is submitted.
Form redirect (jump) to another page
The value of $ form_state ['redirect'] can be a string or an array. After the value passes through the url, a jump address is generated.

The code is as follows: Copy code
$ Form_state ['redirect'] = array (
'Node/123 ',
Array (
'Query' => array (
'Foo' => 'bar ',
),
'Fragment '=> 'Baz www.111cn.net ',
}

The page will jump to node/123? Foo = bar # baz

$ Form_state ['redirect'] = 'node/123456' page will jump to node/123

If the value of $ form_state ['redirect'] is not specified, the current page is displayed by default. Drupal_goto (current_path (), array ('query' => drupal_get_query_parameters (); this is done in the API.

When the Form destination (destination) is specified, the jump address can also be changed.
In the drupal_goto function, you can see that if the destination parameter exists in the url path, the page will directly go to the link pointed to by destination. As a result, after multiple buttons under some forms are submitted, the pages that should have been redirected by redirect are also different.

Therefore, in the form # submit function, destination can be directly deleted in some operations.

The code is as follows: Copy code
If (isset ($ _ GET ['destination']) {
$ Form_state ['redirect'] = array ('next _ step_page_url ', array ('query' => drupal_get_destination ()));
Unset ($ _ GET ['destination']);
}

The method I used is to redefine a url and pass destination, but delete destination from $ _ GET.


However, the destination jump of destination is usually used.

Form implements multiple steps, Form reloads, get the value submitted by Form
In fact, these problems are all in the final sense, that is, let the form continue to be submitted. Instead of refreshing the page.

You only need to execute the following code in the # submit function of form:

The code is as follows: Copy code
If ($ form_state ['values'] ['OP'] = t ("Next Step ")){
$ Form_state ['rebuilt'] = TRUE;
$ Form_state ['store'] ['users'] = $ form_state ['values'] ['users'];
}

In the define definition of form, you can get the value $ form_state ['store'] ['users.

Let's look at an example.

 

The code is as follows: Copy code

<? Php
/**
* Implements hook_element_info ().
*/
Function example_element_info (){
$ Types = array (
'Example '=> array (
'# Input' => TRUE,
'# Tree' => TRUE,
'# Process' => array ('example _ process '),
'# Theme' => array ('example '),
'# Pre_render' => array ('form _ pre_render_conditional_form_element '),
'# Value_callback' => 'example _ value_callback'
    )
); // Www.111Cn. Net
 
Return $ types;
}

/**
* Processing of parameters in the element before the element is displayed
*/
Function example_process ($ element, $ form_state ){
If (isset ($ element ['default _ value']) {
$ Element ['# default_value'] = $ element ['default _ value'];
    }
Return $ element;
}

/**
* Element-rendered Template
*/
Function theme_example ($ variables ){
$ Output = '<input name = "example" type = "text" value = ""/> ';
Return $ output;
}

/**
* Final value of element after from submit
*/
Function example_value_callback ($ element, $ input = FALSE, & $ form_state ){
If ($ input! = FALSE ){
// Element can be composed of multiple elements, but only one return value is returned. Other elements will not be returned.
Return $ _ POST ['example '];
} Elseif (! Empty ($ element ['# default_value']) {
Return $ element ['# default_value'];
  }
 
Return;
}

/**
* Implements hook_theme ().
*/
Function example_theme (){
Return array (
'Example '=> array (
'Render element' => 'element ',
      )
);
}

Function example_menu ()
{
$ Items = array ();
$ Items ['test _ element'] = array (
'Title' => 'example ',
'Page callback' => 'drupal _ get_form ',
'Page arguments' => array ('example _ form '),
'Access callback' => TRUE,
'Expanded' => TRUE,
);
Return $ items;
}

Function example_form ($ form, & $ form_state ){
$ Form = array ();
$ Form ['example '] = array (
'# Type' => 'example ',
'# Title' => 'example ',
);
$ Form ['submit '] = array (
'# Type' => 'Submit ',
'# Value' => 'Submit'
);
Return $ form;
}

Function example_form_submit ($ form, & $ form_state ){
Drupal_set_message ('value: '. $ form_state ['values'] ['example']);
}

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.