1

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') – JHONATAN DAVID FERNANDEZ ROSA Nov 15 '17 at 20:38
  • 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
0

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, privacy policy and cookie policy

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