1

I'm trying to customize the register form. A user has a reference field to content of type A. However, I only want to allow values of A which satisfy a certain condition. To achieve this, I based my code on https://www.foreach.be/blog/how-manipulate-forms-drupal-8.

I created a module with the following services.

services:
  travels.route_subscriber:
    class: Drupal\travels\Routing\RouteSubscriber
    tags: 
      - {name: event_subscriber }

I defined a RouteSubscriber class.

class RouteSubscriber extends RouteSubscriberBase {

  /**
   * {@inheritdoc}
   */
  protected function alterRoutes(RouteCollection $collection) {
     if ($route = $collection->get('user.register')) {
        $route->setDefault('_form', '\Drupal\travels\ExtendedRegisterForm');
     }

   }
}

I also have an ExtendedRegisterForm class defined as follows.

class ExtendedRegisterForm extends RegisterForm {
     public function buildForm(array $form, FormStateInterface $form_state) {
         $form['test'] = [
            '#type' => 'email',
            '#default_value' => 'test'
          ];
         return $form;
      }
}

If I load the page to register a new user, I get the following error:

Fatal error: Call to a member function getEntityTypeId() on null in xxxx\core\lib\Drupal\Core\Entity\EntityForm.php on line 77

I'm assuming no entity was given because the Entity still needs to be created (after the registration). Does anyone have a suggestion of what I might still need to do to get this working?

0

The error you get is because you are overriding EntityForm::buildForm() without invoking it. In fact, it contains the following code.

  // During the initial form build, add this form object to the form state and
  // allow for initial preparation before form building and processing.
  if (!$form_state->has('entity_form_initialized')) {
    $this->init($form_state);
  }

It is then EntityForm::init() to initialize the entity object.

Truly, you are overriding the wrong method: It's form() that you need to implement, in the same way the RegisterForm class does overriding AccountForm::form().

As for implementing hook_form_alter() or implementing the code as you did, the first is preferable when you need to just add some fields, or change any of them. If you are going to alter the logic behind the registration form, then the code you implemented is probably better. Keep in mind that another module could do the same, but if both the modules need to heavily change the logic behind the registration form, it is also probable they are not compatible with each other.

  • Thank you for the response. This explains why it didn't work. I still went with the hook_form_alter approach however. – Matthias Verstraete Oct 2 '16 at 6:13
0

It seems like you could reduce complexity with hook_form_alter, and manipulate the options on the reference field.

  • Where exactly do I write this hook_form_alter function? I tried adding it to the module file, but it is never called. – Matthias Verstraete Sep 26 '16 at 18:56
  • In a custom module. mymodulename_form_alter($args). You can also do hook_form_FORM_ID_alter, mymodulename_form_user_register_alter($args). api.drupal.org/api/drupal/… – Kevin Sep 26 '16 at 19:23
  • And rebuild caches when you first save the hook – Jonathan Sep 27 '16 at 5:47

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.