chado_linker__cvterm_adder.inc 7.9 KB

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