0

I'm trying to implement the three Drupal 8 user forms : login, register account and forgotten password forms.

I'm creating custom pages in my module for each one of them because I want to display content around those forms, change titles, hook the forms.

I'm having a hard time to do it, especially to get those forms in order to send them to the template and render them.

0

Here is what I ended up doing in my controllers :

# Login form
public function content() {
  $formBuilder = $this->formBuilder();
  $form = $formBuilder->getForm("Drupal\user\Form\UserLoginForm");
  return [
  '#theme' => 'weber_login_form',
  '#login_form' => $form,
  ];
}

# Forgotten password form
public function content() {
  $formBuilder = $this->formBuilder();
  $form = $formBuilder->getForm("Drupal\user\Form\UserPasswordForm");
  return [
    '#theme' => 'weber_password_form',
    '#password_form' => $form,
  ];
}

# Register form
public function content() {
  $entity = \Drupal::entityManager()
  ->getStorage('user')
  ->create(array());

  $formObject = \Drupal::entityManager()
  ->getFormObject('user', 'register')
  ->setEntity($entity);

  $form = \Drupal::formBuilder()->getForm($formObject);
  return [
    '#theme' => 'weber_register_form',
    '#register_form' => $form,
    '#link' => NULL,
  ];
}
0

I would always prefer to customise the built-in pages rather than use the approach OP followed because those pages still exist and can be visited (unless OP is overriding the routing as well, which they did not mention).

There's a lot that can be done by simpler means:

  1. Using the block system to inject extra content into the pages. Blocks can be programmatic or content managed, whichever best suits your needs.
  2. Using hook_form_ID_alter() to alter the individual forms as required.
  3. Using theme overrides to adjust the HTML for theming purposes.

Without knowing the specific changes required it's difficult to advise in more detail. Certain contributed modules might be useful but again without further information I can't really say.

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.