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?
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.
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;
}
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']);
}
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']);
}