Skip to main content
Category:

Code snippet that can be used to redirect your custom form to another page and pass a variable Drupal 8.

public function buildForm(array $form, FormStateInterface $form_state, $arg = NULL)
{
  $form['email'] = array(
    '#type' => 'email',
    '#title' => t('Email'),
    '#required' => TRUE,
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
  );
  return $form;
}

public function submitForm(array &$form, FormStateInterface $form_state)
{
  $path = \Drupal\Core\Url::fromRoute('your.routename', ['email' => $form_state->getValue('email')])->toString();
  $response = new RedirectResponse($path);
  $response->send();
}

or use this method:

FormState::setRedirectUrl()

$url = Url::fromUri('internal:' . 'YOUR_ROUTE', $params);
$form_state->setRedirectUrl($url);

 

 

Riadh Rahmi

Senior Web Developer PHP/Drupal & Laravel

I am a senior web developer, I have experience in planning and developing large scale dynamic web solutions especially in Drupal & Laravel.

Web Posts

Search

Page Facebook