chado_linker__cvterm_adder.inc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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_widget_info.
  30. *
  31. * This is a hook provided by the tripal_chado module for offloading
  32. * the hook_field_widget_info() hook for each field to specify.
  33. */
  34. function chado_linker__cvterm_adder_widget_info() {
  35. return array(
  36. 'label' => t('Add an Annotation'),
  37. 'field types' => array('chado_linker__cvterm_adder'),
  38. );
  39. }
  40. /**
  41. *
  42. */
  43. function chado_linker__cvterm_adder_widget(&$widget, &$form, &$form_state,
  44. $field, $instance, $langcode, $items, $delta, $element) {
  45. // This field has no value field. Just a fieldset for adding new annotation types.
  46. $widget['#element_validate'] = array('chado_linker__cvterm_adder_widget_validate');
  47. $widget['#type'] = 'fieldset';
  48. $widget['#title'] = $element['#title'];
  49. $widget['#description'] = $element['#description'];
  50. $widget['#group'] = 'entity_form_vtabs';
  51. $widget['cvterm_class_adder_instructions'] = array(
  52. '#type' => 'item',
  53. '#markup' => t('You may add annotation types to this form by
  54. providing a vocabulary name in the field below
  55. and clicking the "Add Annotation Type" button. This will add a
  56. new field to the form above for the vocabulary you entered which
  57. will allow users to associate terms from that vocabulary to
  58. this record.'),
  59. );
  60. $options = tripal_get_cv_select_options();
  61. $widget['value'] = array(
  62. '#type' => 'select',
  63. '#title' => t('Vocabulary'),
  64. '#options' => $options,
  65. '#description' => t("Please enter the vocabulary that contains terms
  66. you want to allow users to use for annotations."),
  67. );
  68. // When this button is clicked, the form will be validated and submitted.
  69. // Therefore, we set custom submit and validate functions to override the
  70. // default form submit. In the validate function we set the form_state
  71. // to rebuild the form so the submit function never actually gets called,
  72. // but we need it or Drupal will run the default validate anyway.
  73. // we also set #limit_validation_errors to empty so fields that
  74. // are required that don't have values won't generate warnings.
  75. $widget['cvterm_class_adder_button'] = array(
  76. '#value' => t('Add Annotation Type'),
  77. '#type' => 'submit',
  78. '#name' => 'cvterm_class_adder_button',
  79. '#submit' => array('chado_linker__cvterm_adder_widget_submit'),
  80. '#limit_validation_errors' => array(array($field['field_name'])),
  81. );
  82. }
  83. /**
  84. * Callback function for validating the chado_linker__cvterm_adder_widget.
  85. */
  86. function chado_linker__cvterm_adder_widget_validate($element, &$form_state) {
  87. // Add the new field to the entity
  88. if (array_key_exists('triggering_element', $form_state) and
  89. $form_state['triggering_element']['#name'] == 'cvterm_class_adder_button') {
  90. $form_state['rebuild'] = TRUE;
  91. $field_name = $element['#field_name'];
  92. $entity_type = $element['#entity']->type;
  93. $bundle = $element['#entity']->bundle;
  94. // Get the base table name from the field annotations.
  95. $field = field_info_field($field_name);
  96. $base_table = $field['settings']['base_table'];
  97. // Get the vocabulary.
  98. $cvterm_class_adder = tripal_chado_get_field_form_values($field_name, $form_state);
  99. $cv = chado_generate_var('cv', array('cv_id' => $cvterm_class_adder));
  100. if (!$cv) {
  101. form_set_error(implode('][', $element ['#parents']) . '][value', t("Please select a vocabulary."));
  102. return;
  103. }
  104. $type_field_name = $field_name . '__' . $cv->cv_id;
  105. // The field name is the table name in this case. We want to get the
  106. // primary key as this should be the field that maps th the value.
  107. $schema = chado_get_schema($field_name);
  108. $pkey = $schema['primary key'][0];
  109. $field_info = array(
  110. 'field_type' => 'cvterm',
  111. 'widget_type' => 'tripal_chado_cvterm_widget',
  112. 'field_settings' => array(
  113. 'chado_table' => $field_name,
  114. 'chado_column' => $pkey,
  115. 'base_table' => $base_table,
  116. ),
  117. 'storage' => 'field_chado_storage',
  118. 'widget_settings' => array(),
  119. 'description' => "Annotations from the $cv->name vocabulary",
  120. 'label' => ucfirst(preg_replace('/_/', ' ', $cv->name)),
  121. 'is_required' => FALSE,
  122. // All annotations are unlimited.
  123. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  124. );
  125. tripal_add_bundle_field($type_field_name, $field_info, $entity_type, $bundle);
  126. }
  127. }
  128. /**
  129. * Callback function for submitting the chado_linker__cvterm_adder_widget.
  130. */
  131. function chado_linker__cvterm_adder_widget_submit($element, &$form_state) {
  132. }