chado_linker__cvterm_adder.inc 6.7 KB

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