kvproperty_adder.inc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /**
  3. *
  4. * @param unknown $entity_type
  5. * @param unknown $entity
  6. * @param unknown $field
  7. * @param unknown $instance
  8. * @param unknown $langcode
  9. * @param unknown $items
  10. * @param unknown $display
  11. */
  12. function tripal_fields_kvproperty_adder_formatter(&$element, $entity_type, $entity, $field,
  13. $instance, $langcode, $items, $display) {
  14. foreach ($items as $delta => $item) {
  15. // Do nothing, this field is only meant for the form.
  16. }
  17. }
  18. /**
  19. *
  20. * @param unknown $field_name
  21. * @param unknown $widget
  22. * @param unknown $form
  23. * @param unknown $form_state
  24. * @param unknown $field
  25. * @param unknown $instance
  26. * @param unknown $langcode
  27. * @param unknown $items
  28. * @param unknown $delta
  29. * @param unknown $element
  30. */
  31. function tripal_fields_kvproperty_adder_widget($form, $form_state, $field, $instance, $langcode, $items, $delta, $element) {
  32. $field_name = $field['field_name'];
  33. $widget = $element;
  34. $widget += array(
  35. '#element_validate' => array('tripal_fields_kvproperty_adder_widget_validate'),
  36. '#type' => 'fieldset',
  37. '#title' => $element['#title'],
  38. '#group' => 'ev_tabs',
  39. array(
  40. 'kvproperty_instructions' => array(
  41. '#type' => 'item',
  42. '#markup' => t('You may add additional properties to this form by
  43. providing a property name (from a vocabulary) in the field below
  44. and clicking the "Add Property" button. This will add a
  45. new field to the form above for the property you entered.'),
  46. ),
  47. 'kvproperty_adder' => array(
  48. '#title' => t('Property Type'),
  49. '#type' => 'textfield',
  50. '#description' => t("Please enter the type of property that you want to add. As you type, suggestions will be provided."),
  51. '#autocomplete_path' => "admin/tripal/chado/tripal_cv/cvterm/auto_name/",
  52. ),
  53. 'kvproperty_adder_link' => array(
  54. '#type' => 'item',
  55. '#markup' => '<span class="kvproperty-adder-link">' . l('Add a term', 'admin/tripal/chado/tripal_cv/cvterm/add', array('attributes' => array('target' => '_blank'))) . '</span>',
  56. ),
  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. 'kvproperty_adder_button' => array(
  65. '#value' => t('Add Property'),
  66. '#type' => 'submit',
  67. '#name' => 'kvproperty_adder_button',
  68. '#validate' => 'tripal_fields_kvproperty_adder_widget_validate',
  69. '#submit' => 'tripal_fields_kvproperty_adder_widget_submit',
  70. '#limit_validation_errors' => array(array($field_name)),
  71. ),
  72. ),
  73. );
  74. return $widget;
  75. }
  76. /**
  77. * Callback function for validating the tripal_fields_kvproperty_adder_widget.
  78. */
  79. function tripal_fields_kvproperty_adder_widget_validate($element, &$form_state) {
  80. // Add the new field to the entity
  81. if (array_key_exists('triggering_element', $form_state) and
  82. $form_state['triggering_element']['#name'] == 'kvproperty_adder_button') {
  83. $form_state['rebuild'] = TRUE;
  84. $field_name = $element['#field_name'];
  85. $entity_type = $element['#entity_type'];
  86. $bundle = $element['#bundle'];
  87. // Get the term for the property
  88. $kvproperty = tripal_fields_get_field_form_values($field_name, $form_state, 'kvproperty_adder');
  89. $term = chado_generate_var('cvterm', array('name' => $kvproperty), $options = array('return_array' => TRUE));
  90. if (count($term) == 1) {
  91. $prop_field_name = $field_name . '__' . strtolower($term[0]->dbxref_id->db_id->name) . '_' . $term[0]->dbxref_id->accession;
  92. $field_info = array(
  93. 'field_type' => 'kvproperty',
  94. 'widget_type' => 'tripal_fields_kvproperty_widget',
  95. 'field_settings' => array(),
  96. 'widget_settings' => array(),
  97. 'description' => '',
  98. 'label' => ucfirst(preg_replace('/_/', ' ', $term[0]->name)),
  99. 'is_required' => FALSE,
  100. // All properties are unlimited.
  101. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  102. );
  103. tripal_add_bundle_field($prop_field_name, $field_info, $entity_type, $bundle);
  104. }
  105. else if (count($term) > 1) {
  106. form_set_error(implode('][', $element ['#parents']) . '][0][kvproperty_adder', t("This term is present in multiple vocabularies. Please select the appropriate one."));
  107. }
  108. else {
  109. form_set_error(implode('][', $element ['#parents']) . '][0][kvproperty_adder', t("Please provide a property type to add."));
  110. }
  111. }
  112. }
  113. /**
  114. * Callback function for submitting the tripal_fields_kvproperty_adder_widget.
  115. */
  116. function tripal_fields_kvproperty_adder_widget_submit($element, &$form_state) {
  117. }