0

In drupal 8, I have created a custom module to alter the user login and register forms in 2 class that extends UserLoginForm and RegisterForm respectively.

Whereas for the login form I can add some link in the $form['action'] array or add a class to the submit button:

 $form['actions']['submit']['#attributes'] = [
    'class' => ['btn', 'btn-primary'],
 ];

a contrario, there is no action array in user register $form variable(!)

So how can I access in my class the submit button, and more generally, the action array?

2
  • In general if you do hook_form_FORM_ID_alter like "YOURMODULE_form_user_register_form_alter(&$form, FormStateInterface $form_state, $form_id)" you can access $form['actions']['submit'] where you can provide #attributes to it.
    – Ajay Reddy
    Commented Oct 30, 2017 at 7:58
  • Thank you but I wanted to access it in the extended Class, but I will go for it if I cannot find the way.
    – anou
    Commented Oct 30, 2017 at 8:03

1 Answer 1

2

While UserLoginForm is a regular form based on form api, RegisterForm is an entity form, where the actions are defined in a separate method EntityForm::actions(), which is overridden in RegisterForm:

  protected function actions(array $form, FormStateInterface $form_state) {
    $element = parent::actions($form, $form_state);
    $element['submit']['#value'] = $this->t('Create new account');
    return $element;
  }

which you can override again in your extended class.

1
  • Thanks, I just found it when I saw your comment :-). But now my class just throw an error so... I must have miss something on class extends...
    – anou
    Commented Oct 30, 2017 at 12:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.