1

I want to redirect a user when they log in to node/2. In Drupal 7, this is just drupal_goto(), but that was removed from 8. This is what I tried:

function mymodule_form_user_register_form_alter(&$form, &$form_state, $form_id) {
  $form_state->setRedirect('node/2');
}

I'm not getting an error but I'm not getting redirected, either.

EDIT: Then, based on Berdir's comments, I tried this:

function mymodule_form_user_register_form_alter(&$form, &$form_state, $form_id) {
  $form['#submit'][] = 'mymodule_user_register_submit_handler';
}

function mymodule_user_register_submit_handler($form, &$form_state) {
  $form_state->setRedirect('entity.node.canonical', ['node' => 2]);
}

However, this gives me an integrity error:

Drupal\Core\Entity\EntityStorageException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '16-0-0-ja' for key 'PRIMARY': INSERT INTO {user__field_agree} (entity_id, revision_id, bundle, delta, langcode, field_agree_value) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5); Array ( [:db_insert_placeholder_0] => 16 [:db_insert_placeholder_1] => 16 [:db_insert_placeholder_2] => user [:db_insert_placeholder_3] => 0 [:db_insert_placeholder_4] => ja [:db_insert_placeholder_5] => 1 ) in Drupal\Core\Entity\Sql\SqlContentEntityStorage->save() (line 757 of core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php).
Drupal\Core\Database\Statement->execute(Array, Array)
Drupal\Core\Database\Connection->query('INSERT INTO {user__field_agree} (entity_id, revision_id, bundle, delta, langcode, field_agree_value) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5)', Array, Array)
Drupal\Core\Database\Driver\mysql\Connection->query('INSERT INTO {user__field_agree} (entity_id, revision_id, bundle, delta, langcode, field_agree_value) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5)', Array, Array)
Drupal\Core\Database\Driver\mysql\Insert->execute()
Drupal\Core\Entity\Sql\SqlContentEntityStorage->saveToDedicatedTables(Object, , Array)
Drupal\Core\Entity\Sql\SqlContentEntityStorage->doSaveFieldItems(Object, Array)
Drupal\user\UserStorage->doSaveFieldItems(Object)
Drupal\Core\Entity\ContentEntityStorageBase->doSave(NULL, Object)
Drupal\Core\Entity\EntityStorageBase->save(Object)
Drupal\Core\Entity\Sql\SqlContentEntityStorage->save(Object)
Drupal\Core\Entity\Entity->save()
Drupal\user\RegisterForm->save(Array, Object)
call_user_func_array(Array, Array)
Drupal\Core\Form\FormSubmitter->executeSubmitHandlers(Array, Object)
Drupal\Core\Form\FormSubmitter->doSubmitForm(Array, Object)
Drupal\Core\Form\FormBuilder->processForm('user_register_form', Array, Object)
Drupal\Core\Form\FormBuilder->buildForm(Object, Object)
Drupal\Core\Controller\FormController->getContentResult(Object, Object)
call_user_func_array(Array, Array)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
call_user_func_array(Object, Array)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1)
Stack\StackedHttpKernel->handle(Object, 1, 1)
Drupal\Core\DrupalKernel->handle(Object)
  • 1
    This could also be accomplished using Rules 8 – No Sssweat Feb 20 '16 at 10:34
  • 1
    @NoSssweat Yes, I can't wait till Rules is marked stable for D8. – Patrick Kenny Feb 20 '16 at 10:41
  • you can do it with the latest 8.x-3.0-unstable7 version, see my demo, but if you want to wait for a stable version, I understand. – No Sssweat Feb 20 '16 at 12:10
1

Redirection can be set in a submit handler.

function mymodule_user_register_submit_handler($form, &$form_state) {
  $form_state->setRedirect('entity.node.canonical', ['node' => 2]);
}

But the new submit handler should be appended to the submit button's '#submit' value, not the form's root '#submit'.

function mymodule_form_user_register_form_alter(&$form, &$form_state, $form_id) {
  $form['actions']['submit']['#submit'][] = 'mymodule_user_register_submit_handler';
}
1

You can't use setRedirect() in form alter, that hasn't changed compared to Drupal 7. That would send the user away while building/displaying the form.

Just like in Drupal 7, you need to add an additional submit callback and then set the redirect there.

Also note that that setRedirect() expects a route name and arguments, not a path. In your case, that would be $form_state->setRedirect('entity.node.canonical', ['node' => 2])); or $form_state->setRedirectUrl($node->toUrl()).

  • 1
    I tried adding a callback, but I'm getting a duplicate insert error. Is there some trick to that? – Patrick Kenny Feb 20 '16 at 14:50
1

So like you I tried Berdir's answer in about every different way possible. For some reason the using the mymodule_form_user_register_form_alter hook did not work, but the mymodule_form_alter hook with a conditional did like:

function mymodule_user_register_custom_submit_handler($form, 
FormStateInterface $form_state) {
    $form_state->setRedirect('entity.node.canonical', ['node' => 1]);
}
function mymodule_form_alter(array &$form, FormStateInterface $form_state, 
$form_id) {
  if ($form_id == 'user_register_form') {
    $form['actions']['submit']['#submit'][] = 'mymodule_user_register_custom_submit_handler';
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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