chado_linker__cvterm_adder.inc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. class chado_linker__cvterm_adder extends TripalField {
  3. // The default lable for this field.
  4. public static $default_label = 'Add an Annotation Type';
  5. // The default description for this field.
  6. public static $default_description = 'This record may have any number of types of
  7. annotations. Use this field to first add the type.';
  8. // Add any default settings elements. If you override the globalSettingsForm()
  9. // or the instanceSettingsForm() functions then you need to be sure that
  10. // any settings you want those functions to manage are listed in this
  11. // array.
  12. public static $default_settings = array(
  13. 'chado_table' => '',
  14. 'chado_column' => '',
  15. 'base_table' => '',
  16. 'semantic_web' => '',
  17. );
  18. // Set this to the name of the storage backend that by default will support
  19. // this field.
  20. public static $default_storage = 'field_chado_storage';
  21. /**
  22. * @see TripalField::widgetForm()
  23. */
  24. public function widgetForm(&$widget, &$form, &$form_state, $langcode, $items, $delta, $element) {
  25. // This field has no value field. Just a fieldset for adding new annotation types.
  26. $widget['#type'] = 'fieldset';
  27. $widget['#title'] = $element['#title'];
  28. $widget['#description'] = $element['#description'];
  29. $widget['#group'] = 'entity_form_vtabs';
  30. $widget['cvterm_class_adder_instructions'] = array(
  31. '#type' => 'item',
  32. '#markup' => t('You may add annotation types to this form by
  33. providing a vocabulary name in the field below
  34. and clicking the "Add Annotation Type" button. This will add a
  35. new field to the form above for the vocabulary you entered which
  36. will allow users to associate terms from that vocabulary to
  37. this record.'),
  38. );
  39. $options = tripal_get_cv_select_options();
  40. $widget['value'] = array(
  41. '#type' => 'select',
  42. '#title' => t('Vocabulary'),
  43. '#options' => $options,
  44. '#description' => t("Please enter the vocabulary that contains terms
  45. you want to allow users to use for annotations."),
  46. );
  47. // $widget['#element_validate'] = array('chado_linker__cvterm_adder_widget_validate');
  48. // When this button is clicked, the form will be validated and submitted.
  49. // Therefore, we set custom submit and validate functions to override the
  50. // default form submit. In the validate function we set the form_state
  51. // to rebuild the form so the submit function never actually gets called,
  52. // but we need it or Drupal will run the default validate anyway.
  53. // we also set #limit_validation_errors to empty so fields that
  54. // are required that don't have values won't generate warnings.
  55. $widget['cvterm_class_adder_button'] = array(
  56. '#value' => t('Add Annotation Type'),
  57. '#type' => 'submit',
  58. '#name' => 'cvterm_class_adder_button',
  59. // '#submit' => array('chado_linker__cvterm_adder_widget_submit'),
  60. '#limit_validation_errors' => array(array($this->field['field_name'])),
  61. );
  62. }
  63. public function widgetFormSubmit($entity_type, $entity, $langcode, &$items, $form, &$form_state) {
  64. // Add the new field to the entity
  65. if (array_key_exists('triggering_element', $form_state) and
  66. $form_state['triggering_element']['#name'] == 'cvterm_class_adder_button') {
  67. $form_state['rebuild'] = TRUE;
  68. $field_name = $element['#field_name'];
  69. $entity_type = $element['#entity']->type;
  70. $bundle = $element['#entity']->bundle;
  71. // Get the base table name from the field annotations.
  72. $field = field_info_field($field_name);
  73. $base_table = $field['settings']['base_table'];
  74. // Get the vocabulary.
  75. //$cvterm_class_adder = tripal_chado_get_field_form_values($field_name, $form_state);
  76. $cv = chado_generate_var('cv', array('cv_id' => $cvterm_class_adder));
  77. if (!$cv) {
  78. form_set_error(implode('][', $element ['#parents']) . '][value', t("Please select a vocabulary."));
  79. return;
  80. }
  81. $type_field_name = $field_name . '__' . $cv->cv_id;
  82. // The field name is the table name in this case. We want to get the
  83. // primary key as this should be the field that maps th the value.
  84. $schema = chado_get_schema($field_name);
  85. $pkey = $schema['primary key'][0];
  86. // Add the field if it doesn't already exists.
  87. $field = field_info_field('cvterm');
  88. if (!$field) {
  89. $create_info = array(
  90. 'field_name' => 'cvterm',
  91. 'type' => 'tripal_chado_cvterm_widget',
  92. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  93. 'locked' => FALSE,
  94. 'storage' => array(
  95. 'type' => 'field_chado_storage',
  96. ),
  97. 'settings' => array(
  98. 'chado_table' => $field_name,
  99. 'chado_column' => $pkey,
  100. 'base_table' => $base_table,
  101. ),
  102. );
  103. $field = field_create_field($create_info);
  104. }
  105. // Attach the field to the bundle if it isn't already.
  106. if (!$field or !array_key_exists('bundles', $field) or
  107. !array_key_exists('TripalEntity', $field['bundles']) or
  108. !in_array($bundle_name, $field['bundles']['TripalEntity'])) {
  109. $createInstanceInfo = array(
  110. 'field_name' => 'cvtmerm',
  111. 'entity_type' => 'TripalEntity',
  112. 'bundle' => $this->bundle->name,
  113. 'label' => ucfirst(preg_replace('/_/', ' ', $cv->name)),
  114. 'description' => "Annotations from the $cv->name vocabulary",
  115. 'required' => FALSE,
  116. 'settings' => array(),
  117. 'widget' => array(
  118. 'type' => 'tripal_chado_cvterm_widget',
  119. 'settings' => array(
  120. 'display_label' => 1,
  121. ),
  122. ),
  123. 'display' => array(
  124. 'default' => array(
  125. 'label' => 'above',
  126. 'type' => 'tripal_chado_cvterm_formatter',
  127. 'settings' => array(),
  128. ),
  129. ),
  130. );
  131. $instance = field_create_instance($createInstanceInfo);
  132. }
  133. }
  134. }
  135. }