You are here

function entityform_edit_form in Entityform 7.2

Same name and namespace in other branches
  1. 7 entityform.admin.inc \entityform_edit_form()

Form callback: create or edit a entityform.

Parameters

$entityform: The entityform object to edit or for a create form an empty entityform object with only a entityform type defined.

2 string references to 'entityform_edit_form'
entityform_entity_rules_info in ./entityform.module
Implements hook_entity_rules_info().
entityform_forms in ./entityform.module
Implements hook_forms().

File

./entityform.admin.inc, line 361
Entityform editing UI.

Code

function entityform_edit_form($form, &$form_state, $entityform, $mode = 'submit', $form_context = 'page') {
  global $user;
  $entityform_type = entityform_type_load($entityform->type);
  $is_review = FALSE;
  if (!empty($form_state['values']['op']) && $form_state['values']['op'] == t('Change')) {
    $entityform = $form_state['entityform_preview_entity'];
    unset($form_state['entityform_preview_entity']);
  }
  else {
    if (!empty($entityform_type->data['preview_page']) && !empty($form_state['entityform_preview_entity'])) {
      $entityform = $form_state['entityform_preview_entity'];
      $entityform->uid = $user->uid;
      $is_review = TRUE;
      $form['review'] = entity_get_controller('entityform')
        ->view(array(
        $entityform->entityform_id => $entityform,
      ), 'review', NULL, TRUE);
      drupal_set_message(t('Please review your submission'));
    }
  }

  // Add the field related form elements.
  $form_state['entityform'] = $entityform;
  $form_state['entityform_form_mode'] = $mode;
  if (!$is_review) {
    field_attach_form('entityform', $entityform, $form, $form_state);
  }

  // Add CSS classes to the form for styling.
  $form['#attributes']['class'][] = 'entityform';
  $form['#attributes']['class'][] = 'entitytype-' . $entityform->type . '-form';
  $form['actions'] = array(
    '#type' => 'actions',
    '#attributes' => array(
      'class' => array(
        'form-actions',
      ),
    ),
    '#weight' => 400,
  );

  // We add the form's #submit array to this button along with the actual submit
  // handler to preserve any submit handlers added by a form callback_wrapper.
  $submit = array();
  if (!empty($form['#submit'])) {
    $submit += $form['#submit'];
  }
  if ($mode == 'submit') {

    // Determine if we are on Preview Form page.
    $entity_info = entity_get_info('entityform_type');

    // Create Draft button so that other modules can activate but don't give access by default.
    $form['actions']['save'] = array(
      '#type' => 'submit',
      '#value' => $entityform_type
        ->get_prop('draft_button_text'),
      // Causes errors.

      //'#limit_validation_errors' => array(),
      '#entityform_draft' => '1',
      '#submit' => $submit + array(
        'entityform_edit_form_submit',
      ),
      '#access' => FALSE,
    );

    // If form is draftable and user is logged in add draft button.
    if ($entityform_type->data['draftable'] && !empty($user->uid)) {
      if (!$is_review) {
        $form['actions']['save']['#access'] = TRUE;
      }
    }
    if ($is_review) {
      $form['actions']['change'] = array(
        '#type' => 'submit',
        '#value' => t('Change'),
        '#entityform_draft' => 0,
        '#entityform_change' => 1,
        '#submit' => $submit + array(
          'entityform_edit_form_submit',
        ),
      );
    }
    $form['actions']['submit'] = array(
      '#type' => 'submit',
      '#value' => $entityform_type
        ->get_prop('submit_button_text'),
      '#entityform_draft' => 0,
      '#submit' => $submit + array(
        'entityform_edit_form_submit',
      ),
      '#entityform_after_review' => $is_review,
    );
  }
  else {
    $form['user_info'] = array(
      '#type' => 'markup',
      '#markup' => _entityform_get_submit_info($entityform),
      '#weight' => -200,
      '#prefix' => '<div class="submitted">',
      '#suffix' => '</div>',
    );
    $form['actions']['save'] = array(
      '#type' => 'submit',
      '#value' => t('Save Changes'),
      '#entityform_draft' => '0',
      '#submit' => $submit + array(
        'entityform_edit_form_submit',
      ),
    );
  }
  if (!empty($entityform->name)) {
    $form['actions']['delete'] = array(
      '#type' => 'submit',
      '#value' => t('Delete entityform'),
      '#suffix' => l(t('Cancel'), 'admin/structure/entityforms'),
      '#submit' => $submit + array(
        'entityform_form_submit_delete',
      ),
      '#weight' => 45,
    );
  }

  // We append the validate handler to #validate in case a form callback_wrapper
  // is used to add validate handlers earlier.
  $form['#validate'][] = 'entityform_edit_form_validate';

  // Make sure the proper files are loaded if the form has an ajax callback.
  // This happens in image field upload for instance.
  $form['#after_build'][] = 'entityform_load_required_entityform_admin';
  $form['#after_build'][] = 'entityform_clear_required';
  $form['#validate'][] = 'entityform_set_required';
  if ($form_context == 'preview') {
    _entityform_type_modify_preview_form($form);
  }
  return $form;
}