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.

LATER EDIT: Looks like $form_state->getUserInput() is returning the right values but they are multidimensional arrays for some fields.


Have you tried to use?


instead of

  • Why do you ask? – Pierre.Vriens Nov 15 '17 at 20:59
  • Yeah, getValue() I used and it doesn't work. It was a typo in the question and I will fix it. – Marius Ilie Nov 17 '17 at 9:39

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.