chado_linker__cvterm_adder.inc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. /**
  3. * Implements hook_info() for fields.
  4. *
  5. * This is a hook provided by the tripal_chado module for offloading the
  6. * hook_field_info() hook for each field to specify.
  7. *
  8. * The field provides a widget for adding new vocabularies for cvterm
  9. * linker tables. This will allow cvterms to be grouped by vocabulary
  10. * ('category').
  11. *
  12. */
  13. function chado_linker__cvterm_adder_info() {
  14. return array(
  15. 'label' => t('Add an Annotation Type'),
  16. 'description' => t('This record may have any number of types of
  17. annotations. Use this field to first add the type.'),
  18. 'default_widget' => 'chado_linker__cvterm_adder_widget',
  19. 'default_formatter' => 'hidden',
  20. 'settings' => array(),
  21. 'storage' => array(
  22. 'type' => 'field_chado_storage',
  23. 'module' => 'tripal_chado',
  24. 'active' => TRUE
  25. ),
  26. 'no_ui' => TRUE
  27. );
  28. }
  29. /**
  30. * Implements hook_attach_info().
  31. *
  32. * This is a hook provided by the tripal_Chado module. It allows the field
  33. * to specify which bundles it will attach to and to specify thee settings.
  34. *
  35. * @param $entity_type
  36. * @param $entity
  37. * @param $term
  38. *
  39. * @return
  40. * A field array
  41. */
  42. function chado_linker__cvterm_adder_attach_info($entity_type, $bundle, $target) {
  43. $field_info = array();
  44. $table_name = $target['data_table'];
  45. $type_table = $target['type_table'];
  46. $type_field = $target['field'];
  47. $cv_id = $target['cv_id'];
  48. $cvterm_id = $target['cvterm_id'];
  49. // If the linker table does not exists then we don't want to add attach.
  50. $cvterm_table = $table_name . '_cvterm';
  51. if (!chado_table_exists($cvterm_table)) {
  52. return $field_info;
  53. }
  54. // Initialize the field array.
  55. $field_info = array(
  56. 'field_name' => $cvterm_table,
  57. 'field_type' => 'chado_linker__cvterm_adder',
  58. 'widget_type' => 'chado_linker__cvterm_adder_widget',
  59. 'field_settings' => array(
  60. 'base_table' => $table_name,
  61. ),
  62. 'cardinality' => 1,
  63. 'storage' => 'field_chado_storage',
  64. 'widget_settings' => array('display_label' => 1),
  65. 'description' => '',
  66. 'label' => 'Additional Annotation Types',
  67. 'is_required' => 0,
  68. // This feld is never visible so there are no field settings for
  69. // Chado nor the semantiv web.
  70. );
  71. return $field_info;
  72. }
  73. /**
  74. * Implements hook_widget_info.
  75. *
  76. * This is a hook provided by the tripal_chado module for offloading
  77. * the hook_field_widget_info() hook for each field to specify.
  78. */
  79. function chado_linker__cvterm_adder_widget_info() {
  80. return array(
  81. 'label' => t('Add an Annotation'),
  82. 'field types' => array('chado_linker__cvterm_adder'),
  83. );
  84. }
  85. /**
  86. *
  87. */
  88. function chado_linker__cvterm_adder_widget(&$widget, &$form, &$form_state,
  89. $field, $instance, $langcode, $items, $delta, $element) {
  90. // This field has no value field. Just a fieldset for adding new annotation types.
  91. $widget['#element_validate'] = array('chado_linker__cvterm_adder_widget_validate');
  92. $widget['#type'] = 'fieldset';
  93. $widget['#title'] = $element['#title'];
  94. $widget['#description'] = $element['#description'];
  95. $widget['#group'] = 'entity_form_vtabs';
  96. $widget['cvterm_class_adder_instructions'] = array(
  97. '#type' => 'item',
  98. '#markup' => t('You may add annotation types to this form by
  99. providing a vocabulary name in the field below
  100. and clicking the "Add Annotation Type" button. This will add a
  101. new field to the form above for the vocabulary you entered which
  102. will allow users to associate terms from that vocabulary to
  103. this record.'),
  104. );
  105. $options = tripal_get_cv_select_options();
  106. $widget['value'] = array(
  107. '#type' => 'select',
  108. '#title' => t('Vocabulary'),
  109. '#options' => $options,
  110. '#description' => t("Please enter the vocabulary that contains terms
  111. you want to allow users to use for annotations."),
  112. );
  113. // When this button is clicked, the form will be validated and submitted.
  114. // Therefore, we set custom submit and validate functions to override the
  115. // default form submit. In the validate function we set the form_state
  116. // to rebuild the form so the submit function never actually gets called,
  117. // but we need it or Drupal will run the default validate anyway.
  118. // we also set #limit_validation_errors to empty so fields that
  119. // are required that don't have values won't generate warnings.
  120. $widget['cvterm_class_adder_button'] = array(
  121. '#value' => t('Add Annotation Type'),
  122. '#type' => 'submit',
  123. '#name' => 'cvterm_class_adder_button',
  124. '#submit' => array('chado_linker__cvterm_adder_widget_submit'),
  125. '#limit_validation_errors' => array(array($field['field_name'])),
  126. );
  127. }
  128. /**
  129. * Callback function for validating the chado_linker__cvterm_adder_widget.
  130. */
  131. function chado_linker__cvterm_adder_widget_validate($element, &$form_state) {
  132. // Add the new field to the entity
  133. if (array_key_exists('triggering_element', $form_state) and
  134. $form_state['triggering_element']['#name'] == 'cvterm_class_adder_button') {
  135. $form_state['rebuild'] = TRUE;
  136. $field_name = $element['#field_name'];
  137. $entity_type = $element['#entity']->type;
  138. $bundle = $element['#entity']->bundle;
  139. // Get the base table name from the field annotations.
  140. $field = field_info_field($field_name);
  141. $base_table = $field['settings']['base_table'];
  142. // Get the vocabulary.
  143. $cvterm_class_adder = tripal_chado_get_field_form_values($field_name, $form_state);
  144. $cv = chado_generate_var('cv', array('cv_id' => $cvterm_class_adder));
  145. if (!$cv) {
  146. form_set_error(implode('][', $element ['#parents']) . '][value', t("Please select a vocabulary."));
  147. return;
  148. }
  149. $type_field_name = $field_name . '__' . $cv->cv_id;
  150. // The field name is the table name in this case. We want to get the
  151. // primary key as this should be the field that maps th the value.
  152. $schema = chado_get_schema($field_name);
  153. $pkey = $schema['primary key'][0];
  154. $field_info = array(
  155. 'field_type' => 'cvterm',
  156. 'widget_type' => 'tripal_chado_cvterm_widget',
  157. 'field_settings' => array(
  158. 'chado_table' => $field_name,
  159. 'chado_column' => $pkey,
  160. 'base_table' => $base_table,
  161. ),
  162. 'storage' => 'field_chado_storage',
  163. 'widget_settings' => array(),
  164. 'description' => "Annotations from the $cv->name vocabulary",
  165. 'label' => ucfirst(preg_replace('/_/', ' ', $cv->name)),
  166. 'is_required' => FALSE,
  167. // All annotations are unlimited.
  168. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  169. );
  170. tripal_add_bundle_field($type_field_name, $field_info, $entity_type, $bundle);
  171. }
  172. }
  173. /**
  174. * Callback function for submitting the chado_linker__cvterm_adder_widget.
  175. */
  176. function chado_linker__cvterm_adder_widget_submit($element, &$form_state) {
  177. }