tripal_chado.entity.inc 10 KB

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