chado_linker__cvterm_adder.inc 6.6 KB

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