TransWikia.com

how to add an event subscriber when the user reaches the login form

Drupal Answers Asked on October 26, 2021

I am new on Drupal 8 , I have implemented a hook ‘hook_form_alter’ in my module to add a new field to the login form , but now I want to handle it with the event , I want to trigger an event when the user reaches the login form and then add this field using event not a hook.

Can anyone please suggest me how to do it ?

This is the hook

function my_module_form_alter(&$form, DrupalCoreFormFormStateInterface $form_state, $form_id) {
  if ($form_id == 'user_login_form' ) {
    // Add placeholders.
    $form['condition'] = array(
      '#type' => 'checkbox',
      '#options' => t('Enable'),
      '#title' => t('J’accepte les conditions'),
      '#required' => true,
    );
  }
}

One Answer

You can't, not with core alone. D8 doesn't have a form-alter event.

D8 added events as an alternative to some D7 hooks but didn't fully replace all the hooks available. Replacing all hooks was mooted for a D10 goal but didn't make the cut.

As such, the only way to do this modify core functionality like:

  • Patch core (see issue comment) to add form-alter events.
  • Do something like Hook Event Dispatcher which wraps custom form-alter events around a hook_form_alter handler.

Then add an event subscriber for it.

Answered by Shawn Conn on October 26, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP