chado_linker__cvterm_adder.inc 5.8 KB

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