tripal_chado.entity.inc 11 KB

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