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?