Customizing the register form with a new class: Why do I get this error? 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-&gt;get('user.register')) {
        $route-&gt;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' =&gt; 'email',
            '#default_value' =&gt; '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?
It seems like you could reduce complexity with hook_form_alter, and manipulate the options on the reference field.