TripalBundleController.inc

File

tripal/includes/TripalBundleController.inc
View source
  1. <?php
  2. /**
  3. * The Controller for Tripal data type entities
  4. */
  5. class TripalBundleController extends EntityAPIControllerExportable {
  6. public function __construct($entityType) {
  7. parent::__construct($entityType);
  8. }
  9. /**
  10. * Create a type we first set up the values that are specific
  11. * to our type schema but then also go through the EntityAPIController
  12. * function.
  13. *
  14. * @param $type
  15. * The machine-readable type of the tripal data entity.
  16. *
  17. * @return
  18. * A type object with all default fields initialized.
  19. */
  20. public function create(array $values = []) {
  21. // Add values that are specific to our entity
  22. $values += [
  23. 'id' => '',
  24. 'is_new' => TRUE,
  25. 'data' => '',
  26. ];
  27. $bundle = parent::create($values);
  28. // Allow modules to make additions to the entity when it's created.
  29. $modules = module_implements('bundle_create');
  30. foreach ($modules as $module) {
  31. $function = $module . '_bundle_create';
  32. $function($bundle, []);
  33. }
  34. return $bundle;
  35. }
  36. /**
  37. * Overrides the parent delete function.
  38. *
  39. * @param $ids
  40. * @param DatabaseTransaction $transaction
  41. */
  42. public function delete($ids, DatabaseTransaction $transaction = NULL) {
  43. $bundles = $ids ? $this->load($ids) : FALSE;
  44. if (!$transaction) {
  45. $transaction = db_transaction();
  46. }
  47. if ($bundles) {
  48. foreach ($bundles as $id => $bundle) {
  49. // Allow modules to perform actions when the bundle is deleted.
  50. $modules = module_implements('bundle_delete');
  51. foreach ($modules as $module) {
  52. $function = $module . '_bundle_delete';
  53. $function($bundle);
  54. }
  55. // Find any TripalEntity fields that are attached to this bundle and
  56. // remove them.
  57. $instances = field_info_instances('TripalEntity', $bundle->name);
  58. foreach ($instances as $instance) {
  59. // Mark the instance as deleted and purge it.
  60. $field = field_info_field($instance['field_name']);
  61. field_delete_instance($instance);
  62. field_purge_instance($instance);
  63. // If the field has no more instances then purge it too.
  64. if (count($field['bundles']) == 1 and
  65. count($field['bundles']['TripalEntity']) == 1 and
  66. in_array($bundle->name, $field['bundles']['TripalEntity'])
  67. ) {
  68. field_purge_field($field);
  69. }
  70. }
  71. // Remove any entities from the tripal_entity table.
  72. db_delete('tripal_entity')
  73. ->condition('bundle', $bundle->name)
  74. ->execute();
  75. // Remove the terms for the bundles that are to be deleted.
  76. db_delete('tripal_term')
  77. ->condition('id', $bundle->term_id)
  78. ->execute();
  79. }
  80. // Use the parent function to delete the bundles.
  81. parent::delete($ids, $transaction);
  82. // Not sure what this does, but copied from the
  83. // EntityAPIControllerExportable->delete() function which this one
  84. // overrides.
  85. foreach ($bundles as $id => $bundle) {
  86. if (entity_has_status($this->entityType, $bundle, ENTITY_IN_CODE)) {
  87. entity_defaults_rebuild([$this->entityType]);
  88. break;
  89. }
  90. }
  91. }
  92. }
  93. }