Multiple Entity Form

Categories

Component ID

1972772

Component name

Multiple Entity Form

Component type

module

Maintenance status

Development status

Component security advisory coverage

covered

Downloads

7196

Component created

Component changed

Component body

Provides helper functions for creating forms that display multiple entities, of multiple types, showing selected fields. This can also include new, unsaved entities.

Example:

// Create a form builder as normal.
function my_form($form, &$form_state) {
  // Define an array of data for the entities to show in the form.
  $entity_data = array();
  // First entity: node 1.
  $entity_data[] = array(
    'entity_type' => 'node',
    'entity' => node_load(1),
    'fields' => array('field_foo', 'field_bar'),
    'fieldset' => t('My First Node'),
  );
  // Second entity: node 2.
  $entity_data[] = array(
    'entity_type' => 'node',
    'entity' => node_load(2),
    // 'fields' not given: all fields shown.
  );
  
  $form += multiple_entity_form($form, $form_state, $entity_data);

  // Add your submit button and other form elements you need.
  
  return $form;
}

function my_form_validate($form, &$form_state) {
  multiple_entity_form_validate($form, $form_state);
}

function my_form_submit($form, &$form_state) {
  multiple_entity_form_submit($form, $form_state);

  // Save the entities.
  foreach ($form['#entity_form_keys'] as $form_key) {
    $entity_type = $form[$form_key]['#entity_type'];
    $entity = $form[$form_key]['#entity'];
    
    entity_save($entity_type, $entity);
  }
}