You are here

function entity_ui_get_form in Entity API 7

A wrapper around drupal_get_form() that helps building entity forms.

This function may be used by entities to build their entity form. It has to be used instead of calling drupal_get_form(). Entity forms built with this helper receive useful defaults suiting for editing a single entity, whereas the special cases of adding and cloning of entities are supported too.

While this function is intended to be used to get entity forms for entities using the entity ui controller, it may be used for entity types not using the ui controller too.

Parameters

$entity_type: The entity type for which to get the form.

$entity: The entity for which to return the form. If $op is 'add' the entity has to be either initialized before calling this function, or NULL may be passed. If NULL is passed, an entity will be initialized with empty values using entity_create(). Thus entities, for which this is problematic have to care to pass in an initialized entity.

$op: (optional) One of 'edit', 'add' or 'clone'. Defaults to edit.

$form_state: (optional) A pre-populated form state, e.g. to add in form include files. See entity_metadata_form_entity_ui().

Return value

array The fully built and processed form, ready to be rendered.

See also

EntityDefaultUIController::hook_forms()

entity_ui_form_submit_build_entity()

2 calls to entity_ui_get_form()
entity_metadata_form_entity_ui in modules/callbacks.inc
Callback to get the form for entities using the entity API admin ui.
entity_ui_get_bundle_add_form in ./entity.module
Page callback to add an entity of a specific bundle.
2 string references to 'entity_ui_get_form'
EntityContentUIController::hook_menu in includes/entity.ui.inc
Provides definitions for implementing hook_menu().
EntityDefaultUIController::hook_menu in includes/entity.ui.inc
Provides definitions for implementing hook_menu().

File

./entity.module, line 1266

Code

function entity_ui_get_form($entity_type, $entity, $op = 'edit', $form_state = array()) {
  if (isset($entity)) {
    list(, , $bundle) = entity_extract_ids($entity_type, $entity);
  }
  $form_id = !isset($bundle) || $bundle == $entity_type ? $entity_type . '_form' : $entity_type . '_edit_' . $bundle . '_form';
  if (!isset($entity) && $op == 'add') {
    $entity = entity_create($entity_type, array());
  }

  // Do not use drupal_get_form(), but invoke drupal_build_form() ourself so
  // we can prepulate the form state.
  $form_state['wrapper_callback'] = 'entity_ui_main_form_defaults';
  $form_state['entity_type'] = $entity_type;
  form_load_include($form_state, 'inc', 'entity', 'includes/entity.ui');

  // Handle cloning. We cannot do that in the wrapper callback as it is too late
  // for changing arguments.
  if ($op == 'clone') {
    $entity = entity_ui_clone_entity($entity_type, $entity);
  }

  // We don't pass the entity type as first parameter, as the implementing
  // module knows the type anyway. However, in order to allow for efficient
  // hook_forms() implementiations we append the entity type as last argument,
  // which the module implementing the form constructor may safely ignore.
  // @see entity_forms()
  $form_state['build_info']['args'] = array(
    $entity,
    $op,
    $entity_type,
  );
  return drupal_build_form($form_id, $form_state);
}