1

I have written a hook form alter which adds a submit action but I get this error

Recoverable fatal error: Argument 2 passed to xxx_form_submit() must be an instance of FormStateInterface, instance of Drupal\Core\Form\FormState given in xxx_form_submit()

the code is like this

function xxx_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {

 if ($form_id=='user_login_form') {

    $form['actions']['submit']['#submit'][] = 'xxx_form_submit';
  }
}

function xxx_form_submit ($form, FormStateInterface  $form_state) {
  $username=$form_state->getValue('name'));
}
1
  • I am closing this question as it's a plain PHP question. Drupal doesn't change how PHP namespaces are used, nor when a fully qualified class name is necessary.
    – apaderno
    May 12, 2019 at 12:48

2 Answers 2

4

You are getting this because you did not include the use statement at the top of your file:

use Drupal\Core\Form\FormStateInterface;

function xxx_form_alter(&$form, FormStateInterface $form_state, $form_id) {

 if ($form_id=='user_login_form') {

    $form['actions']['submit']['#submit'][] = 'xxx_form_submit';
  }
}

You can use the full path, but it is preferred for developers to do use statements.

0

I had to put the whole path of the formstateinterface into the submit function \Drupal\Core\Form\FormStateInterface

1
  • 3
    That's because you didn't add a use statement at the top of your file for FormStateInterface.
    – Kevin
    Sep 22, 2016 at 20:18

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