function chado_organism_form

2.x tripal_organism.chado_node.inc chado_organism_form($node, $form_state)
3.x tripal_organism.chado_node.inc chado_organism_form($node, $form_state)
1.x tripal_organism.module chado_organism_form($node, $param)

Implement hook_form().

When editing or creating a new node of type 'chado_organism' we need a form. This function creates the form that will be used for this.

Related topics

File

legacy/tripal_organism/includes/tripal_organism.chado_node.inc, line 104
Implements the organims node content type

Code

function chado_organism_form($node, $form_state) {
  $form = array();
  $chado_version = chado_get_version(TRUE);

  // Default values can come in the following ways:
  //
  // 1) As elements of the $node object.  This occurs when editing an existing
  //    organism.
  // 2) In the $form_state['values'] array which occurs on a failed validation
  //    or ajax callbacks from non submit form elements
  // 3) In the $form_state['input'[ array which occurs on ajax callbacks from
  //    submit form elements and the form is being rebuilt
  //
  // Set form field defaults.
  $organism = NULL;
  $organism_id = NULL;
  $abbreviation = '';
  $genus = '';
  $species = '';
  $common_name = '';
  $description = '';
  $infraspecific_name = '';
  $type_id = '';

  // We have a file upload element on the form soe we need the multipart
  // encoding type
  $form['#attributes']['enctype'] = 'multipart/form-data';

  // If the organism is part of the node object then we are editing. If not
  // we are inserting
  if (property_exists($node, 'organism')) {
    $organism = $node->organism;

    // Add in the comment since it is a text field and may not be included if
    // too big
    $organism = chado_expand_var($organism, 'field', 'organism.comment');

    // Get form defaults.
    $abbreviation = $organism->abbreviation;
    $genus = $organism->genus;
    $species = $organism->species;
    $common_name = $organism->common_name;
    $description = $organism->comment;
    // The infraspecific and type_id fields are new to Chado v1.3
    if ($chado_version > 1.2) {
      $infraspecific_name = $organism->infraspecific_name;
      $type_id = $organism->type_id->cvterm_id;
    }

    // Set the organism_id in the form.
    $form['organism_id'] = array(
      '#type' => 'value',
      '#value' => $organism->organism_id,
    );
    $organism_id = $organism->organism_id;
  }

  // If we are re constructing the form from a failed validation or ajax
  // callback then use the $form_state['values'] values.
  if (array_key_exists('values', $form_state) and isset($form_state['values']['genus'])) {
    $abbreviation = $form_state['values']['abbreviation'];
    $genus = $form_state['values']['genus'];
    $species = $form_state['values']['species'];
    $common_name = $form_state['values']['common_name'];
    $description = $form_state['values']['comment'];
    if ($chado_version > 1.2) {
      $infraspecific_name = $form_state['values']['infraspecific_name'];
      $type_id = $form_state['values']['type_id'];
    }
  }
  // If we are re building the form from after submission (from ajax call) then
  // the values are in the $form_state['input'] array.
  if (array_key_exists('input', $form_state) and !empty($form_state['input'])) {
    $abbreviation = $form_state['input']['abbreviation'];
    $genus = $form_state['input']['genus'];
    $species = $form_state['input']['species'];
    $common_name = $form_state['input']['common_name'];
    $description = $form_state['input']['comment'];
    if ($chado_version > 1.2) {
      $infraspecific_name = $form_state['input']['infraspecific_name'];
      $type_id = $form_state['input']['type_id'];
    }
  }

  $form['genus'] = array(
    '#type' => 'textfield',
    '#title' => t('Genus'),
    '#required' => TRUE,
    '#default_value' => $genus,
  );
  $form['species'] = array(
    '#type' => 'textfield',
    '#title' => t('Species'),
    '#required' => TRUE,
    '#default_value' => $species,
  );
  // The infraspecific and type_id fields are new to Chado v1.3.
  if ($chado_version > 1.2) {

    $options = array('0' => 'Select a rank');
    $cv = tripal_get_cv(array('name' => 'taxonomic_rank'));
    if (!$cv) {
      drupal_set_message('The taxonomic_rank vocabulary cannot be found, thus selects for "rank" are not available.', 'warning');
    }
    else {
      $terms = tripal_get_cvterm_select_options($cv->cv_id);

      // Unfortunately the taxonomic_rank vocabulary is not properly organized
      // such that we only include terms below 'species'. Therefore we will
      // just list them here and hope we haven't missed one.
      $valid_terms = array('subspecies', 'varietas', 'subvariety', 'forma', 'subforma');
      foreach ($terms as $cvterm_id => $name) {
        if (in_array($name, $valid_terms)) {
          $options[$cvterm_id] = $name;
        }
      }
    }

    $form['type_id'] = array(
      '#type' => 'select',
      '#title' => t('Infraspecific Rank'),
      '#options' => $options,
      '#default_value' => $type_id,
      '#description' => t('The scientific name for any taxon
        below the rank of species. This field is used for constructing the
        full infraspecific name for the organism.')
    );

    $form['infraspecific_name'] = array(
      '#type' => 'textfield',
      '#title' => t('Infraspecific Name'),
      '#default_value' => $infraspecific_name,
      '#description' => t("The infraspecific name for this organism. When
          diplaying the full taxonomic name, this field is appended to the
          genus, species and rank."),
    );
  }
  $form['abbreviation'] = array(
    '#type' => 'textfield',
    '#title' => t('Abbreviation'),
    '#default_value' => $abbreviation,
    '#descriptoin' => t('A short abbreviation for this species (e.g. O.sativa)'),
  );
  $form['common_name'] = array(
    '#type' => 'textfield',
    '#title' => t('Common Name'),
    '#default_value' => $common_name,
  );
  $form['description'] = array(
    '#type' => 'text_format',
    '#rows' => 15,
    '#title' => t('Description'),
    '#default_value' => $description,
  );

  $form['organism_image'] = array(
    '#type' => 'managed_file',
    '#title' => t('Organism Image'),
    '#description' => t('Add an image to display for this organism.'),
    '#progress_indicator' => 'bar',
    '#upload_location' => 'public://tripal/tripal_organism/images/',
  );

  // PROPERTIES FORM
  //---------------------------------------------
  $prop_cv = tripal_get_default_cv('organismprop', 'type_id');
  $cv_id = $prop_cv ? $prop_cv->cv_id : NULL;
  $details = array(
    'property_table' => 'organismprop',
    'chado_id' => $organism_id,
    'cv_id' => $cv_id
  );
  // Adds the form elements to your current form
  chado_add_node_form_properties($form, $form_state, $details);

  // ADDITIONAL DBXREFS FORM
  //---------------------------------------------
  $details = array(
    'linking_table' => 'organism_dbxref',
    'base_foreign_key' => 'organism_id',
    'base_key_value' => $organism_id
  );
  // Adds the form elements to your current form.
  chado_add_node_form_dbxrefs($form, $form_state, $details);

  return $form;
}