1

I created a module with Drupal 8 beta11 which defines an content entity and a form (contentEntityForm) with custom submit callback.When I tried using the same module with Drupal 8 beta 14 it raise the following error.

Fatal error: Call to a member function extractFormValues() on null in C:\xampp\htdocs\drupalbeta14\core\lib\Drupal\Core\Entity\ContentEntityForm.php on line 219

The form code is the following one.

public function buildForm(array $form, FormStateInterface $form_state) {
  $form['employee']['first_name'] = array(
    '#type' => 'textfield',
    '#title' => 'First Name',
    '#required' => TRUE,
  );

  $form['employee']['last_name'] = array(
    '#type' => 'textfield',
    '#title' => 'Last Name',
    '#required' => TRUE,
  );
  
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Submit',
    '#name' => 'employee-data',
    '#submit' => array('::submitEmployeeData')
  );

  return $form;
}

public function submitEmployeeData(array $form, FormStateInterface $form_state) {
}

After the form is submitted, the error mentioned above is shown.

I checked the contentEntityForm.php file on beta11 and beta14 and it seems to have a similar definition for extractFormValues().

1 Answer 1

0

This question is old but I stumbled across this issue as well, so posting the solution for future googlers..

Basically, you need to init the form_state within the buildForm method with:

if (!$form_state->has('entity_form_initialized')) {
  $this->init($form_state);
}

I found this solution here: https://withpulp.com/blog/drupal-forms-api/#initialize-the-form-state

When extending the ContentEntityForm class the parent buildForm normally handles this for you, but if for some reason you choose not to extend the buildForm you will need to init the form_state yourself.

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.