I have a weird use case. I created a custom form with some fields and I added some other fields from one entity form like this:

public function buildForm(array $form, FormStateInterface $form_state) {
    $entity_fields = array('entity_field_1', 'entity_field_2');
    // added other form fields here
    $entityForm = \Drupal::service('entity.form_builder')->getForm($entity, 'default');
    foreach( $entity_fields as $key ) {
        $form[$key] = $entityForm[$key];
    // etc
    return $form;

I need to get the exact entity field widget because these fields can be updated from the interface and I can't have them hard-coded.

Now, If I submit the form, the form state values for 'entity_field_1', 'entity_field_2' are empty. $form_state->getValue('entity_field_1') is returning an empty array. Other fields defined manually are returning the correct value when saving.

How do I get those entity field values? In drupal 7 I used field_attach_submit() to attach the form values to an entity.

  • Have you tried to use? $form_state->getValue('entity_field_1') instead of $form_state->get('entity_field_1') Nov 15, 2017 at 20:38
  • Why do you ask? Nov 15, 2017 at 20:59
  • Yeah, getValue() I used and it doesn't work. It was a typo in the question and I will fix it. Nov 17, 2017 at 9:39

1 Answer 1


You could also try $form_state->getValues()['entity_field_1'] as getValues() will build an array of values with the field name as the key.

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.