tripal_ds.ds.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. <?php
  2. /**
  3. * Implements hook_ds_layout_settings_info().
  4. */
  5. function _ds_layout_settings_info($bundle_name, $instances) {
  6. $region_right = array();
  7. $region_left = array();
  8. $prop_fields = array();
  9. $summary_fields = array();
  10. $data_sequence_fields = array();
  11. $all_other_fields = array();
  12. $fields_with_regions = array();
  13. $i = 0;
  14. try {
  15. // Get the bundle and term objects.
  16. $bundle = tripal_load_bundle_entity(array('name' => $bundle_name));
  17. $term = tripal_load_term_entity(array('term_id' => $bundle->term_id));
  18. // Iterate through the fields of this bundle.
  19. foreach ($instances as $key => $instance){
  20. $instance_name = $instance['field_name'];
  21. if($instance_name=="rdfs__type"){
  22. array_push($summary_fields, $instance_name);
  23. $fields_with_regions[$instance_name]= 'right';
  24. }
  25. else {
  26. //TODO: How do we handle non-chado dbs, placement of fields within
  27. // tripal panes might need to be done in a hook.
  28. $instance_base_table = array_key_exists('base_table', $instance['settings']) ? $instance['settings']['base_table'] : '';
  29. $instance_base_chado = array_key_exists('chado_table', $instance['settings']) ? $instance['settings']['chado_table'] : '';
  30. $prop_table = strpos($instance_base_chado, 'prop');
  31. $data_sequence = strpos($instance_name, 'data__sequence');
  32. if ($instance_base_chado && $instance_base_table){
  33. if ($instance_base_chado == $instance_base_table){
  34. if ($prop_table !== FALSE){
  35. // Properties section instances.
  36. array_push($prop_fields, $instance_name);
  37. $fields_with_regions[$instance_name]= 'right';
  38. }
  39. elseif ($data_sequence !== FALSE) {
  40. // Data sequence section instances.
  41. array_push($data_sequence_fields, $instance_name);
  42. $fields_with_regions[$instance_name] = 'right';
  43. }
  44. else {
  45. // Overview section instances.
  46. array_push($summary_fields, $instance_name);
  47. $fields_with_regions[$instance_name] = 'right';
  48. }
  49. }
  50. elseif ($instance_base_chado != $instance_base_table){
  51. if ($prop_table !== FALSE){
  52. // Properties section instances.
  53. array_push($prop_fields, $instance_name);
  54. $fields_with_regions[$instance_name]= 'right';
  55. }
  56. elseif ($data_sequence !== FALSE){
  57. // Data sequence section instances.
  58. array_push($data_sequence_fields, $instance_name);
  59. $fields_with_regions[$instance_name]= 'right';
  60. }
  61. else {
  62. // Linker section instances.
  63. array_push($all_other_fields, $instance);
  64. $fields_with_regions[$instance_name]= 'right';
  65. // Update the display settings so that the title is hidden.
  66. $instance['display']['default']['label'] = 'hidden';
  67. field_update_instance($instance);
  68. }
  69. }
  70. }
  71. else {
  72. // The tripal_chado module adds an image to the organism content
  73. // type so we want to make sure that image goes in the summary.
  74. // It is not a TripalField so it won't have a chado table.
  75. if ($instance_name == 'data__image' and $term->name == 'organism') {
  76. array_push($summary_fields, $instance_name);
  77. $fields_with_regions[$instance_name] = 'right';
  78. }
  79. }
  80. }
  81. $i++;
  82. }
  83. // Consolidate the field sets.
  84. if(!empty($summary_fields)){
  85. _summary_field_group_info($bundle_name, $summary_fields);
  86. // Add the fields to the regions.
  87. array_unshift($summary_fields, 'group_summary_tripalpane', 'group_summary_table');
  88. $fields_with_regions += [ 'group_summary_tripalpane' =>'right', 'group_summary_table' => 'right' ];
  89. $region_right = array_merge($summary_fields, $region_right);
  90. }
  91. if (!empty($prop_fields)){
  92. _prop_field_group_info($bundle_name, $prop_fields);
  93. array_unshift($prop_fields, 'group_prop_tripalpane', 'group_prop_table');
  94. // Add the fields to the regions.
  95. $region_right = array_merge($region_right, $prop_fields);
  96. $fields_with_regions += [ 'group_prop_tripalpane' => 'right', 'group_prop_table' => 'right' ];
  97. }
  98. if (!empty($data_sequence_fields)){
  99. _data_sequence_field_group_info($bundle_name, $data_sequence_fields);
  100. array_unshift($data_sequence_fields, 'group_sequence_tripalpane', 'group_sequence_table');
  101. // Add the fields to the regions.
  102. $region_right = array_merge($region_right, $data_sequence_fields);
  103. $fields_with_regions += [ 'group_sequence_tripalpane' => 'right', 'group_sequence_table' => 'right' ];
  104. }
  105. if (!empty($all_other_fields)){
  106. foreach ($all_other_fields as $key => $other_field) {
  107. // Temporary field names.
  108. $temporary_field = array();
  109. $group_field_name = 'gp_'.$other_field['field_name'];
  110. // Need to truncate the names because of database field size restrictions,
  111. // updating fields here to ensure name consistency.
  112. $group_field_name = substr($group_field_name, 0, 27);
  113. // Add random numbers to ensure the field name is unique within the 32
  114. // character limit of the field.
  115. $group_field_name = $group_field_name.rand(0, 99999);
  116. // Build the field group.
  117. _additional_fields_field_group_info($bundle_name, $other_field['label'], $group_field_name, $other_field['field_name']);
  118. // Update arrays.
  119. array_push($temporary_field, $group_field_name, $other_field['field_name']);
  120. $region_right = array_merge($region_right, $temporary_field);
  121. $fields_with_regions += [ $group_field_name => 'right',];
  122. }
  123. }
  124. // Add blocks to $region_left and build the toc field that is placed within.
  125. _ds_fields_info_write($bundle_name);
  126. $region_left += [ 'toc' ];
  127. $fields_with_regions += [ 'toc' => 'left' ];
  128. // Build the ds layout.
  129. $record = new stdClass;
  130. $record->id ='TripalEntity|' . $bundle_name . '|default';
  131. $record->entity_type = 'TripalEntity';
  132. $record->bundle = $bundle_name;
  133. $record->view_mode = 'default';
  134. $record->layout = 'tripal_ds_feature';
  135. $settings = array(
  136. 'regions' => array(
  137. 'left' =>
  138. $region_left,
  139. 'right' =>
  140. $region_right,
  141. ),
  142. 'fields' =>
  143. $fields_with_regions,
  144. 'classes' => array(),
  145. 'wrappers' => array(
  146. 'left' => 'div',
  147. 'right' => 'div',
  148. ),
  149. 'layout_wrapper' => 'div',
  150. 'layout_attributes' => '',
  151. 'layout_attributes_merge' => 1,
  152. 'layout_link_attribute' => '',
  153. 'layout_link_custom' => '',
  154. 'layout_disable_css' => 0,
  155. );
  156. $record->settings = $settings;
  157. drupal_write_record('ds_layout_settings', $record);
  158. }
  159. catch (Exception $e) {
  160. watchdog_exception('tripal_ds', $e);
  161. return FALSE;
  162. }
  163. return TRUE;
  164. }
  165. /**
  166. * Implements hook_ds_layout_settings_info().
  167. */
  168. function _ds_layout_pub_settings_info($bundle_name, $instances) {
  169. $region_right = array();
  170. $region_left = array();
  171. $properties= array();
  172. $all_fields = array();
  173. $instances_for_field_groups = array();
  174. $disabled_instances = array();
  175. try {
  176. // Add Abstract, Citation, DB Cross Reference, Properties.
  177. $all_fields['tpub__abstract']= 'right';
  178. $all_fields['tpub__citation']= 'right';
  179. $all_fields['sbo__database_cross_reference']= 'right';
  180. $all_fields['tpub__publication_type']= 'right';
  181. $all_fields['tpub__doi']= 'right';
  182. $all_fields['tpub__publication_date']= 'right';
  183. $all_fields['sio__references']= 'right';
  184. // Iterate through the fields of this bundle.
  185. foreach ($instances as $key => $instance) {
  186. $instance_name = $instance['field_name'];
  187. if($instance_name == 'tpub__abstract' || $instance_name == 'tpub__citation' || $instance_name == 'sbo__database_cross_reference' || $instance_name == 'sio__references'){
  188. array_push($instances_for_field_groups, $instance);
  189. // Update the display settings so that the title is hidden.
  190. $instance['display']['default']['label'] = 'hidden';
  191. field_update_instance($instance);
  192. }
  193. elseif($instance_name == 'tpub__publication_type' || $instance_name == 'tpub__doi' || $instance_name == 'tpub__publication_date') {
  194. array_push($properties, $instance);
  195. }
  196. else {
  197. array_push($disabled_instances, $instance);
  198. }
  199. }
  200. //Publication fields that are not going in the properties table.
  201. foreach ($instances_for_field_groups as $key => $other_field) {
  202. // Temporary field names.
  203. $temporary_field = array();
  204. $group_field_name = 'gp_'.$other_field['field_name'];
  205. // Need to truncate the names because of database field size restrictions,
  206. // updating fields here to ensure name consistency.
  207. $group_field_name = substr($group_field_name, 0, 27);
  208. // Add randomm numbers to ensure the field name is unique within the 32
  209. // character limit of the field.
  210. $group_field_name = $group_field_name.rand(0, 99999);
  211. // Build the field group.
  212. _additional_fields_field_group_info($bundle_name, $other_field['label'], $other_field['field_name']);
  213. // Update arrays.
  214. array_push($temporary_field, $group_field_name, $other_field['field_name']);
  215. $region_right = array_merge($region_right, $temporary_field);
  216. $all_fields += [ $group_field_name => 'right', ];
  217. }
  218. //Properties table fields.
  219. if(!empty($properties)){
  220. _publication_prop_field_group_info($bundle_name, $properties);
  221. array_unshift($properties, 'group_prop_tripalpane', 'group_prop_table');
  222. $region_right = array_merge($region_right, $properties);
  223. $all_fields+= [ 'group_prop_tripalpane' => 'right', 'group_prop_table' => 'right' ];
  224. }
  225. // Add blocks to $region_left and build the toc field that is placed within.
  226. _ds_fields_info_write($bundle_name);
  227. $region_left += [ 'toc' ];
  228. $all_fields += [ 'toc' => 'left' ];
  229. // Build the ds layout.
  230. $record = new stdClass;
  231. $record->id ='TripalEntity|' . $bundle_name . '|default';
  232. $record->entity_type = 'TripalEntity';
  233. $record->bundle = $bundle_name;
  234. $record->view_mode = 'default';
  235. $record->layout = 'tripal_ds_feature';
  236. $settings = array(
  237. 'regions' => array(
  238. 'left' =>
  239. $region_left,
  240. 'right' =>
  241. $region_right,
  242. ),
  243. 'fields' =>
  244. $all_fields,
  245. 'classes' => array(),
  246. 'wrappers' => array(
  247. 'left' => 'div',
  248. 'right' => 'div',
  249. ),
  250. 'layout_wrapper' => 'div',
  251. 'layout_attributes' => '',
  252. 'layout_attributes_merge' => 1,
  253. 'layout_link_attribute' => '',
  254. 'layout_link_custom' => '',
  255. 'layout_disable_css' => 0,
  256. );
  257. $record->settings = $settings;
  258. drupal_write_record('ds_layout_settings', $record);
  259. }
  260. catch (Exception $e) {
  261. watchdog_exception('tripal_ds', $e);
  262. return FALSE;
  263. }
  264. return TRUE;
  265. }
  266. /**
  267. * Implements hook_ds_fields_info().
  268. */
  269. function tripal_ds_ds_fields_info($entity_type) {
  270. $fields = array();
  271. $fields['toc'] = array(
  272. 'title' => t('Table of Contents'),
  273. 'field_type' => DS_FIELD_TYPE_FUNCTION,
  274. 'function' => 'tripal_ds_toc_block',
  275. );
  276. return array('TripalEntity' => $fields);
  277. }
  278. /**
  279. *
  280. * @param $entity_type
  281. * @return
  282. */
  283. function tripal_ds_toc_block($entity_type) {
  284. $bundle_name = $entity_type['bundle'];
  285. $toc = views_embed_view('tripal_content_type_toc', 'block', $bundle_name);
  286. return $toc;
  287. }
  288. function _ds_fields_info_write($bundle_name) {
  289. $fields = new stdClass;
  290. $fields->id ='TripalEntity|' . $bundle_name . '|default';
  291. $fields->entity_type = 'TripalEntity';
  292. $fields->bundle = $bundle_name;
  293. $fields->view_mode = 'default';
  294. $fields->settings = array(
  295. 'toc' => array(
  296. 'weight' => 0,
  297. 'label' => 'hidden',
  298. 'format' => 'default',
  299. ),
  300. );
  301. drupal_write_record('ds_field_settings', $fields);
  302. }