chado_linker__prop_adder.inc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /**
  3. * Implements hook_info() for fields.
  4. *
  5. * This is a hook provided by the tripal_chado module for offloading the
  6. * hook_field_info() hook for each field to specify.
  7. *
  8. * Fields that add new fields. These fields are not shown on
  9. * pages. They are avaiable to site curators when adding/updating
  10. * a record and allow the user to add new linker table fields.
  11. *
  12. */
  13. function chado_linker__prop_adder_info() {
  14. return array(
  15. 'label' => t('Add a Property Type'),
  16. 'description' => t('This record may have any number of properties. Use
  17. this field to first add the type.'),
  18. 'default_widget' => 'chado_linker__prop_adder_widget',
  19. 'default_formatter' => 'hidden',
  20. 'settings' => array(),
  21. 'storage' => array(
  22. 'type' => 'field_chado_storage',
  23. 'module' => 'tripal_chado',
  24. 'active' => TRUE
  25. ),
  26. );
  27. }
  28. /**
  29. * Implements hook_widget_info.
  30. *
  31. * This is a hook provided by the tripal_chado module for offloading
  32. * the hook_field_widget_info() hook for each field to specify.
  33. */
  34. function chado_linker__prop_adder_widget_info() {
  35. return array(
  36. 'label' => t('Add a Property'),
  37. 'field types' => array('chado_linker__prop_adder'),
  38. );
  39. }
  40. /**
  41. *
  42. */
  43. function chado_linker__prop_adder_widget(&$widget, $form, $form_state,
  44. $field, $instance, $langcode, $items, $delta, $element) {
  45. // This field has no value field. Just a fieldset for adding new properties.
  46. $widget['#element_validate'] = array('chado_linker__prop_adder_widget_validate');
  47. $widget['#type'] = 'fieldset';
  48. $widget['#title'] = $element['#title'];
  49. $widget['#description'] = $element['#description'];
  50. $widget['#group'] = 'entity_form_vtabs';
  51. $widget['kvproperty_instructions'] = array(
  52. '#type' => 'item',
  53. '#markup' => t('You may add additional properties to this form by
  54. providing a property name (from a vocabulary) in the field below
  55. and clicking the "Add Property" button. This will add a
  56. new field to the form above for the property you entered.
  57. In the future, this field will be present for all records
  58. of this type.'),
  59. );
  60. $widget['value'] = array(
  61. '#title' => t('Property Type'),
  62. '#type' => 'textfield',
  63. '#description' => t("Please enter the type of property that you want to
  64. add. As you type, suggestions will be provided."),
  65. '#autocomplete_path' => "admin/tripal/storage/chado/auto_name/cvterm/",
  66. );
  67. $widget['kvproperty_adder_link'] = array(
  68. '#type' => 'item',
  69. '#markup' => '<span class="kvproperty-adder-link">' . l('Add a term', 'admin/tripal/vocab/cvterm/add', array('attributes' => array('target' => '_blank'))) . '</span>',
  70. );
  71. // When this button is clicked, the form will be validated and submitted.
  72. // Therefore, we set custom submit and validate functions to override the
  73. // default form submit. In the validate function we set the form_state
  74. // to rebuild the form so the submit function never actually gets called,
  75. // but we need it or Drupal will run the default validate anyway.
  76. // we also set #limit_validation_errors to empty so fields that
  77. // are required that don't have values won't generate warnings.
  78. $widget['kvproperty_adder_button'] = array(
  79. '#value' => t('Add Property'),
  80. '#type' => 'submit',
  81. '#name' => 'kvproperty_adder_button',
  82. '#submit' => array('chado_linker__prop_adder_widget_submit'),
  83. '#limit_validation_errors' => array(array($field['field_name'])),
  84. );
  85. }
  86. /**
  87. * Callback function for validating the chado_linker__prop_adder_widget.
  88. */
  89. function chado_linker__prop_adder_widget_validate($element, &$form_state) {
  90. // Add the new field to the entity
  91. if (array_key_exists('triggering_element', $form_state) and
  92. $form_state['triggering_element']['#name'] == 'kvproperty_adder_button') {
  93. $form_state['rebuild'] = TRUE;
  94. $field_name = $element['#field_name'];
  95. $entity_type = $element['#entity']->type;
  96. $bundle = $element['#entity']->bundle;
  97. // Get the base table name from the field properties.
  98. $field = field_info_field($field_name);
  99. $base_table = $field['settings']['base_table'];
  100. // Get the term for the property
  101. $kvproperty = tripal_chado_get_field_form_values($field_name, $form_state);
  102. $term = chado_generate_var('cvterm', array('name' => $kvproperty), $options = array('return_array' => TRUE));
  103. if (count($term) == 1) {
  104. $prop_field_name = $field_name . '__' . $term[0]->cvterm_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. $field_info = array(
  110. 'field_type' => 'kvproperty',
  111. 'widget_type' => 'tripal_chado_kvproperty_widget',
  112. 'field_settings' => array(
  113. 'chado_table' => $field_name,
  114. 'chado_column' => $pkey,
  115. 'base_table' => $base_table,
  116. ),
  117. 'storage' => 'field_chado_storage',
  118. 'widget_settings' => array(),
  119. 'description' => $term[0]->definition ? $term[0]->definition : '',
  120. 'label' => ucfirst(preg_replace('/_/', ' ', $term[0]->name)),
  121. 'is_required' => FALSE,
  122. // All properties are unlimited.
  123. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  124. );
  125. tripal_add_bundle_field($prop_field_name, $field_info, $entity_type, $bundle);
  126. }
  127. else if (count($term) > 1) {
  128. form_set_error(implode('][', $element ['#parents']) . '][value', t("This term is present in multiple vocabularies. Please select the appropriate one."));
  129. }
  130. else {
  131. form_set_error(implode('][', $element ['#parents']) . '][value', t("Please provide a property type to add."));
  132. }
  133. }
  134. }
  135. /**
  136. * Callback function for submitting the chado_linker__prop_adder_widget.
  137. */
  138. function chado_linker__prop_adder_widget_submit($element, &$form_state) {
  139. }