tripal_ds.ds.inc 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. // Get the bundle and term objects.
  15. $bundle = tripal_load_bundle_entity(array('name' => $bundle_name));
  16. $term = tripal_load_term_entity(array('term_id' => $bundle->term_id));
  17. // Iterate through the fields of this bundle.
  18. foreach ($instances as $key => $instance){
  19. $instance_name = $instance['field_name'];
  20. if($instance_name=="rdfs__type"){
  21. array_push($summary_fields, $instance_name);
  22. $fields_with_regions[$instance_name]= 'right';
  23. }
  24. else {
  25. //TODO: How do we handle non-chado dbs, placement of fields within
  26. // tripal panes might need to be done in a hook.
  27. $instance_base_table = $instance['settings']['base_table'];
  28. $instance_base_chado = $instance['settings']['chado_table'];
  29. $prop_table = strpos($instance_base_chado, 'prop');
  30. $data_sequence = strpos($instance_name, 'data__sequence');
  31. if ($instance_base_chado && $instance_base_table){
  32. if ($instance_base_chado == $instance_base_table){
  33. if ($prop_table !== FALSE){
  34. // Properties section instances.
  35. array_push($prop_fields, $instance_name);
  36. $fields_with_regions[$instance_name]= 'right';
  37. }
  38. elseif ($data_sequence !== FALSE) {
  39. // Data sequence section instances.
  40. array_push($data_sequence_fields, $instance_name);
  41. $fields_with_regions[$instance_name] = 'right';
  42. }
  43. else {
  44. // Overview section instances.
  45. array_push($summary_fields, $instance_name);
  46. $fields_with_regions[$instance_name] = 'right';
  47. }
  48. }
  49. elseif ($instance_base_chado != $instance_base_table){
  50. if ($prop_table !== FALSE){
  51. // Properties section instances.
  52. array_push($prop_fields, $instance_name);
  53. $fields_with_regions[$instance_name]= 'right';
  54. }
  55. elseif ($data_sequence !== FALSE){
  56. // Data sequence section instances.
  57. array_push($data_sequence_fields, $instance_name);
  58. $fields_with_regions[$instance_name]= 'right';
  59. }
  60. else {
  61. // Linker section instances.
  62. array_push($all_other_fields, $instance);
  63. $fields_with_regions[$instance_name]= 'right';
  64. // Update the display settings so that the title is hidden.
  65. $instance['display']['default']['label'] = 'hidden';
  66. field_update_instance($instance);
  67. }
  68. }
  69. }
  70. else {
  71. // The tripal_chado module adds an image to the organism content
  72. // type so we want to make sure that image goes in the summary.
  73. // It is not a TripalField so it won't have a chado table.
  74. if ($instance_name == 'data__image' and $term->name == 'organism') {
  75. array_push($summary_fields, $instance_name);
  76. $fields_with_regions[$instance_name] = 'right';
  77. }
  78. }
  79. }
  80. $i++;
  81. }
  82. // Consolidate the field sets.
  83. if(!empty($summary_fields)){
  84. _summary_field_group_info($bundle_name, $summary_fields);
  85. // Add the fields to the regions.
  86. array_unshift($summary_fields, 'group_summary_tripalpane', 'group_summary', 'group_summary_table');
  87. $fields_with_regions += [ 'group_summary_tripalpane' =>'right', 'group_summary' => 'right', 'group_summary_table' => 'right' ];
  88. $region_right = array_merge($summary_fields, $region_right);
  89. }
  90. if (!empty($prop_fields)){
  91. _prop_field_group_info($bundle_name, $prop_fields);
  92. array_unshift($prop_fields, 'group_prop_tripalpane', 'group_prop', 'group_prop_table');
  93. // Add the fields to the regions.
  94. $region_right = array_merge($region_right, $prop_fields);
  95. $fields_with_regions += [ 'group_prop_tripalpane' => 'right', 'group_prop' => 'right', 'group_prop_table' => 'right' ];
  96. }
  97. if (!empty($data_sequence_fields)){
  98. _data_sequence_field_group_info($bundle_name, $data_sequence_fields);
  99. array_unshift($data_sequence_fields, 'group_sequence_tripalpane', 'group_sequence', 'group_sequence_table');
  100. // Add the fields to the regions.
  101. $region_right = array_merge($region_right, $data_sequence_fields);
  102. $fields_with_regions += [ 'group_sequence_tripalpane' => 'right', 'group_sequence' => 'right', 'group_sequence_table' => 'right' ];
  103. }
  104. if (!empty($all_other_fields)){
  105. foreach ($all_other_fields as $key => $other_field) {
  106. // Temporary field names.
  107. $temporary_field = array();
  108. $group_field_name = 'gp_'.$other_field['field_name'];
  109. $fieldset_field_name = 'ft_'.$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. $fieldset_field_name = substr($fieldset_field_name, 0, 27);
  114. // Add randomm numbers to ensure the field name is unique within the 32
  115. // character limit of the field.
  116. $group_field_name = $group_field_name.rand(0, 99999);
  117. $fieldset_field_name = $fieldset_field_name.rand(0, 99999);
  118. // Build the field group.
  119. _additional_fields_field_group_info($bundle_name, $other_field['label'], $group_field_name, $fieldset_field_name, $other_field['field_name']);
  120. // Update arrays.
  121. array_push($temporary_field, $group_field_name, $fieldset_field_name, $other_field['field_name']);
  122. $region_right = array_merge($region_right, $temporary_field);
  123. $fields_with_regions += [ $group_field_name => 'right', $fieldset_field_name => 'right' ];
  124. }
  125. }
  126. // Add blocks to $region_left.
  127. $position = count($region_right);
  128. $region_left[$position] = 'toc';
  129. $fields_with_regions += [ 'toc' => 'left'];
  130. // Build the ds layout.
  131. $record = new stdClass;
  132. $record->id ='TripalEntity|' . $bundle_name . '|default';
  133. $record->entity_type = 'TripalEntity';
  134. $record->bundle = $bundle_name;
  135. $record->view_mode = 'default';
  136. $record->layout = 'tripal_ds_feature';
  137. $settings = array(
  138. 'regions' => array(
  139. 'left' =>
  140. $region_left,
  141. 'right' =>
  142. $region_right,
  143. ),
  144. 'fields' =>
  145. $fields_with_regions,
  146. 'classes' => array(),
  147. 'wrappers' => array(
  148. 'left' => 'div',
  149. 'right' => 'div',
  150. ),
  151. 'layout_wrapper' => 'div',
  152. 'layout_attributes' => '',
  153. 'layout_attributes_merge' => 1,
  154. 'layout_link_attribute' => '',
  155. 'layout_link_custom' => '',
  156. 'layout_disable_css' => 0,
  157. );
  158. $record->settings = $settings;
  159. drupal_write_record('ds_layout_settings', $record);
  160. }
  161. /**
  162. * Implements hook_ds_fields_info().
  163. */
  164. function tripal_ds_ds_fields_info($entity_type) {
  165. $fields = array();
  166. $fields['toc'] = array(
  167. 'title' => t('Table of Contents'),
  168. 'field_type' => DS_FIELD_TYPE_FUNCTION,
  169. 'function' => 'tripal_ds_toc_block',
  170. );
  171. return array('TripalEntity' => $fields);
  172. }
  173. /**
  174. *
  175. * @param $entity_type
  176. * @return
  177. */
  178. function tripal_ds_toc_block($entity_type) {
  179. $bundle_name = $entity_type['bundle'];
  180. $toc = views_embed_view('tripal_content_type_toc', 'block', $bundle_name);
  181. return $toc;
  182. }