organism_id.inc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /**
  3. *
  4. * @param $entity_type
  5. * @param $entity
  6. * @param $field
  7. * @param $instance
  8. * @param $langcode
  9. * @param $items
  10. * @param $display
  11. * @return string
  12. */
  13. function tripal_chado_organism_select_formatter(&$element, $entity_type, $entity, $field,
  14. $instance, $langcode, $items, $display) {
  15. foreach ($items as $delta => $item) {
  16. $organism = chado_select_record('organism', array('genus', 'species'), array('organism_id' => $item['value']));
  17. $content = '<i>' . $organism[0]->genus .' ' . $organism[0]->species . '</i>';
  18. $element[$delta] = array(
  19. '#type' => 'markup',
  20. '#markup' => $content,
  21. );
  22. }
  23. }
  24. /**
  25. *
  26. * @param $field_name
  27. * @param $widget
  28. * @param $form
  29. * @param $form_state
  30. * @param $field
  31. * @param $instance
  32. * @param $langcode
  33. * @param $items
  34. * @param $delta
  35. * @param $element
  36. */
  37. function tripal_chado_organism_select_widget(&$widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element) {
  38. $options = tripal_get_organism_select_options(FALSE);
  39. $widget['value'] = array(
  40. '#type' => 'select',
  41. '#title' => $element['#title'],
  42. '#description' => $element['#description'],
  43. '#options' => $options,
  44. '#default_value' => count($items) > 0 ? $items[0]['value'] : 0,
  45. '#required' => $element['#required'],
  46. '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
  47. '#delta' => $delta,
  48. '#element_validate' => array('tripal_chado_organism_select_widget_validate'),
  49. );
  50. $widget['add_organism'] = array(
  51. '#type' => 'item',
  52. '#markup' => l('Add a new species', 'admin/content/bio_data/add/species', array('attributes' => array('target' => '_blank'))),
  53. );
  54. }
  55. /**
  56. * Callback function for validating the tripal_chado_organism_select_widget.
  57. */
  58. function tripal_chado_organism_select_widget_validate($element, &$form_state) {
  59. $field_name = $element['#parents'][0];
  60. // If the form ID is field_ui_field_edit_form, then the user is editing the
  61. // field's values in the manage fields form of Drupal. We don't want
  62. // to validate it as if it were being used in a data entry form.
  63. if ($form_state['build_info']['form_id'] =='field_ui_field_edit_form') {
  64. return;
  65. }
  66. $organism_id = tripal_chado_get_field_form_values($field_name, $form_state);
  67. if (!$organism_id) {
  68. form_error($element, t("Please specify an organism."));
  69. }
  70. }
  71. /**
  72. * Provides a settings form for the formatter.
  73. *
  74. * This function is equiavlent to the hook_field_formatter_settings_form()
  75. * hook.
  76. *
  77. * @param $field
  78. * The field structure being configured.
  79. * @param $instance
  80. * The instance structure being configured.
  81. * @param $view_mode
  82. * The view mode being configured.
  83. * @param $form
  84. * The (entire) configuration form array, which will usually have no use here.
  85. * @param $form_state
  86. * The form state of the (entire) configuration form.
  87. */
  88. function tripal_chado_organism_select_formatter_form($field, $instance,
  89. $view_mode, $form, &$form_state) {
  90. $display = $instance['display'][$view_mode];
  91. $settings = $display['settings'];
  92. $element = array();
  93. $term = NULL;
  94. $bundle = NULL;
  95. // Check to see if the organism bundle exists
  96. $term = tripal_load_term_entity(array(
  97. 'namespace' => $field['settings']['semantic_web']['ns'],
  98. 'accession' => $field['settings']['semantic_web']['type']
  99. ));
  100. if ($term) {
  101. $bundle = tripal_load_bundle_entity(array('term_id' => $term->id));
  102. }
  103. $element['instructions'] = array(
  104. '#type' => 'item',
  105. '#markup' => 'Please provide the format for viewing the organism. You
  106. can specify the format using tokens that correspond to each field'
  107. );
  108. $element['field_display'] = array(
  109. '#type' => 'textfield',
  110. '#title' => 'Display Format',
  111. '#description' => t('Provide a mixture of text and/or tokens for the format.
  112. For example: [organism__genus] [organism__species]. When displayed
  113. the tokens will be replaced with the actual value.'),
  114. '#default_value' => '[organism__genus] [organism__species]',
  115. );
  116. $element['field_display_entity'] = array(
  117. '#type' => 'checkbox',
  118. '#title' => 'Display teaser if available',
  119. '#description' => t('If the organism that this field is associated with is
  120. a published page then display the teaser rather use the display
  121. format above.'),
  122. );
  123. $element['tokens'] = array(
  124. '#type' => 'fieldset',
  125. '#collapsed' => TRUE,
  126. '#collapsible' => TRUE,
  127. '#title' => 'Available Tokens'
  128. );
  129. $headers = array('Token', 'Description');
  130. $rows = array();
  131. $tokens = tripal_get_tokens($bundle);
  132. foreach ($tokens as $token) {
  133. $rows[] = array(
  134. $token['token'],
  135. $token['description'],
  136. );
  137. }
  138. $table_vars = array(
  139. 'header' => $headers,
  140. 'rows' => $rows,
  141. 'attributes' => array(),
  142. 'sticky' => FALSE,
  143. 'caption' => '',
  144. 'colgroups' => array(),
  145. 'empty' => 'There are no tokens',
  146. );
  147. $project_details = theme('table', $table_vars);
  148. $element['tokens']['list'] = array(
  149. '#type' => 'item',
  150. '#markup' => theme_table($table_vars),
  151. );
  152. return $element;
  153. }