chado_linker__cvterm_adder.inc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. // 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 below
  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. public function widgetFormSubmit($entity_type, $entity, $langcode, &$items, $form, &$form_state) {
  72. // Add the new field to the entity
  73. if (array_key_exists('triggering_element', $form_state) and
  74. $form_state['triggering_element']['#name'] == 'cvterm_class_adder_button') {
  75. $form_state['rebuild'] = TRUE;
  76. $field_name = $element['#field_name'];
  77. $entity_type = $element['#entity']->type;
  78. $bundle = $element['#entity']->bundle;
  79. // Get the base table name from the field annotations.
  80. $field = field_info_field($field_name);
  81. $base_table = $field['settings']['base_table'];
  82. // Get the vocabulary.
  83. //$cvterm_class_adder = tripal_chado_get_field_form_values($field_name, $form_state);
  84. $cv = chado_generate_var('cv', array('cv_id' => $cvterm_class_adder));
  85. if (!$cv) {
  86. form_set_error(implode('][', $element ['#parents']) . '][value', t("Please select a vocabulary."));
  87. return;
  88. }
  89. $type_field_name = $field_name . '__' . $cv->cv_id;
  90. // The field name is the table name in this case. We want to get the
  91. // primary key as this should be the field that maps th the value.
  92. $schema = chado_get_schema($field_name);
  93. $pkey = $schema['primary key'][0];
  94. // Add the field if it doesn't already exists.
  95. $field = field_info_field('cvterm');
  96. if (!$field) {
  97. $create_info = array(
  98. 'field_name' => 'cvterm',
  99. 'type' => 'tripal_chado_cvterm_widget',
  100. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  101. 'locked' => FALSE,
  102. 'storage' => array(
  103. 'type' => 'field_chado_storage',
  104. ),
  105. 'settings' => array(
  106. 'chado_table' => $field_name,
  107. 'chado_column' => $pkey,
  108. 'base_table' => $base_table,
  109. ),
  110. );
  111. $field = field_create_field($create_info);
  112. }
  113. // Attach the field to the bundle if it isn't already.
  114. if (!$field or !array_key_exists('bundles', $field) or
  115. !array_key_exists('TripalEntity', $field['bundles']) or
  116. !in_array($bundle_name, $field['bundles']['TripalEntity'])) {
  117. $createInstanceInfo = array(
  118. 'field_name' => 'cvtmerm',
  119. 'entity_type' => 'TripalEntity',
  120. 'bundle' => $this->bundle->name,
  121. 'label' => ucfirst(preg_replace('/_/', ' ', $cv->name)),
  122. 'description' => "Annotations from the $cv->name vocabulary",
  123. 'required' => FALSE,
  124. 'settings' => array(),
  125. 'widget' => array(
  126. 'type' => 'tripal_chado_cvterm_widget',
  127. 'settings' => array(
  128. 'display_label' => 1,
  129. ),
  130. ),
  131. 'display' => array(
  132. 'default' => array(
  133. 'label' => 'above',
  134. 'type' => 'tripal_chado_cvterm_formatter',
  135. 'settings' => array(),
  136. ),
  137. ),
  138. );
  139. $instance = field_create_instance($createInstanceInfo);
  140. }
  141. }
  142. }
  143. }