tripal_chado.api.inc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /**
  3. * Adds a field to an Entity bundle.
  4. *
  5. * @param $field_name
  6. * The name of the field.
  7. * @param $field_info
  8. * An associative array containing the field information. The following
  9. * key/value pairs are supported:
  10. * 'field_type' : a valid field type. May be any of the Drupal default
  11. * fields, one created by the tripal_chado module or another custom module.
  12. * 'widget_type' : a valid widget type. May be any of the Drupal default
  13. * fields, one created by the tripal_chado module or another custom module.
  14. * 'field_settings' : an array of settings that are appropriate for the
  15. * selected field type.
  16. * 'widget_settings' : an array of settings that are appropriate for the
  17. * selected widget type.
  18. * 'description' : a default description for this field.
  19. * 'label' : a label used as a header for this field.
  20. * 'is_required' : indicates if the field is required in the edit form.
  21. * 'cardinality' : indicates the number of values this field can support.
  22. * the default is 1 (meaning only one value). Use a value of
  23. * FIELD_CARDINALITY_UNLIMITED for unlimited number of values.
  24. * @param $entity_type_name
  25. * The entity type name.
  26. * @param $bundle_name
  27. * The bundle name.
  28. *
  29. */
  30. function tripal_add_bundle_field($field_name, $field_info, $entity_type_name, $bundle_name) {
  31. $field = field_info_field($field_name);
  32. // If the field exists and is attached to this bundle then just return,
  33. // there is nothing left to do.
  34. if ($field and array_key_exists('bundles', $field) and
  35. array_key_exists($entity_type_name, $field['bundles']) and
  36. in_array($bundle_name, $field['bundles'][$entity_type_name])) {
  37. return;
  38. }
  39. // Allow other modules to alter the field information array.
  40. drupal_alter('chado_field', $field_info);
  41. $cardinality = 1;
  42. if (array_key_exists('cardinality', $field_info) and is_numeric($field_info['cardinality'])) {
  43. $cardinality = $field_info['cardinality'];
  44. }
  45. // If the field doesn't exist then create it.
  46. if (!$field) {
  47. $field = array(
  48. 'field_name' => $field_name,
  49. 'type' => $field_info['field_type'],
  50. 'cardinality' => $cardinality,
  51. 'locked' => FALSE,
  52. 'storage' => array(
  53. 'type' => 'field_chado_storage'
  54. ),
  55. 'settings' => $field_info['field_settings'],
  56. );
  57. field_create_field($field);
  58. }
  59. // Attach the field to the bundle.
  60. $field_instance = array(
  61. 'field_name' => $field_name,
  62. 'label' => $field_info['label'],
  63. 'description' => $field_info['description'],
  64. 'widget' => array(
  65. 'type' => $field_info['widget_type'],
  66. 'settings' => $field_info['widget_settings'],
  67. ),
  68. 'entity_type' => $entity_type_name,
  69. 'required' => $field_info['is_required'],
  70. 'settings' => $field_info['field_settings'],
  71. 'bundle' => $bundle_name,
  72. );
  73. field_create_instance($field_instance);
  74. }