tripal_entities.api.inc 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /**
  3. * Creates a new Tripal Entity type (i.e. bundle).
  4. *
  5. * @param $cvterm
  6. * A cvterm object created using the tripal_get_cvterm() function.
  7. * @param $error
  8. * A string, passed by reference, that is filled with the error message
  9. * if the function fails.
  10. *
  11. * @return
  12. * TRUE if the entity type (bundle) was succesfully created. FALSE otherwise.
  13. */
  14. function tripal_create_entity_type($cvterm, &$error = '') {
  15. $bundle_id = 'bio-data_' . $cvterm->cvterm_id;
  16. // Check to see if this bundle exists. If not then create it
  17. $bundle = db_select('tripal_bundle', 't')
  18. ->fields('t')
  19. ->condition('type', 'TripalEntity')
  20. ->condition('bundle', $bundle_id)
  21. ->execute()
  22. ->fetchObject();
  23. if (!$bundle) {
  24. // Inser the bundle.
  25. db_insert('tripal_bundle')
  26. ->fields(array(
  27. 'label' => $cvterm->name,
  28. 'type' => 'TripalEntity',
  29. 'bundle' => $bundle_id,
  30. 'data' => serialize($bundle_data),
  31. 'module' => 'tripal_entities'
  32. ))
  33. ->execute();
  34. }
  35. // Clear the entity cache so that Drupal will read our
  36. // hook_entity_info() implementation.
  37. global $language;
  38. $langcode = $language->language;
  39. cache_clear_all("entity_info:$langcode", 'cache');
  40. variable_set('menu_rebuild_needed', TRUE);
  41. // Allow modules to now add fields to the bundle
  42. module_invoke_all('add_bundle_fields', 'TripalEntity', $bundle_id, $cvterm);
  43. return TRUE;
  44. }
  45. /**
  46. * Allows a module to add key/value pairs about a bundle.
  47. *
  48. * If a module needs to associate variables with a particular TripalEntity
  49. * type (bundle), it can do so by setting the $bundle_data array passed into
  50. * this function. This hook is called prior to creation of a new entity type.
  51. *
  52. * @param $bundle_data
  53. * An array for key/value pairs to be associated with a bundle.
  54. * @param $bundle_id
  55. * The ID for the bundle.
  56. * @param $cvterm
  57. * The CV term object that the bundle represents.
  58. */
  59. function hook_tripal_bundle_data_alter(&$bundle_data, $bundle_id, $cvterm) {
  60. // Get the cvterm for this entity type.
  61. $bundle_id = $entity->bundle;
  62. $cvterm_id = preg_replace('/bio-data_/', $bundle_id);
  63. $cvterm = tripal_get_cv(array('cvterm_id' => $cvterm_id));
  64. // Add any key/value pairs to the $bundle_data array as desired.
  65. }