tripal_ws.fields.inc 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * @file
  4. * Contains all field specific code outside the classes.
  5. */
  6. /**
  7. * Implements hook_bundle_fields_info().
  8. */
  9. function tripal_ws_bundle_fields_info($entity_type, $bundle) {
  10. $fields = [];
  11. // No fields are added programmatically.
  12. return $fields;
  13. }
  14. /**
  15. * Implements hook_bundle_instances_info().
  16. */
  17. function tripal_ws_bundle_instances_info($entity_type, $bundle) {
  18. $instances = [];
  19. // No field instances are added programmatically.
  20. return $instances;
  21. }
  22. /**
  23. * Implements hook_bundle_create_user_field().
  24. *
  25. * A priviledged user has the ability to add new fields to the bundle. The
  26. * remote__data field is allowed to be added dynamically by the user.
  27. * But, Drupal doesn't know how to deal with it, so this function is called
  28. * for any field attached to a TripalEntity bundle type. Any fields whose
  29. * TripalField::$module argument is set to 'tripal_ws' and that can be
  30. * added dynamically will result in a call to this function.
  31. */
  32. function tripal_ws_bundle_create_user_field($new_field, $bundle) {
  33. // Get the table this bundle is mapped to.
  34. $term = tripal_load_term_entity(['term_id' => $bundle->term_id]);
  35. $vocab = $term->vocab;
  36. $params = [
  37. 'vocabulary' => $vocab->vocabulary,
  38. 'accession' => $term->accession,
  39. ];
  40. // We allow site admins to add new chado_linker__prop fields to an entity.
  41. // This function will allow us to properly add them. But at this point we
  42. // don't know the controlled vocabulary term. We'll have to use the
  43. // defaults and let the user set it using the interface.
  44. if ($new_field['type'] == 'remote__data') {
  45. $field_name = $new_field['field_name'];
  46. $field_type = 'remote__data';
  47. // First add the field.
  48. field_create_field([
  49. 'field_name' => $field_name,
  50. 'type' => $field_type,
  51. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  52. 'locked' => FALSE,
  53. 'storage' => [
  54. 'type' => 'field_tripal_ws_storage',
  55. ],
  56. ]);
  57. // Now add the instance
  58. field_create_instance([
  59. 'field_name' => $field_name,
  60. 'entity_type' => 'TripalEntity',
  61. 'bundle' => $bundle->name,
  62. 'label' => $new_field['label'],
  63. 'description' => '',
  64. 'required' => FALSE,
  65. 'settings' => [
  66. 'auto_attach' => FALSE,
  67. 'term_vocabulary' => '',
  68. 'term_accession' => '',
  69. 'term_name' => '',
  70. ],
  71. 'widget' => [
  72. 'type' => 'remote__data_widget',
  73. 'settings' => [
  74. 'display_label' => 1,
  75. ],
  76. ],
  77. 'display' => [
  78. 'default' => [
  79. 'label' => 'inline',
  80. 'type' => 'remote__data_formatter',
  81. 'settings' => [],
  82. ],
  83. ],
  84. ]);
  85. }
  86. }