chado_linker__cvterm_adder.inc 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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::formatterView()
  23. */
  24. public function formatterView(&$element, $entity_type, $entity, $langcode, $items, $display) {
  25. // This field should never be viewed. It's to help add new cvterms
  26. // when editing an entity. So return nothing.
  27. return '';
  28. }
  29. /**
  30. * @see TripalField::widgetForm()
  31. */
  32. public function widgetForm(&$widget, &$form, &$form_state, $langcode, $items, $delta, $element) {
  33. parent::widgetForm($widget, $form, $form_state, $langcode, $items, $delta, $element);
  34. // This field has no value field. Just a fieldset for adding new annotation types.
  35. $widget['#type'] = 'fieldset';
  36. $widget['#title'] = $element['#title'];
  37. $widget['#description'] = $element['#description'];
  38. $widget['#group'] = 'entity_form_vtabs';
  39. $widget['cvterm_class_adder_instructions'] = array(
  40. '#type' => 'item',
  41. '#markup' => t('You may add annotation types to this form by
  42. providing a vocabulary name in the field above
  43. and clicking the "Add Annotation Type" button. This will add a
  44. new field to the form above for the vocabulary you entered which
  45. will allow users to associate terms from that vocabulary to
  46. this record.'),
  47. );
  48. $options = tripal_get_cv_select_options();
  49. $widget['value'] = array(
  50. '#type' => 'select',
  51. '#title' => t('Vocabulary'),
  52. '#options' => $options,
  53. '#description' => t("Please enter the vocabulary that contains terms
  54. you want to allow users to use for annotations."),
  55. );
  56. // $widget['#element_validate'] = array('chado_linker__cvterm_adder_widget_validate');
  57. // When this button is clicked, the form will be validated and submitted.
  58. // Therefore, we set custom submit and validate functions to override the
  59. // default form submit. In the validate function we set the form_state
  60. // to rebuild the form so the submit function never actually gets called,
  61. // but we need it or Drupal will run the default validate anyway.
  62. // we also set #limit_validation_errors to empty so fields that
  63. // are required that don't have values won't generate warnings.
  64. $widget['cvterm_class_adder_button'] = array(
  65. '#value' => t('Add Annotation Type'),
  66. '#type' => 'submit',
  67. '#name' => 'cvterm_class_adder_button',
  68. // '#submit' => array('chado_linker__cvterm_adder_widget_submit'),
  69. '#limit_validation_errors' => array(array($this->field['field_name'])),
  70. );
  71. }
  72. /**
  73. * @see TripalField::widgetFormValidate()
  74. */
  75. public function widgetFormValidate($form, &$form_state, $entity_type, $entity, $langcode, $delta) {
  76. if (array_key_exists('triggering_element', $form_state) and
  77. $form_state['triggering_element']['#name'] == 'cvterm_class_adder_button') {
  78. $this_field = $this->field;
  79. $field_name = $this_field['field_name'];
  80. $bundle = $entity->bundle;
  81. // Get the base table name from the field annotations.
  82. $base_table = $entity->chado_table;
  83. $cvterm_class_adder = $form_state['values'][$base_table . '_cvterm'][$langcode][$delta]['value'];
  84. $cv = chado_generate_var('cv', array('cv_id' => $cvterm_class_adder));
  85. // Make sure a valid vocabulary is selected
  86. if (!$cv) {
  87. form_set_error("$field_name][$langcode][$delta][value", "Please select a vocabulary.");
  88. }
  89. else {
  90. // Make sure this vocabulary doesn't already have a field
  91. if (key_exists($field_name . '__' . $cv->cv_id, $form_state['values'])) {
  92. form_set_error("$field_name][$langcode][$delta][wrapper][terms_name", "Field for this vocabulary already exists. Please select another vocabulary.");
  93. }
  94. }
  95. }
  96. }
  97. /**
  98. * @see TripalField::widgetFormSubmit()
  99. */
  100. public function widgetFormSubmit($form, &$form_state, $entity_type, $entity, $langcode, $delta) {
  101. // Add the new field to the entity
  102. if (array_key_exists('triggering_element', $form_state) and
  103. $form_state['triggering_element']['#name'] == 'cvterm_class_adder_button') {
  104. $form_state['rebuild'] = TRUE;
  105. $this_field = $this->field;
  106. $field_name = $this_field['field_name'];
  107. $bundle = $entity->bundle;
  108. // Get the base table name from the field annotations.
  109. $base_table = $entity->chado_table;
  110. $cvterm_class_adder = $form_state['values'][$base_table . '_cvterm'][$langcode][$delta]['value'];
  111. // Get the vocabulary.
  112. //$cvterm_class_adder = tripal_chado_get_field_form_values($field_name, $form_state);
  113. $cv = chado_generate_var('cv', array('cv_id' => $cvterm_class_adder));
  114. if (!$cv) {
  115. return;
  116. }
  117. $type_field_name = $field_name . '__' . $cv->cv_id;
  118. // The field name is the table name in this case. We want to get the
  119. // primary key as this should be the field that maps th the value.
  120. $schema = chado_get_schema($field_name);
  121. $pkey = $schema['primary key'][0];
  122. // Add the field if it doesn't already exists.
  123. $field = field_info_field($type_field_name);
  124. if (!$field) {
  125. $create_info = array(
  126. 'field_name' => $type_field_name,
  127. 'type' => 'chado_linker__cvterm',
  128. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  129. 'locked' => FALSE,
  130. 'storage' => array(
  131. 'type' => 'field_chado_storage',
  132. ),
  133. 'settings' => array(
  134. 'chado_table' => $field_name,
  135. 'chado_column' => $pkey,
  136. 'base_table' => $base_table,
  137. ),
  138. );
  139. $field = field_create_field($create_info);
  140. }
  141. // Attach the field to the bundle if it isn't already.
  142. if (!$field or !array_key_exists('bundles', $field) or
  143. !array_key_exists('TripalEntity', $field['bundles']) or
  144. !in_array($bundle, $field['bundles']['TripalEntity'])) {
  145. $createInstanceInfo = array(
  146. 'field_name' => $type_field_name,
  147. 'entity_type' => 'TripalEntity',
  148. 'bundle' => $bundle,
  149. 'label' => ucfirst(preg_replace('/_/', ' ', $cv->name)),
  150. 'description' => "Annotations from the $cv->name vocabulary",
  151. 'required' => FALSE,
  152. 'settings' => array(),
  153. 'widget' => array(
  154. 'type' => 'chado_linker__cvterm_widget',
  155. 'settings' => array(
  156. 'display_label' => 1,
  157. ),
  158. ),
  159. 'display' => array(
  160. 'default' => array(
  161. 'label' => 'above',
  162. 'type' => 'chado_linker__cvterm_formatter',
  163. 'settings' => array(),
  164. ),
  165. ),
  166. );
  167. $instance = field_create_instance($createInstanceInfo);
  168. }
  169. }
  170. }
  171. }