chado_linker__prop_adder.inc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. class chado_linker__prop_adder extends TripalField {
  3. // The default lable for this field.
  4. public static $default_label = 'Add a Property Type';
  5. // The default description for this field.
  6. public static $default_description = 'This record may have any number of properties. Use
  7. 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. $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/storage/chado/auto_name/cvterm/",
  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. '#limit_validation_errors' => array(array($this->field['field_name'])),
  61. );
  62. }
  63. /**
  64. * @see TripalField::widgetFormValidate
  65. */
  66. public function widgetFormValidate($entity_type, $entity, $field, $items, &$errors) {
  67. }
  68. }
  69. /**
  70. * Callback function for validating the chado_linker__prop_adder_widget.
  71. */
  72. function chado_linker__prop_adder_widget_validate($element, &$form_state) {
  73. // Add the new field to the entity
  74. if (array_key_exists('triggering_element', $form_state) and
  75. $form_state['triggering_element']['#name'] == 'kvproperty_adder_button') {
  76. $form_state['rebuild'] = TRUE;
  77. $field_name = $element['#field_name'];
  78. $entity_type = $element['#entity']->type;
  79. $bundle = $element['#entity']->bundle;
  80. // Get the base table name from the field properties.
  81. $field = field_info_field($field_name);
  82. $base_table = $field['settings']['base_table'];
  83. // Get the term for the property
  84. //$kvproperty = tripal_chado_get_field_form_values($field_name, $form_state);
  85. $term = chado_generate_var('cvterm', array('name' => $kvproperty), $options = array('return_array' => TRUE));
  86. if (count($term) == 1) {
  87. $prop_field_name = $field_name . '__' . $term[0]->cvterm_id;
  88. // The field name is the table name in this case. We want to get the
  89. // primary key as this should be the field that maps th the value.
  90. $schema = chado_get_schema($field_name);
  91. $pkey = $schema['primary key'][0];
  92. // Add the field if it doesn't already exists.
  93. $field = field_info_field('cvterm');
  94. if (!$field) {
  95. $create_info = array(
  96. 'field_name' => 'property-' . $term[0]->cvterm_id,
  97. 'type' => 'tripal_chado_kvproperty_widget',
  98. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  99. 'locked' => FALSE,
  100. 'storage' => array(
  101. 'type' => 'field_chado_storage',
  102. ),
  103. 'settings' => array(
  104. 'chado_table' => $field_name,
  105. 'chado_column' => $pkey,
  106. 'base_table' => $base_table,
  107. 'semantic_web' => '',
  108. ),
  109. );
  110. $field = field_create_field($create_info);
  111. }
  112. // Attach the field to the bundle if it isn't already.
  113. if (!$field and array_key_exists('bundles', $field) or
  114. !array_key_exists('TripalEntity', $field['bundles']) or
  115. !in_array($bundle_name, $field['bundles']['TripalEntity'])) {
  116. $createInstanceInfo = array(
  117. 'field_name' => 'property-' . $term[0]->cvterm_id,
  118. 'entity_type' => 'TripalEntity',
  119. 'bundle' => $this->bundle->name,
  120. 'label' => ucfirst(preg_replace('/_/', ' ', $term[0]->name)),
  121. 'description' => $term[0]->definition ? $term[0]->definition : '',
  122. 'required' => FALSE,
  123. 'settings' => array(),
  124. 'widget' => array(
  125. 'type' => 'tripal_chado_kvproperty_widget',
  126. 'settings' => array(
  127. 'display_label' => 1,
  128. ),
  129. ),
  130. 'display' => array(
  131. 'default' => array(
  132. 'label' => 'inline',
  133. 'type' => 'tripal_chado_kvproperty_formatter',
  134. 'settings' => array(),
  135. ),
  136. ),
  137. );
  138. $instance = field_create_instance($createInstanceInfo);
  139. }
  140. }
  141. else if (count($term) > 1) {
  142. form_set_error(implode('][', $element ['#parents']) . '][value', t("This term is present in multiple vocabularies. Please select the appropriate one."));
  143. }
  144. else {
  145. form_set_error(implode('][', $element ['#parents']) . '][value', t("Please provide a property type to add."));
  146. }
  147. }
  148. }