My objective: Let say there is a role named "author". I would like to have a registration form having this path: /register/author
, when the user submit, he/she will get the author role (same for any other roles like /register/another_role
)
Here is what I have done:
1-Creating a route: last argument will be the role name
mymodule.register:
path: '/register/{role}'
defaults:
_form: 'Drupal\mymodule\Form\RegisterByRole'
_title: 'Register by role'
requirements:
_permission: 'access content'
2-Extending the RegisterForm Class
class RegisterByRole extends RegisterForm
{
public function form(array $form, FormStateInterface $form_state, $role = null)
{
$form = parent::form($form, $form_state);
if (role_is_allowed($role)) {
$form['test'] = [
'#markup' => '<p>Test:' . $role
];
}
return $form;
}
}
Of course I must change the submit function to assign the role, but I already have the following error when visiting the /register/author path:
Error: Call to a member function getEntityTypeId() on null in Drupal\Core\Entity\EntityForm->getBaseFormId() (line 111 of core/lib/Drupal/Core/Entity/EntityForm.php).
I found that if I add the __construct
and the create
functions in the RegisterByRole
class definition, the getBaseFormId
error disappears... But I don't understand why?... And more annoying, I couldn't get the value of my $role argument (last arg of the route).
What is wrong in my plan?
$user->addRole($role);
and pass the entity to the form as shown in the linked question. – 4k4 Mar 12 '20 at 9:20