tripal_ds.ds.inc 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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', 'group_summary_table');
  88. $fields_with_regions += [ 'group_summary_tripalpane' =>'right', 'group_summary' => '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', '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' => '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', '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' => '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. $fieldset_field_name = 'ft_'.$other_field['field_name'];
  111. // Need to truncate the names because of database field size restrictions,
  112. // updating fields here to ensure name consistency.
  113. $group_field_name = substr($group_field_name, 0, 27);
  114. $fieldset_field_name = substr($fieldset_field_name, 0, 27);
  115. // Add randomm numbers to ensure the field name is unique within the 32
  116. // character limit of the field.
  117. $group_field_name = $group_field_name.rand(0, 99999);
  118. $fieldset_field_name = $fieldset_field_name.rand(0, 99999);
  119. // Build the field group.
  120. _additional_fields_field_group_info($bundle_name, $other_field['label'], $group_field_name, $fieldset_field_name, $other_field['field_name']);
  121. // Update arrays.
  122. array_push($temporary_field, $group_field_name, $fieldset_field_name, $other_field['field_name']);
  123. $region_right = array_merge($region_right, $temporary_field);
  124. $fields_with_regions += [ $group_field_name => 'right', $fieldset_field_name => 'right' ];
  125. }
  126. }
  127. // Add blocks to $region_left and build the toc field that is placed within.
  128. _ds_fields_info_write($bundle_name);
  129. $region_left += [ 'toc' ];
  130. $fields_with_regions += [ 'toc' => 'left' ];
  131. // Build the ds layout.
  132. $record = new stdClass;
  133. $record->id ='TripalEntity|' . $bundle_name . '|default';
  134. $record->entity_type = 'TripalEntity';
  135. $record->bundle = $bundle_name;
  136. $record->view_mode = 'default';
  137. $record->layout = 'tripal_ds_feature';
  138. $settings = array(
  139. 'regions' => array(
  140. 'left' =>
  141. $region_left,
  142. 'right' =>
  143. $region_right,
  144. ),
  145. 'fields' =>
  146. $fields_with_regions,
  147. 'classes' => array(),
  148. 'wrappers' => array(
  149. 'left' => 'div',
  150. 'right' => 'div',
  151. ),
  152. 'layout_wrapper' => 'div',
  153. 'layout_attributes' => '',
  154. 'layout_attributes_merge' => 1,
  155. 'layout_link_attribute' => '',
  156. 'layout_link_custom' => '',
  157. 'layout_disable_css' => 0,
  158. );
  159. $record->settings = $settings;
  160. drupal_write_record('ds_layout_settings', $record);
  161. }
  162. catch (Exception $e) {
  163. watchdog_exception('tripal_ds', $e);
  164. return FALSE;
  165. }
  166. return TRUE;
  167. }
  168. /**
  169. * Implements hook_ds_fields_info().
  170. */
  171. function tripal_ds_ds_fields_info($entity_type) {
  172. $fields = array();
  173. $fields['toc'] = array(
  174. 'title' => t('Table of Contents'),
  175. 'field_type' => DS_FIELD_TYPE_FUNCTION,
  176. 'function' => 'tripal_ds_toc_block',
  177. );
  178. return array('TripalEntity' => $fields);
  179. }
  180. /**
  181. *
  182. * @param $entity_type
  183. * @return
  184. */
  185. function tripal_ds_toc_block($entity_type) {
  186. $bundle_name = $entity_type['bundle'];
  187. $toc = views_embed_view('tripal_content_type_toc', 'block', $bundle_name);
  188. return $toc;
  189. }
  190. function _ds_fields_info_write($bundle_name) {
  191. $fields = new stdClass;
  192. $fields->id ='TripalEntity|' . $bundle_name . '|default';
  193. $fields->entity_type = 'TripalEntity';
  194. $fields->bundle = $bundle_name;
  195. $fields->view_mode = 'default';
  196. $fields->settings = array(
  197. 'toc' => array(
  198. 'weight' => 0,
  199. 'label' => 'hidden',
  200. 'format' => 'default',
  201. ),
  202. );
  203. drupal_write_record('ds_field_settings', $fields);
  204. }