You are here

function casetracker_form_alter in Case Tracker 5

Same name and namespace in other branches
  1. 6 casetracker.module \casetracker_form_alter()
  2. 7 casetracker.module \casetracker_form_alter()

Implementation of hook_form_alter().

File

./casetracker.module, line 1291
Enables the handling of projects and their cases.

Code

function casetracker_form_alter($form_id, &$form) {
  $node = !empty($form['nid']['#value']) ? node_load($form['nid']['#value']) : NULL;

  // add case options to our basic case type.
  if (in_array(str_replace('_node_form', '', $form_id), variable_get('casetracker_case_node_types', array(
    'casetracker_basic_case',
  )), TRUE)) {
    $count = db_result(db_query(db_rewrite_sql("SELECT COUNT(*) AS count FROM {node} n WHERE n.type IN (" . str_pad('', count(array_filter(variable_get('casetracker_project_node_types', array(
      'casetracker_basic_project',
    )))) * 5 - 1, "'%s',") . ")"), array_filter(variable_get('casetracker_project_node_types', array(
      'casetracker_basic_project',
    )))));
    if ($count == 0) {
      drupal_set_message(t('You must create a project before adding cases.'), 'error');
      return;
    }

    // we can't make a link to a project here because the admin may have assigned more than one node type as project usable.
    $form = casetracker_case_form_common($form, arg(3));

    // proceed as normal with modifications.
  }

  // add case options to the comment form.
  if ($form_id == 'comment_form' && in_array($node->type, variable_get('casetracker_case_node_types', array(
    'casetracker_basic_case',
  )), TRUE)) {
    $form = casetracker_case_form_common($form);
    $form['casetracker_case_information']['case_title'] = array(
      '#type' => 'textfield',
      '#title' => t('Title'),
      '#required' => TRUE,
      '#weight' => -10,
      '#default_value' => isset($node->title) ? $node->title : NULL,
      '#prefix' => '<div id="comment-case-title">',
      '#suffix' => '</div>',
    );

    // we use 'pid' for a project ID, but the comment form uses 'pid' for
    // the parent comment (in a reply). we'll change ours to 'prid'. sigh.
    $form['casetracker_project_information']['prid'] = $form['casetracker_project_information']['pid'];
    unset($form['casetracker_project_information']['pid']);

    // cater to this in casetracker_comment().
    // necessary for our casetracker_comment() callback.
    $form['nid'] = array(
      '#type' => 'hidden',
      '#value' => $node->nid,
    );
    $form['case_number'] = array(
      '#type' => 'hidden',
      '#value' => $node->case_number,
    );
    $form['revision_id'] = array(
      '#type' => 'hidden',
      '#value' => $node->vid,
    );
  }
}