开发者

How do I persist form data across an "access denied" page in Drupal?

开发者 https://www.devze.com 2022-12-26 19:42 出处:网络
We\'re building a small sub-site that, on the front page, has a one input box form that users can submit. From there, they\'re taken to a page with a few more associated form fields (add more details,

We're building a small sub-site that, on the front page, has a one input box form that users can submit. From there, they're taken to a page with a few more associated form fields (add more details, tag it, etc.) with the first main form field already filled in. This works splendidly, thus far.

The problem comes for users that are not logged in. When they submit that first form, they're taken to a (LoginToboggan based) login page that allows them to login. After they login, they redirect to the second form page, but the first main form field isn't filled in -- in other word开发者_运维技巧s, the form data didn't persist.

How can we store that data and have it persist across the access denied page?


You could save the data in a cookie that would be passed 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 "fieldname" in the function to generate 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 the cookie(s) 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 login and are redirected to page two of your form, you can read the value of the cookie(s) and if they exist, insert them into the default values of 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
  );
}

The cookies(s) will be presented on each page load after they are set, so it doesn't matter if there are several failed login attempts or other page views in between. You should probably delete the cookies after they're used or set a short expiration time so that they are cleaned up automatically.


I don't know if there is a good way to transfer form data from two entirely different forms in Drupal, especially in the context of access denied. The best choices I can find would be to:

  • Use the url to send the data for the field.
  • You could also save the data in the db, but it might be a bit tricky to extract the data again.
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号