function chado_library_form
2.x tripal_library.chado_node.inc | chado_library_form($node, &$form_state) |
3.x tripal_library.chado_node.inc | chado_library_form($node, & |
1.x tripal_library.module | chado_library_form($node) |
Implements hook_form().
When editing or creating a new node of type 'chado_library' we need a form. This function creates the form that will be used for this.
Related topics
File
- tripal_library/
includes/ tripal_library.chado_node.inc, line 47 - Implements the library node content type
Code
function chado_library_form($node, &$form_state) {
$form = array();
// Default values can come in the following ways:
//
// 1) As elements of the $node object: this occurs when editing an existing
// library.
// 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.
$library_id = NULL;
$libraryname = '';
$uniquename = '';
$library_type = '';
$organism_id = '';
$description = '';
// If we are editing an existing node then the library is already part of
// the node
if (property_exists($node, 'library')) {
$library = $node->library;
$library_id = $library->library_id;
$libraryname = $library->name;
$uniquename = $library->uniquename;
$library_type = $library->type_id->cvterm_id;
$organism_id = $library->organism_id->organism_id;
$libprop = chado_get_property(
array('table' => 'library', 'id' => $library->library_id),
array('type_name' => 'Library Description', 'cv_name' => 'library_property')
);
$description = $libprop->value;
// Keep track of the library id if we have. If we do have one then
// this is an update as opposed to an insert.
$form['library_id'] = array(
'#type' => 'value',
'#value' => $library_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)) {
$libraryname = $form_state['values']['libraryname'];
$uniquename = $form_state['values']['uniquename'];
$library_type = $form_state['values']['library_type'];
$organism_id = $form_state['values']['organism_id'];
$description = $form_state['values']['description'];
}
// 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'])) {
$libraryname = $form_state['input']['libraryname'];
$uniquename = $form_state['input']['uniquename'];
$library_type = $form_state['input']['library_type'];
$organism_id = $form_state['input']['organism_id'];
$description = $form_state['input']['description'];
}
$form['libraryname'] = array(
'#type' => 'textfield',
'#title' => t('Library Name'),
'#description' => t('Please enter the name for this library. Library names should be recognizable but do not need to be unique.'),
'#required' => TRUE,
'#default_value' => $libraryname,
);
$form['uniquename'] = array(
'#type' => 'textfield',
'#title' => t('Unique Name'),
'#description' => t('Please enter a unique name for this library. This can be any value used to uniquely identify a library.'),
'#required' => TRUE,
'#default_value' => $uniquename,
);
// Get the list of library types.
$lt_cv = tripal_get_default_cv("library", "type_id");
$types = tripal_get_cvterm_default_select_options('library', 'type_id', 'library types');
$types[0] = 'Select a Type';
$lt_message = tripal_set_message("To add additional items to the library type drop down list,
add a term to the " .
l($lt_cv->name . " controlled vocabulary",
"admin/tripal/chado/tripal_cv/cv/" . $lt_cv->cv_id . "/cvterm/add",
array('attributes' => array('target' => '_blank'))
),
TRIPAL_INFO, array('return_html' => TRUE)
);
$form['library_type'] = array(
'#title' => t('Library Type'),
'#type' => t('select'),
'#description' => t("Choose the library type."),
'#required' => TRUE,
'#default_value' => $library_type,
'#options' => $types,
'#suffix' => $lt_message,
);
// Get the list of organisms.
$sql = "SELECT * FROM {organism}";
$org_rset = chado_query($sql);
$organisms = array();
$organisms[''] = '';
while ($organism = $org_rset->fetchObject()) {
$organisms[$organism->organism_id] =
"$organism->genus $organism->species ($organism->common_name)";
}
$form['organism_id'] = array(
'#title' => t('Organism'),
'#type' => t('select'),
'#description' => t("Choose the organism with which this library is associated."),
'#required' => TRUE,
'#default_value' => $organism_id,
'#options' => $organisms,
);
$form['description'] = array(
'#type' => 'text_format',
'#title' => t('Library Description'),
'#description' => t('A brief description of the library'),
'#required' => TRUE,
'#default_value' => $description,
);
// PROPERTIES FORM.
//---------------------------------------------
$prop_cv = tripal_get_default_cv('libraryprop', 'type_id');
$cv_id = $prop_cv ? $prop_cv->cv_id : NULL;
$details = array(
// The name of the prop table.
'property_table' => 'libraryprop',
// The value of library_id for this record.
'chado_id' => $library_id,
// The cv.cv_id of the cv governing libraryprop.type_id.
'cv_id' => $cv_id,
);
// If the default is the 'library_property' vocabulary then we want
// to exclude the 'Library Description' term since it has it's own form
// element above
if ($prop_cv->name == 'library_property') {
// Generate our own select list so we can exclude the description element
$select_options = array();
$cv_result = chado_select_record('cv', array('cv_id'), array('name' => 'library_property'));
$cv_id = $cv_result[0]->cv_id;
$select_options = tripal_get_cvterm_select_options($cv_id);
$descrip_id = array_search('Library Description', $select_options);
unset($select_options[$descrip_id]);
$details['select_options'] = $select_options;
}
// Adds the form elements to your current form
chado_add_node_form_properties($form, $form_state, $details);
// ADDITIONAL DBXREFS FORM
//---------------------------------------------
$details = array(
// The name of the _dbxref table.
'linking_table' => 'library_dbxref',
// The name of the key in your base chado table.
'base_foreign_key' => 'library_id',
// The value of library_id for this record.
'base_key_value' => $library_id
);
// Adds the form elements to your current form
chado_add_node_form_dbxrefs($form, $form_state, $details);
return $form;
}