AnswerBun.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!

Related Questions

Make node only accesible by path alias

3  Asked on November 19, 2021 by kevin-ramirez-zavalza

     

Form and gated content

1  Asked on November 17, 2021 by dataguy

 

Calculate Star rating based on webform submissions

1  Asked on November 17, 2021 by brainhax

 

Pager not working when $form[‘table’] is called with AJAX

1  Asked on November 17, 2021 by user3428971

     

Send message notification when license expired

1  Asked on November 15, 2021 by learner123

 

View filtering, Relationship Vs Contextual filter

1  Asked on November 15, 2021 by bassem-mohamed

   

Missing some block fields

1  Asked on November 15, 2021 by metalbearsolid

   

Custom template for each profile2 profile type edit form

1  Asked on November 13, 2021 by stavovei-ciprian

       

How to save an image resource

1  Asked on November 13, 2021 by kosher

 

How to alter node body content through Views pre render hook?

1  Asked on November 11, 2021 by pramodg

 

Set Default Value for Image Field contain Multiple Images

1  Asked on November 11, 2021 by pglatz

   

Ask a Question

Get help from others!

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