3 namespace Drupal\form_test\Form;
5 use Drupal\Core\Form\FormBase;
6 use Drupal\Core\Form\FormStateInterface;
10 * Form builder to detect form redirect.
12 class FormTestRedirectForm extends FormBase {
17 public function getFormId() {
18 return 'form_test_redirect';
24 public function buildForm(array $form, FormStateInterface $form_state) {
25 $form['redirection'] = [
26 '#type' => 'checkbox',
27 '#title' => t('Use redirection'),
29 $form['destination'] = [
30 '#type' => 'textfield',
31 '#title' => t('Redirect destination'),
34 ':input[name="redirection"]' => ['checked' => TRUE],
40 '#value' => t('Submit'),
49 public function submitForm(array &$form, FormStateInterface $form_state) {
50 if (!$form_state->isValueEmpty('redirection')) {
51 if (!$form_state->isValueEmpty('destination')) {
52 // The destination is a random URL, so we can't use routed URLs.
53 // @todo Revist this in https://www.drupal.org/node/2418219.
54 $form_state->setRedirectUrl(Url::fromUserInput('/' . $form_state->getValue('destination')));
58 $form_state->disableRedirect();