tripal_fields.api.inc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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_fields 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_fields 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. * You may add additional key/value pairs as needed by your custom module
  25. * if your module adds a new field type. The following fields are examples
  26. * that are added automatically by the tripal_fields module. These
  27. * fields are expected when modules implement the hook_chado_field_alter()
  28. * function:
  29. * 'chado_table' : the name of the table in Chado that will house the
  30. * value(s) of this field.
  31. * 'chado_column' : the column in the Chado table where the field will go.
  32. * @param $entity_type_name
  33. * The entity type name.
  34. * @param $bundle_name
  35. * The bundle name.
  36. *
  37. */
  38. function tripal_add_bundle_field($field_name, $field_info,
  39. $entity_type_name, $bundle_name) {
  40. $field = field_info_field($field_name);
  41. // If the field exists and is attached to this bundle then just return,
  42. // there is nothing left to do.
  43. if ($field and array_key_exists('bundles', $field) and
  44. array_key_exists($entity_type_name, $field['bundles']) and
  45. in_array($bundle_name, $field['bundles'][$entity_type_name])) {
  46. return;
  47. }
  48. // Allow other modules to alter the field information array.
  49. drupal_alter('chado_field', $field_info);
  50. $cardinality = 1;
  51. if (array_key_exists('cardinality', $field_info) and is_numeric($field_info['cardinality'])) {
  52. $cardinality = $field_info['cardinality'];
  53. }
  54. // If the field doesn't exist then create it.
  55. if (!$field) {
  56. $field = array(
  57. 'field_name' => $field_name,
  58. 'type' => $field_info['field_type'],
  59. 'cardinality' => $cardinality,
  60. 'locked' => FALSE,
  61. 'storage' => array(
  62. 'type' => 'field_chado_storage'
  63. ),
  64. 'settings' => $field_info['field_settings'],
  65. );
  66. field_create_field($field);
  67. }
  68. // Attach the field to the bundle.
  69. $field_instance = array(
  70. 'field_name' => $field_name,
  71. 'label' => $field_info['label'],
  72. 'description' => $field_info['description'],
  73. 'widget' => array(
  74. 'type' => $field_info['widget_type'],
  75. 'settings' => $field_info['widget_settings'],
  76. ),
  77. 'entity_type' => $entity_type_name,
  78. 'required' => $field_info['is_required'],
  79. 'settings' => $field_info['field_settings'],
  80. 'bundle' => $bundle_name,
  81. );
  82. field_create_instance($field_instance);
  83. }