Drupal7 form form secondary development points and examples

  • 2021-01-18 06:20:48
  • OfStack

Please remember to bookmark this article, as you will often need to skip or reload form forms when doing Drupal 7 custom module.

The three main points are summarized:

1. After the page is submitted and processed by #submit, ES10en is required to jump to another page.
2. If destination parameter exists in url path, the page will directly jump to url referred to by destination, which can not be controlled.
3. How to implement multiple, steps and forms, or how to get the submitted value from the form after the form is submitted.

1. Form form redirect (skip) to another page

$form_state['redirect'] = $form_state['redirect'] = $form_state['redirect'];

$form_state['redirect'] = array(
  'node/123',
  array(
    'query' => array(
      'foo' => 'bar',
    ),
    'fragment' => 'baz',
}
// The page will jump to  node/123?foo=bar#baz

$form_state['redirect'] = 'node/123'
// The page will jump to  node/123


If you do not specify a value of $form_state['redirect'], the default is to jump to the current page. drupal_goto (current_path (), array (' query '= > drupal_get_query_parameters())); This is done in API.

2. The Form form destination (destination) can also change the address of the jump when it is specified

In the drupal_goto function, you can see that if the destination parameter is present in the url path, the page will go directly to the link destination points to, resulting in multiple buttons under some forms being submitted, and the page that should have been redirect will not be different.

destination can be deleted directly from the #submit function of form.

if (isset($_GET['destination'])) {
  $form_state['redirect'] = array('next_step_page_url', array('query' => drupal_get_destination()));
  unset($_GET['destination']);
}

The approach I took was to redefine 1 url and continue passing destination, but remove the destination from $_GET. However, destination is often used as a destination jump.

3. The Form form implements multiple steps of multiple steps. The Form form is overloaded to obtain the value submitted by Form

All of these problems really boil down to the same thing, which is to keep the form submitted. Instead of refreshing the page. Simply execute the following code in the #submit function of the form form:

if ($form_state['values']['op'] == t("Next Step")) {
  $form_state['rebuild'] = TRUE;
  $form_state['storage']['users'] = $form_state['values']['users'];
}

The value $form_state['storage']['users'] can be found in the define definition of form.

Refer to Drupal7 related API functions:

drupal_redirect_form
drupal_goto
drupal_get_destination


Related articles: