Advertisement
kunago

tablefield rowspan and colspan

Aug 26th, 2017
663
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.16 KB | None | 0 0
  1. function yourthemename_tablefield($variables) {
  2. $header = $variables['header'];
  3. $rows = $variables['rows'];
  4. $attributes = $variables['attributes'];
  5. $caption = $variables['caption'];
  6. $colgroups = $variables['colgroups'];
  7. $header_orientation = $variables['header_orientation'];
  8. $sticky = $variables['sticky'];
  9. $no_striping = !$variables['striping'];
  10. $empty = $variables['empty'];
  11. if ($variables['sortable']) {
  12. drupal_add_css(drupal_get_path('module', 'tablefield') . '/css/tablefield_sort.css');
  13. $tooltip = ' title="' . t('Sort column') . '"';
  14. }
  15. else {
  16. $tooltip = NULL;
  17. }
  18. drupal_add_css(drupal_get_path('module', 'tablefield') . '/css/tablefield_sort.css'); $empty = $variables['empty'];
  19. // If the only header is the first column make the first row a normal row.
  20. if (!isset($rows['row_0']) && $header_orientation == 'Vertical') {
  21. $rows['row_0'] = $header;
  22. ksort($rows);
  23. $header = array();
  24. }
  25. // Add sticky headers, if applicable.
  26. if (count($header) && $sticky) {
  27. drupal_add_js('misc/tableheader.js');
  28. // Add 'sticky-enabled' class to the table to identify it for JS.
  29. // This is needed to target tables constructed by this function.
  30. $attributes['class'][] = 'sticky-enabled';
  31. }
  32.  
  33. $output = '<table' . drupal_attributes($attributes) . ">\n";
  34.  
  35. if (isset($caption)) {
  36. $output .= '<caption>' . $caption . "</caption>\n";
  37. }
  38.  
  39. // Format the table columns:
  40. if (count($colgroups)) {
  41. foreach ($colgroups as $number => $colgroup) {
  42. $attributes = array();
  43.  
  44. // Check if we're dealing with a simple or complex column.
  45. if (isset($colgroup['data'])) {
  46. foreach ($colgroup as $key => $value) {
  47. if ($key == 'data') {
  48. $cols = $value;
  49. }
  50. else {
  51. $attributes[$key] = $value;
  52. }
  53. }
  54. }
  55. else {
  56. $cols = $colgroup;
  57. }
  58.  
  59. // Build colgroup.
  60. if (is_array($cols) && count($cols)) {
  61. $output .= ' <colgroup' . drupal_attributes($attributes) . '>';
  62. $i = 0;
  63. foreach ($cols as $col) {
  64. $output .= ' <col' . drupal_attributes($col) . ' />';
  65. }
  66. $output .= " </colgroup>\n";
  67. }
  68. else {
  69. $output .= ' <colgroup' . drupal_attributes($attributes) . " />\n";
  70. }
  71. }
  72. }
  73.  
  74. // Add the 'empty' row message if available.
  75. if (!count($rows) && $empty) {
  76. $header_count = 0;
  77. foreach ($header as $header_cell) {
  78. if (is_array($header_cell)) {
  79. $header_count += isset($header_cell['colspan']) ? $header_cell['colspan'] : 1;
  80. }
  81. else {
  82. $header_count++;
  83. }
  84. }
  85. $rows[] = array(
  86. array(
  87. 'data' => $empty,
  88. 'colspan' => $header_count,
  89. 'class' => array('empty', 'message'),
  90. ),
  91. );
  92. }
  93.  
  94. // Add rowspan and colspan based on empty cells
  95. $previous_row = false;
  96. foreach ($rows as $row => $columns) {
  97. foreach ($columns as $col => $cell) {
  98. preg_match("/(?:#colspan#)/", $cell['data'], $match);
  99. // Is #colspan# in the data?
  100. if(!empty($match)) {
  101. $keys = array_keys($rows[$row]);
  102. $search = array_search($col, $keys);
  103. $prev = $keys[intval($search) - 1];
  104. $colspan = isset($rows[$row][$prev]['colspan']) ? $rows[$row][$prev]['colspan'] : 1;
  105. $rows[$row][$prev]['colspan'] = $colspan + 1;
  106. unset($rows[$row][$col]);
  107. }
  108.  
  109. preg_match("/(?:#rowspan#)/", $cell['data'], $match);
  110. // Is #rowpsan# in the data?
  111. if(!empty($match)) {
  112. if($previous_row !== false) {
  113. $keys = array_keys($rows[$row]);
  114. $search = array_search($col,$keys);
  115. $col = $keys[intval($search)];
  116.  
  117. $prev_row = $previous_row;
  118. while (true) {
  119. if(isset($rows[$prev_row][$col])) {
  120. $rowspan = isset($rows[$prev_row][$col]['rowspan']) ? $rows[$prev_row][$col]['rowspan'] : 1;
  121. $rows[$prev_row][$col]['rowspan'] = $rowspan + 1;
  122. break;
  123. }
  124. $prev_row--;
  125. }
  126. unset($rows[$row][$col]);
  127. }
  128. }
  129. }
  130. $previous_row = $row;
  131. }
  132.  
  133. // Format the table header:
  134. if (count($header)) {
  135. $ts = tablesort_init($header);
  136. // HTML requires that the thead tag has tr tags in it followed by tbody
  137. // tags. Using ternary operator to check and see if we have any rows.
  138. $output .= (count($rows) ? ' <thead' . $tooltip . '><tr>' : ' <tr>');
  139. foreach ($header as $cell) {
  140. $cell = tablesort_header($cell, $header, $ts);
  141. $output .= _theme_table_cell($cell, TRUE);
  142. }
  143. // Using ternary operator to close the tags based on whether or not there
  144. // are rows.
  145. $output .= (count($rows) ? " </tr></thead>\n" : "</tr>\n");
  146. }
  147. else {
  148. $ts = array();
  149. }
  150.  
  151. // Format the table rows:
  152. if (count($rows)) {
  153. $output .= "<tbody>\n";
  154. $flip = array('even' => 'odd', 'odd' => 'even');
  155. $class = 'even';
  156. foreach ($rows as $number => $row) {
  157. // Check if we're dealing with a simple or complex row.
  158. if (isset($row['data'])) {
  159. $cells = $row['data'];
  160. $no_striping = isset($row['no_striping']) ? $row['no_striping'] : FALSE;
  161.  
  162. // Set the attributes array and exclude 'data' and 'no_striping'.
  163. $attributes = $row;
  164. unset($attributes['data']);
  165. unset($attributes['no_striping']);
  166. }
  167. else {
  168. $cells = $row;
  169. $attributes = array();
  170. }
  171. if (count($cells)) {
  172. // Add odd/even class.
  173. if (!$no_striping) {
  174. $class = $flip[$class];
  175. $attributes['class'][] = $class;
  176. }
  177.  
  178. // Build row.
  179. $output .= ' <tr' . drupal_attributes($attributes) . '>';
  180. $i = 0;
  181. foreach ($cells as $cell) {
  182. $cell = tablesort_cell($cell, $header, $ts, $i++);
  183. $output .= _theme_table_cell($cell, ($header_orientation === 'Both' || $header_orientation === 'Vertical') && $i == 1);
  184. $i++;
  185. }
  186. $output .= " </tr>\n";
  187. }
  188. }
  189. $output .= "</tbody>\n";
  190. }
  191.  
  192. $output .= "</table>\n";
  193. return $output;
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement