Example of entity-within-entity subform processing Suppose that, in Drupal 7, EntityA includes one or more instances of EntityB.

Is there an example of building the create/edit form for EntityA that includes one or more edit forms for EntityB, and that can ajax in a new create form for a new EntityB?
It's not really that hard. You would want to create form elements that use the #tree attribute, to make sure the data for instances of EntityB will be well structured.

In order to make add another functionality you need to:


Keep track of the count in the $form_state.

if (empty($form_state['count'])) {
  $form_state['count'] = 2;
}

Add a button somewhere to add another.

$form['entity_b']['add_another'] = array(
  '#type' =&gt; 'submit',
  '#value' =&gt; t('Add another'),
  '#limit_validation_errors' =&gt; array(),
  '#submit' =&gt; array('add_another_submit_handler'),
);

function add_another_submit_handler(&amp;$form, &amp;$form_state) {
  // Add one to our counter and rebuild the form
  $form_state['count'] += 1;
  $form_state['rebuild'] = TRUE;
}



The above will work with no JavaScript. If you want to Ajax-enable it, you need to do some work on the wrapper.


Add a CSS ID that is used from the Ajax callback. 

$form['entity_b'] = array(
  '#tree' =&gt; TRUE,
  '#attributes' =&gt; array('id' =&gt; array('add-another')),
  // ...
);

Add the #ajax element.

 // ...
 '#ajax' =&gt;  array(
  'callback' =&gt; 'add_another_ajax',
  'wrapper' =&gt; 'add-another',
  'effect' =&gt; 'fade',
 ),
 // ...

Add the Ajax callback function.

function add_another_ajax($form, &amp;$form_state) {
  return $form['entity_b'];
}