tripal_fields.api.inc 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. * @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,
  31. $entity_type_name, $bundle_name) {
  32. $field = field_info_field($field_name);
  33. // If the field exists and is attached to this bundle then just return,
  34. // there is nothing left to do.
  35. if ($field and array_key_exists('bundles', $field) and
  36. array_key_exists($entity_type_name, $field['bundles']) and
  37. in_array($bundle_name, $field['bundles'][$entity_type_name])) {
  38. return;
  39. }
  40. // Allow other modules to alter the field information array.
  41. drupal_alter('chado_field', $field_info);
  42. $cardinality = 1;
  43. if (array_key_exists('cardinality', $field_info) and is_numeric($field_info['cardinality'])) {
  44. $cardinality = $field_info['cardinality'];
  45. }
  46. // If the field doesn't exist then create it.
  47. if (!$field) {
  48. $field = array(
  49. 'field_name' => $field_name,
  50. 'type' => $field_info['field_type'],
  51. 'cardinality' => $cardinality,
  52. 'locked' => FALSE,
  53. 'storage' => array(
  54. 'type' => 'field_chado_storage'
  55. ),
  56. 'settings' => $field_info['field_settings'],
  57. );
  58. field_create_field($field);
  59. }
  60. // Attach the field to the bundle.
  61. $field_instance = array(
  62. 'field_name' => $field_name,
  63. 'label' => $field_info['label'],
  64. 'description' => $field_info['description'],
  65. 'widget' => array(
  66. 'type' => $field_info['widget_type'],
  67. 'settings' => $field_info['widget_settings'],
  68. ),
  69. 'entity_type' => $entity_type_name,
  70. 'required' => $field_info['is_required'],
  71. 'settings' => $field_info['field_settings'],
  72. 'bundle' => $bundle_name,
  73. );
  74. field_create_instance($field_instance);
  75. }