You are here

function casetracker_case_edit_form in Case Tracker 7.2

Form callback: create or edit a casetracker_case.

Parameters

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

1 string reference to 'casetracker_case_edit_form'
casetracker_case_form_wrapper in ./casetracker_case.inc
Form callback wrapper: create or edit a casetracker_case.

File

./casetracker_case.inc, line 169
This file concentrates all general functionality related to Cases in Case Tracker, leaving to the other files in /admin the stuff related to CRUD and structural configuration

Code

function casetracker_case_edit_form($form, &$form_state, $case) {
  $amount_of_projects = _casetracker_project_get_amount();
  if ($amount_of_projects == 0) {
    drupal_set_message(t('You need to have at least 1 project to relate your cases to. If you wish, you can go to the <a href="@link">projects creation page</a> now to create one.', array(
      '@link' => base_path() . 'admin/content/casetracker/projects/add',
    )), 'error');
  }
  casetracker_case_set_breadcrumb();
  if (!empty($case->cid)) {
    drupal_set_title(t('@case (editing case info)', array(
      '@case' => $case->title,
    )), PASS_THROUGH);
  }

  // Add the default field elements.
  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Title'),
    '#default_value' => isset($case->title) ? $case->title : '',
    '#maxlength' => 255,
    '#required' => TRUE,
    '#weight' => -25,
  );

  // Add the field related form elements.
  $form_state['casetracker_case'] = $case;
  field_attach_form('casetracker_case', $case, $form, $form_state);

  // If the entity is being created, try to prepopulate the project reference
  // from the URL argument
  if (isset($case->is_new) && $case->is_new) {

    // If the project was informed via URL argument, prepopulate the field
    $pid = arg(1);
    if (!empty($pid)) {
      $project = casetracker_project_load($pid);
      if ($project) {
        $case_wrapper = entity_metadata_wrapper('casetracker_case', $case);
        $casetracker_case_entity_info = $case_wrapper
          ->entityInfo();
        $selected_project = $project->title . ' (' . $pid . ')';
        $form['field_casetracker_project_ref']['und'][0]['target_id']['#default_value'] = $selected_project;
        drupal_set_title(t('Add @bundle to project "@title"', array(
          '@title' => $project->title,
          '@bundle' => $casetracker_case_entity_info['bundles'][$case->type]['label'],
        )));
      }
    }
  }
  $form['uid'] = array(
    '#type' => 'value',
    '#value' => $case->uid,
  );
  $form['actions'] = array(
    '#type' => 'container',
    '#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'];
  }
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save case'),
    '#submit' => $submit + array(
      'casetracker_case_edit_form_submit',
    ),
  );

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