How to save form data on the "Access Denied" page in Drupal?

We create a small sub-site, which on the first page has one input form that users can send. From there, they get to a page with several additional form fields (add more details, mark them, etc.) with the first form field already completed. It works great, for now.

The problem occurs for users who are not logged in. When they submit this first form, they go to the login page (LoginToboggan), which allows them to login. After entering the system, they are redirected to the second page of the form, but the first field of the main form is not filled - in other words, the form data is not saved.

How can we save this data and save it on the access denied page?

+3
source share
2 answers

You can save the data in a cookie, which will be sent to the page after the login page.

Assuming that you are using the form API to create the form and that you have a field called "field name" in the function to create the form:

function my_form() {
  $form['fieldname'] = array (
    '#type'          => 'textfield',
    '#title'         => 'Some fieldname'
  );

  // Second field if you need to save another
  $form['fieldname2'] = array (
    '#type'          => 'textfield',
    '#title'         => 'Some other fieldname'
  );
}

Set a cookie in the submit handler for your form:

function my_form_submit($form, &$form_state) {
  // Set the cookie with their data before they are redirected to login
  // Use the array syntax if you have one than one related cookie to save,
  //   otherwise just use setcookie("fieldname",...

  setcookie("saved_data[fieldname]", $form_state['values']['fieldname']);

  // Your other code
}

After they log in and redirected to the second page of the form, you can read the value of the cookie (s), and if they exist, insert them into the default values ​​for the fields:

function my_form() {
  if (isset($_COOKIE['saved_data[fieldname]'])) {
    $default_fieldname = $_COOKIE['saved_data[fieldname]'];
  }
  else {
    $default_fieldname = '';
  }

  // Same check for the second field
  if (isset($_COOKIE['saved_data[fieldname2]']))...

  $form['fieldname'] = array (
    '#type'          => 'textfield',
    '#title'         => 'Some fieldname',
    '#default_value' => $default_fieldname
  );

  // Second field if used
  $form['fieldname2'] = array (
    '#type'          => 'textfield',
    '#title'         => 'Some other fieldname',
    '#default_value' => $default_fieldname2
  );
}

cookie , , . , cookie , .

+1

, Drupal, . , , - :

  • URL .
  • db, .
0

Source: https://habr.com/ru/post/1740834/


All Articles