cvterm_class_adder.inc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. *
  4. * @param unknown $entity_type
  5. * @param unknown $entity
  6. * @param unknown $field
  7. * @param unknown $instance
  8. * @param unknown $langcode
  9. * @param unknown $items
  10. * @param unknown $display
  11. */
  12. function tripal_chado_cvterm_class_adder_formatter(&$element, $entity_type, $entity, $field,
  13. $instance, $langcode, $items, $display) {
  14. foreach ($items as $delta => $item) {
  15. // Do nothing, this field is only meant for the form.
  16. }
  17. }
  18. /**
  19. *
  20. */
  21. function tripal_chado_cvterm_class_adder_widget(&$widget, &$form, &$form_state,
  22. $field, $instance, $langcode, $items, $delta, $element) {
  23. // This field has no value field. Just a fieldset for adding new annotation types.
  24. $widget['#element_validate'] = array('tripal_chado_cvterm_class_adder_widget_validate');
  25. $widget['#type'] = 'fieldset';
  26. $widget['#title'] = $element['#title'];
  27. $widget['#description'] = $element['#description'];
  28. $widget['#group'] = 'entity_form_vtabs';
  29. $widget['cvterm_class_adder_instructions'] = array(
  30. '#type' => 'item',
  31. '#markup' => t('You may add annotation types to this form by
  32. providing a vocabulary name in the field below
  33. and clicking the "Add Annotation Type" button. This will add a
  34. new field to the form above for the vocabulary you entered which
  35. will allow users to associate terms from that vocabulary to
  36. this record.'),
  37. );
  38. $options = tripal_get_cv_select_options();
  39. $widget['value'] = array(
  40. '#type' => 'select',
  41. '#title' => t('Vocabulary'),
  42. '#options' => $options,
  43. '#description' => t("Please enter the vocabulary that contains terms
  44. you want to allow users to use for annotations."),
  45. );
  46. // When this button is clicked, the form will be validated and submitted.
  47. // Therefore, we set custom submit and validate functions to override the
  48. // default form submit. In the validate function we set the form_state
  49. // to rebuild the form so the submit function never actually gets called,
  50. // but we need it or Drupal will run the default validate anyway.
  51. // we also set #limit_validation_errors to empty so fields that
  52. // are required that don't have values won't generate warnings.
  53. $widget['cvterm_class_adder_button'] = array(
  54. '#value' => t('Add Annotation Type'),
  55. '#type' => 'submit',
  56. '#name' => 'cvterm_class_adder_button',
  57. '#submit' => array('tripal_chado_cvterm_class_adder_widget_submit'),
  58. '#limit_validation_errors' => array(array($field['field_name'])),
  59. );
  60. }
  61. /**
  62. * Callback function for validating the tripal_chado_cvterm_class_adder_widget.
  63. */
  64. function tripal_chado_cvterm_class_adder_widget_validate($element, &$form_state) {
  65. // Add the new field to the entity
  66. if (array_key_exists('triggering_element', $form_state) and
  67. $form_state['triggering_element']['#name'] == 'cvterm_class_adder_button') {
  68. $form_state['rebuild'] = TRUE;
  69. $field_name = $element['#field_name'];
  70. $entity_type = $element['#entity']->type;
  71. $bundle = $element['#entity']->bundle;
  72. // Get the base table name from the field annotations.
  73. $field = field_info_field($field_name);
  74. $base_table = $field['settings']['base_table'];
  75. // Get the vocabulary.
  76. $cvterm_class_adder = tripal_chado_get_field_form_values($field_name, $form_state);
  77. $cv = chado_generate_var('cv', array('cv_id' => $cvterm_class_adder));
  78. if (!$cv) {
  79. form_set_error(implode('][', $element ['#parents']) . '][value', t("Please select a vocabulary."));
  80. return;
  81. }
  82. $type_field_name = $field_name . '__' . $cv->cv_id;
  83. // The field name is the table name in this case. We want to get the
  84. // primary key as this should be the field that maps th the value.
  85. $schema = chado_get_schema($field_name);
  86. $pkey = $schema['primary key'][0];
  87. $field_info = array(
  88. 'field_type' => 'cvterm',
  89. 'widget_type' => 'tripal_chado_cvterm_widget',
  90. 'field_settings' => array(
  91. 'chado_table' => $field_name,
  92. 'chado_column' => $pkey,
  93. 'base_table' => $base_table,
  94. ),
  95. 'storage' => 'field_chado_storage',
  96. 'widget_settings' => array(),
  97. 'description' => "Annotations from the $cv->name vocabulary",
  98. 'label' => ucfirst(preg_replace('/_/', ' ', $cv->name)),
  99. 'is_required' => FALSE,
  100. // All annotations are unlimited.
  101. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  102. );
  103. tripal_add_bundle_field($type_field_name, $field_info, $entity_type, $bundle);
  104. }
  105. }
  106. /**
  107. * Callback function for submitting the tripal_chado_cvterm_class_adder_widget.
  108. */
  109. function tripal_chado_cvterm_class_adder_widget_submit($element, &$form_state) {
  110. }