kvproperty_adder.inc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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_chado_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. */
  21. function tripal_chado_kvproperty_adder_widget(&$widget, $form, $form_state,
  22. $field, $instance, $langcode, $items, $delta, $element) {
  23. // This field has no value field. Just a fieldset for adding new properties.
  24. $widget['#element_validate'] = array('tripal_chado_kvproperty_adder_widget_validate');
  25. $widget['#type'] = 'fieldset';
  26. $widget['#title'] = $element['#title'];
  27. $widget['#description'] = $element['#description'];
  28. $widget['#group'] = 'entity_form_vtabs';
  29. $widget['kvproperty_instructions'] = array(
  30. '#type' => 'item',
  31. '#markup' => t('You may add additional properties to this form by
  32. providing a property name (from a vocabulary) in the field below
  33. and clicking the "Add Property" button. This will add a
  34. new field to the form above for the property you entered.
  35. In the future, this field will be present for all records
  36. of this type.'),
  37. );
  38. $widget['value'] = array(
  39. '#title' => t('Property Type'),
  40. '#type' => 'textfield',
  41. '#description' => t("Please enter the type of property that you want to
  42. add. As you type, suggestions will be provided."),
  43. '#autocomplete_path' => "admin/tripal/vocab/cvterm/auto_name//",
  44. );
  45. $widget['kvproperty_adder_link'] = array(
  46. '#type' => 'item',
  47. '#markup' => '<span class="kvproperty-adder-link">' . l('Add a term', 'admin/tripal/vocab/cvterm/add', array('attributes' => array('target' => '_blank'))) . '</span>',
  48. );
  49. // When this button is clicked, the form will be validated and submitted.
  50. // Therefore, we set custom submit and validate functions to override the
  51. // default form submit. In the validate function we set the form_state
  52. // to rebuild the form so the submit function never actually gets called,
  53. // but we need it or Drupal will run the default validate anyway.
  54. // we also set #limit_validation_errors to empty so fields that
  55. // are required that don't have values won't generate warnings.
  56. $widget['kvproperty_adder_button'] = array(
  57. '#value' => t('Add Property'),
  58. '#type' => 'submit',
  59. '#name' => 'kvproperty_adder_button',
  60. '#submit' => array('tripal_chado_kvproperty_adder_widget_submit'),
  61. '#limit_validation_errors' => array(array($field['field_name'])),
  62. );
  63. }
  64. /**
  65. * Callback function for validating the tripal_chado_kvproperty_adder_widget.
  66. */
  67. function tripal_chado_kvproperty_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'] == 'kvproperty_adder_button') {
  71. $form_state['rebuild'] = TRUE;
  72. $field_name = $element['#parents'][0];
  73. $entity_type = $element['#entity']->type;
  74. $bundle = $element['#entity']->bundle;
  75. // Get the term for the property
  76. $kvproperty = tripal_chado_get_field_form_values($field_name, $form_state);
  77. $term = chado_generate_var('cvterm', array('name' => $kvproperty), $options = array('return_array' => TRUE));
  78. if (count($term) == 1) {
  79. $prop_field_name = $field_name . '__' . $term[0]->cvterm_id;
  80. // The field name is the table name in this case. We want to get the
  81. // primary key as this should be the field that maps th the value.
  82. $schema = chado_get_schema($field_name);
  83. $pkey = $schema['primary key'][0];
  84. $field_info = array(
  85. 'field_type' => 'kvproperty',
  86. 'widget_type' => 'tripal_chado_kvproperty_widget',
  87. 'field_settings' => array(
  88. 'chado_table' => $field_name,
  89. 'chado_column' => $pkey,
  90. ),
  91. 'widget_settings' => array(),
  92. 'description' => $term[0]->definition ? $term[0]->definition : '',
  93. 'label' => ucfirst(preg_replace('/_/', ' ', $term[0]->name)),
  94. 'is_required' => FALSE,
  95. // All properties are unlimited.
  96. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  97. );
  98. tripal_add_bundle_field($prop_field_name, $field_info, $entity_type, $bundle);
  99. }
  100. else if (count($term) > 1) {
  101. form_set_error(implode('][', $element ['#parents']) . '][value', t("This term is present in multiple vocabularies. Please select the appropriate one."));
  102. }
  103. else {
  104. form_set_error(implode('][', $element ['#parents']) . '][value', t("Please provide a property type to add."));
  105. }
  106. }
  107. }
  108. /**
  109. * Callback function for submitting the tripal_chado_kvproperty_adder_widget.
  110. */
  111. function tripal_chado_kvproperty_adder_widget_submit($element, &$form_state) {
  112. }