tripal_chado.entity.inc 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. <?php
  2. /**
  3. * Implements hook_entity_create().
  4. *
  5. * This hook is called when brand new entities are created, but
  6. * they are not loaded so the hook_entity_load() is not yet called. We
  7. * can use this hook to add properties to the entity before saving.
  8. *
  9. * @param $entity
  10. * The entity being created.
  11. * @param $type
  12. * The type of entity being created.
  13. * @param $bundle (OPTIONAL)
  14. * The bundle object for the current entity.
  15. */
  16. function tripal_chado_entity_create(&$entity, $type, $bundle = NULL) {
  17. if ($type == 'TripalEntity') {
  18. // Set some defaults on vars needed by this module.
  19. if (!property_exists($entity, 'chado_table')) {
  20. $entity->chado_table = NULL;
  21. $entity->chado_column = NULL;
  22. $entity->chado_linker = NULL;
  23. // Add in the Chado table information for this entity type.
  24. if (!$bundle) {
  25. $bundle = tripal_load_bundle_entity(array('name' => $entity->bundle));
  26. }
  27. if ($bundle->data_table) {
  28. $entity->chado_table = $bundle->data_table;
  29. $entity->chado_column = $bundle->type_column;
  30. $entity->chado_linker = $bundle->type_linker_table;
  31. }
  32. }
  33. if (!property_exists($entity, 'chado_record')) {
  34. $entity->chado_record = NULL;
  35. $entity->chado_record_id = NULL;
  36. }
  37. }
  38. }
  39. /**
  40. * Implements hook_entity_presave().
  41. */
  42. function tripal_chado_entity_presave($entity, $type) {
  43. }
  44. /**
  45. * Implements hook_entity_postsave().
  46. */
  47. function tripal_chado_entity_postsave($entity, $type) {
  48. }
  49. /**
  50. * Implements hook_entity_load().
  51. */
  52. function tripal_chado_entity_load($entities, $type) {
  53. if ($type == 'TripalBundle') {
  54. foreach ($entities as $bundle) {
  55. // We want to add in the record ID to the entity.
  56. if (property_exists($bundle, 'id')) {
  57. $chado_bundle = db_select('chado_bundle', 'cb')
  58. ->fields('cb')
  59. ->condition('cb.bundle_id', $bundle->id)
  60. ->execute()
  61. ->fetchObject();
  62. if ($chado_bundle) {
  63. $bundle->data_table = $chado_bundle->data_table;
  64. $bundle->type_linker_table = $chado_bundle->type_linker_table;
  65. $bundle->type_column = $chado_bundle->type_column;
  66. $bundle->type_id = $chado_bundle->type_id;
  67. $bundle->type_value = $chado_bundle->type_value;
  68. }
  69. }
  70. }
  71. }
  72. if ($type == 'TripalEntity') {
  73. foreach ($entities as $entity) {
  74. // We want to add in the record ID to the entity.
  75. if (property_exists($entity, 'id')) {
  76. // Set some defaults on vars needed by this module.
  77. $entity->chado_table = NULL;
  78. $entity->chado_column = NULL;
  79. $entity->chado_record = NULL;
  80. $entity->chado_record_id = NULL;
  81. // Add in the Chado table information for this entity type.
  82. $bundle = tripal_load_bundle_entity(array('name' => $entity->bundle));
  83. if (!$bundle) {
  84. continue;
  85. }
  86. // TODO: this may need fixing. The chado_column may not always
  87. // be the type_id of the base table. Is it expected to be so here?
  88. $entity->chado_table = $bundle->data_table;
  89. $entity->chado_column = $bundle->type_column;
  90. $chado_entity_table = chado_get_bundle_entity_table($bundle);
  91. $chado_entity = db_select($chado_entity_table, 'ce')
  92. ->fields('ce')
  93. ->condition('ce.entity_id', $entity->id)
  94. ->execute()
  95. ->fetchObject();
  96. if ($chado_entity) {
  97. $schema = chado_get_schema($entity->chado_table);
  98. $record = chado_generate_var($entity->chado_table, array($schema['primary key'][0] => $chado_entity->record_id));
  99. $entity->chado_record = $record;
  100. $entity->chado_record_id = $chado_entity->record_id;
  101. }
  102. }
  103. }
  104. }
  105. }
  106. /**
  107. *
  108. * Implements hook_entity_insert().
  109. */
  110. function tripal_chado_entity_insert($entity, $type) {
  111. }
  112. /**
  113. *
  114. * Implements hook_entity_update().
  115. */
  116. function tripal_chado_entity_update($entity, $type) {
  117. }
  118. /**
  119. *
  120. * Implements hook_entity_delete().
  121. */
  122. function tripal_chado_entity_delete($entity, $type) {
  123. if ($type == 'TripalEntity') {
  124. }
  125. }
  126. /**
  127. * Implements hook_tripal_default_title_format().
  128. *
  129. * Overrides the default titles.
  130. */
  131. function tripal_chado_tripal_default_title_format($bundle, $available_tokens) {
  132. $format = array();
  133. $table = $bundle->data_table;
  134. if ($table == 'organism') {
  135. if (chado_get_version() <= '1.2') {
  136. $format[] = array(
  137. 'format' => '[taxrank__genus] [taxrank__species]',
  138. 'weight' => -5
  139. );
  140. }
  141. else {
  142. $format[] = array(
  143. 'format' => '[taxrank__genus] [taxrank__species] [taxrank__infraspecific_taxon,TAXRANK:0000045]',
  144. 'weight' => -5
  145. );
  146. }
  147. }
  148. if ($table == 'arraydesign') {
  149. $format[] = array(
  150. 'format' => '[schema__name]',
  151. 'weight' => -5
  152. );
  153. }
  154. if ($table == 'assay') {
  155. $format[] = array(
  156. 'format' => '[schema__name]',
  157. 'weight' => -5
  158. );
  159. }
  160. if ($table == 'biomaterial') {
  161. $format[] = array(
  162. 'format' => '[schema__name]',
  163. 'weight' => -5
  164. );
  165. }
  166. if ($table == 'analysis') {
  167. $format[] = array(
  168. 'format' => '[schema__name]',
  169. 'weight' => -5
  170. );
  171. }
  172. if ($table == 'feature') {
  173. $format[] = array(
  174. 'format' => '[schema__name]',
  175. 'weight' => -5
  176. );
  177. }
  178. if ($table == 'featuremap') {
  179. $format[] = array(
  180. 'format' => '[schema__name]',
  181. 'weight' => -5
  182. );
  183. }
  184. if ($table == 'stock') {
  185. $format[] = array(
  186. 'format' => '[schema__name]',
  187. 'weight' => -5
  188. );
  189. }
  190. if ($table == 'pub') {
  191. $format[] = array(
  192. 'format' => '[tpub__title]',
  193. 'weight' => -5,
  194. );
  195. }
  196. if ($table == 'cvterm') {
  197. $format[] = array(
  198. 'format' => '[schema__name]',
  199. 'weight' => -5,
  200. );
  201. }
  202. if ($table == 'project') {
  203. $format[] = array(
  204. 'format' => '[schema__name]',
  205. 'weight' => -5,
  206. );
  207. }
  208. if ($table == 'contact') {
  209. $format[] = array(
  210. 'format' => '[schema__name]',
  211. 'weight' => -5,
  212. );
  213. }
  214. if ($table == 'phylotree') {
  215. $format[] = array(
  216. 'format' => '[schema__name]',
  217. 'weight' => -5,
  218. );
  219. }
  220. if ($table == 'library') {
  221. $format[] = array(
  222. 'format' => '[schema__name]',
  223. 'weight' => -5,
  224. );
  225. }
  226. if ($table == 'protocol') {
  227. $format[] = array(
  228. 'format' => '[schema__name]',
  229. 'weight' => -5,
  230. );
  231. }
  232. return $format;
  233. }
  234. /**
  235. * Implements hook_entity_view().
  236. */
  237. function tripal_chado_entity_view($entity, $type, $view_mode, $langcode) {
  238. // If this entity is a TripalEntity and is a full view, then
  239. // we want to support the legacy view, but only if the legacy
  240. // module is enabled (the functions exist).
  241. if ($type =='TripalEntity') {
  242. // Use the generic template to render the fields
  243. if ($view_mode == 'full') {
  244. // Get the Chado table for this data type.
  245. $bundle = tripal_load_bundle_entity(array('name' => $entity->bundle));
  246. $chado_table = $bundle->data_table;
  247. $chado_field = $bundle->type_column;
  248. // Get the list of templates that should be used for entities and generatte
  249. // the key in the array for this entity type (using the chado table the
  250. // entity maps to).
  251. $enabled_templates = variable_get('tripal_chado_enabled_legacy_templates', array());
  252. $legacy_template = 'legacy_template--chado_' . $chado_table;
  253. // If the site admin has indicated that this entity type should use
  254. // a legacy tmplate then prepare the entity and content to fake a
  255. // node.
  256. if (key_exists($legacy_template, $enabled_templates) && $enabled_templates[$legacy_template]) {
  257. // Remove the fields added by the chado_field_storage.
  258. $fields = field_info_fields();
  259. foreach($fields as $field) {
  260. if ($field['storage']['type'] == 'field_chado_storage' or
  261. $field['storage']['type'] == 'tripal_no_storage') {
  262. $field_name = $field['field_name'];
  263. if (property_exists($entity, $field_name)) {
  264. $entity->$field_name = NULL;
  265. unset($entity->content[$field_name]);
  266. }
  267. }
  268. }
  269. // Make the entity look like a node.
  270. $entity->type = 'chado_' . $chado_table;
  271. $entity->$chado_table = $entity->chado_record;
  272. // Add any node specific fields to the entity to fake the node.
  273. $node_schema = drupal_get_schema('node');
  274. foreach ($node_schema['fields'] as $field_name => $details) {
  275. if (!property_exists($entity, $field_name)) {
  276. $entity->$field_name = '';
  277. if (array_key_exists('default', $details)) {
  278. $entity->$field_name = $details['default'];
  279. }
  280. }
  281. }
  282. // Now call the module's node_view hook to add additional
  283. // content to our 'fake' entity node.
  284. $modules = module_list();
  285. foreach ($modules as $mname => $details) {
  286. $function_name = $mname . '_node_view';
  287. if (function_exists($function_name)) {
  288. $function_name($entity, $view_mode, $langcode);
  289. }
  290. }
  291. }
  292. }
  293. }
  294. }
  295. /**
  296. * Implements hook_entity_view_alter().
  297. *
  298. * This function is used to support legacy Tripal v2 templates
  299. * for use with Tripal v3 entities.
  300. */
  301. function tripal_chado_entity_view_alter(&$build) {
  302. // For the legacy support, we need to make sure the TOC
  303. // is built.
  304. if ($build['#entity_type'] == 'TripalEntity') {
  305. $enabled_templates = variable_get('tripal_chado_enabled_legacy_templates', array());
  306. $entity = $build['#entity'];
  307. $legacy_template = 'legacy_template--' . $entity->type;
  308. if (key_exists($legacy_template, $enabled_templates) && $enabled_templates[$legacy_template]) {
  309. $build['#entity']->nid = NULL;
  310. $build['#node'] = $build['#entity'];
  311. $modules = module_list();
  312. foreach ($modules as $mname => $details) {
  313. $function_name = $mname . '_node_view_alter';
  314. if (function_exists($function_name)) {
  315. $function_name($build);
  316. }
  317. }
  318. }
  319. }
  320. }