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, \Drupal\Core\Form\FormStateInterface $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,
  • That doesn't make sense (event). hook_form_alter is perfect. What's the problem with it? Jul 17 '20 at 11:52
  • there is no problem , but i want to implement it by the event , because in drupal 8 all the hooks was converted to events i think , it's just like an exercise Jul 17 '20 at 12:03
  • 2
    This still needs work in core, see drupal.org/project/drupal/issues/2402445
    – 4k4
    Jul 17 '20 at 12:13
  • that means it's not yet exist in the core Jul 17 '20 at 12:15

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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