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.