Embed one entity form into another and save both I have a custom entity, that is depending on the user entity. In fact, it is even so depending that I felt it made sense to display my entity form within the user profile form:



The problem I am now having is the following; there are 2 save buttons. And if it is not bad enough, the save button for the user (the lower one) does not even function anymore and white label save button only saves the white label entity.

The form is altered into the user form like this:

function whitelabel_form_user_form_alter(&amp;$form, FormStateInterface $form_state) {

  $whitelabel = WhiteLabel::load(1);

  $whitelabel_form = \Drupal::service('entity.manager')
    -&gt;getFormObject('whitelabel', 'default')
    -&gt;setEntity($whitelabel);

  $form['whitelabel'] = array(
    '#type' =&gt; 'details',
    '#title' =&gt; t('White label settings'),
    '#open' =&gt; TRUE,
    'form' =&gt; \Drupal::formBuilder()-&gt;getForm($whitelabel_form),
  );
}


I was hoping to shuffle some parameters in the $whitelabel_form array (that used to work in Drupal 7), but that array is enormous and I could not find the submit buttons and handler that I needed.

So the question is, can this be done? And what would be the recommended way of doing it?
Instead of trying to do your own thing, you should try Inline Entity Form module. This module is made for this specific case (creating / editing entities within entity forms).

I know a lot of work has been put in to this to improve workflow in Drupal Commerce, which means this should be working well. I haven't tested it out myself, but since Drupal Commerce depend on it in Drupal 8 as well, it should be quite stable already.

The module works by adding a widget to the entity reference field that creates the form, so it should be pretty much plug and play. The only requirement is that the user has a reference to your custom entity.