1

If I create a custom form with get method:

$form['#method'] = 'get';

When I click the submit button, many variables are populated with GET Can I remove them cleanly according to the drupal logic?

4 Answers 4

4

You need to alter the form after it has been built, for example

$build = [];
$build['filter'] = $this->formBuilder->getForm(LibrarySearch::class);
$build['filter']['form_id']['#access'] = FALSE;
$build['filter']['form_build_id']['#access'] = FALSE;
$build['filter']['form_token']['#access'] = FALSE;

Once the form has been turned into a render array, you can set the access to FALSE for those items. This is also documented in the core Search module.

1
1

For me, unsetting inside form_alter didn't work. I had to do it in a different way.

Add this inside form_alter hook.

  $form['#after_build'][] = 'function_name';

Later add a separate function and unset inside that.

function function_name($form){

  // Unset variables to cleanup the url after submission
    unset($form['form_token']);
    unset($form['form_build_id']);
    unset($form['form_id']);
    return $form;
  }
0

Another way to solve this problem.

Implement a hook form alter in your corresponding module in the

[name_of_your_module].module file, just add

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * Need to don't see the parameters 'form_build_id' and 'form_id' in the url.
 */
function hook_form_FORM_ID_alter(&$form, &$form_state, $form_id) {
  unset($form['form_build_id'], $form['form_id']);
}
0

Needs to alter the form to unset form_id and form_build_id. In bellow code formid is the id of the form where you want to remove form_id and form_build_id.

function mymodule_form_formid_alter(&$form, &$form_state, $form_id) {

unset($form['form_build_id'], $form['form_id']);

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.