chado_linker__cvterm_adder_widget.inc 7.3 KB

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