function chado_example_form

2.x tripal_example.chado_node.inc chado_example_form($node, &$form_state)

Implementation of hook_form()

Creates the form for editing or inserting a record

File

tripal_example/includes/tripal_example.chado_node.inc, line 114
This file should contain all Drupal hooks for interacting with nodes.

Code

function chado_example_form($node, &$form_state) {

  // EXPLANATION: This function should construct a form array that is used by
  // Drupal to construct a form for inserting or editing our new node type.
  // See this page for information about the Form API:
  // https://api.drupal.org/api/drupal/includes!form.inc/group/form_api/7
  //
  // The code below is laid out in the following order
  // 1) Set default values
  // 2) Add form elements used by this node type
  // 3) Use the Tripal API to add form elements for properties,
  //    dbxref's and relationships
  //
  // For the example code below we assume that the fake 'example' table only has
  // a uniquename, organism_id, type_id and example_id.

  $form = array();

  // Default values can come in the following ways:
  //
  // 1) as elements of the $node object. This occurs when editing an existing
  //    example
  // 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


  // SET FORM DEFAULTS
  //---------------------------------------------
  $example = null; // holds the example object record
  $example_id = null; // when editing an example record we'll have an example_id

  // initialize the defaults for the form fields
  $uniquename = '';
  $example_type = '';
  $organism_id = '';
  $description = '';

  // if we are editing an existing node then the 'example' record from Chado
  // is already part of the node, so we set the defaults from that object
  if (property_exists($node, 'example')) {
    $example = $node->example;
    $example_id = $example->example_id;
    $uniquename = $example->uniquename;
    $description = $example->description;
    $organism_id = $example->organism_id;

    // keep track of the example id
    $form['example_id'] = array(
      '#type' => 'value',
      '#value' => $example_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)) {
    $uniquename = $form_state['values']['uniquename'];
    $example_type = $form_state['values']['example_type'];
    $description = $form_state['values']['description'];
    $organism_id = $form_state['values']['organism_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'])) {
    $uniquename = $form_state['input']['uniquename'];
    $example_type = $form_state['input']['example_type'];
    $organism_id = $form_state['input']['organism_id'];
  }


  // FORM ELEMENTS
  //---------------------------------------------
  $form['uniquename'] = array(
    '#type' => 'textfield',
    '#title' => t('Unique Name'),
    '#required' => TRUE,
    '#default_value' => $uniquename,
    '#description' => t('Enter a unique name for this example. This name must be unique.'),
    '#maxlength' => 255
  );

  // for the type_id we want to use the default vocabulary so that this field
  // can have auto-complete functionality
  $type_cv = tripal_get_default_cv('example', 'type_id');
  $cv_id = $type_cv->cv_id;
  $form['example_type'] = array(
    '#title' => t('Example Type'),
    '#type' => 'textfield',
    '#description' => t("Choose the example type (e.g. Test Type)."),
    '#required' => TRUE,
    '#default_value' => $example_type,
    '#autocomplete_path' => "admin/tripal/chado/tripal_cv/cvterm/auto_name/$cv_id",
  );

  // add a select box of organisms
  $organisms = tripal_get_organism_select_options();
  $form['organism_id'] = array(
    '#title' => t('Organism'),
    '#type' => t('select'),
    '#description' => t("Choose the organism with which this example is associated"),
    '#required' => TRUE,
    '#default_value' => $organism_id,
    '#options' => $organisms,
  );

  $form['description'] = array(
    '#type' => 'text_format',
    '#title' => t('Description'),
    '#required' => TRUE,
    '#default_value' => $description,
    '#description' => t('Enter a description for this example.'),
  );

  // PROPERTIES FORM
  //---------------------------------------------
  // If there is a exampleprop table and you want to allow users to add/remove
  // entries from it through your node form then add this section to your own
  // node form
  $prop_cv = tripal_get_default_cv('exampleprop', 'type_id');
  $cv_id = $prop_cv ? $prop_cv->cv_id : NULL;
  $details = array(
    // the name of the prop table
    'property_table' => 'exampleprop',
    // the value of example_id for this record
    'chado_id' => $example_id,
    // the cv.cv_id of the cv governing exampleprop.type_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
  //---------------------------------------------
  // If there is a example_dbxref table and you want to allow users to
  // add/remove entries from it through your node form then add this section to
  // your own node form
  $details = array(
    // the name of the _dbxref table
    'linking_table' => 'example_dbxref',
    // the name of the key in your base chado table
    'base_foreign_key' => 'example_id',
    // the value of example_id for this record
    'base_key_value' => $example_id
  );
  // Adds the form elements to your current form
  chado_add_node_form_dbxrefs($form, $form_state, $details);

  // RELATIONSHIPS FORM
  //---------------------------------------------
  // If there is a example_relationship table and you want to allow users to
  // add/remove entries from it through your node form then add this section to
  // your own node form
  $rels_cv = tripal_get_default_cv('example_relationship', 'type_id');
  $cv_id = $rels_cv ? $rels_cv->cv_id : NULL;
  $details = array(
    // the name of the _relationship table
    'relationship_table' => 'example_relationship',
    // the name of your chado base table
    'base_table' => 'example',
    // the name of the key in your base chado table
    'base_foreign_key' => 'example_id',
    // the value of example_id for this record
    'base_key_value' => $example_id,
    // the human-readable name of your node type
    'nodetype' => 'example',
    // the cv.cv_id of the cv governing example_relationship.type_id
    'cv_id' => $cv_id
  );
  // Adds the form elements to your current form
  chado_add_node_form_relationships($form, $form_state, $details);

  // return the form
  return $form;
}