Reply to comment

CCK formatters - 3

Csináltam egy harmadik példát is ami date típusú mezőt jelenít meg táblázatban, de mivel az előző kettőnél már mindent leírtam amit akartam, különösebben nem részletezném. Annyit azért mindenképpen el kell mondjak, hogy a sminkelés alapját képező theme_date_display_combination() függvényből elég sok mindent kigyomláltam ezért csak olyan speciális date mezőknél használható tesztelhető ahol a Tól-Ig dátumok kitöltése kötelező.

  1. <?php
  2. // $Id$
  3.  
  4. /**
  5.  * @file
  6.  * myftr3.module
  7.  */
  8.  
  9. /**
  10.  * Implementation of hook_theme().
  11.  */
  12. function myftr3_theme() {
  13. return array(
  14. 'myftr3_formatter_date_table' => array(
  15. 'arguments' => array('element' => NULL),
  16. ),
  17. );
  18. }
  19.  
  20. /**
  21.  * Implementation of hook_field_formatter_info().
  22.  */
  23. function myftr3_field_formatter_info() {
  24. return array(
  25. 'date_table' => array(
  26. 'label' => t('Table'),
  27. 'field types' => array('date', 'datestamp', 'datetime'),
  28. 'multiple values' => CONTENT_HANDLE_MODULE
  29. ),
  30. );
  31. }
  32.  
  33. function theme_myftr3_formatter_date_table($element) {
  34. $node = $element['#node'];
  35. $field_name = $element['#field_name'];
  36. $context = !empty($node->content) && !empty($node->content[$field_name]) ? $node->content[$field_name]['#context'] : 'full';
  37. $type_name = $element['#type_name'];
  38. $field = content_fields($field_name);
  39.  
  40. // Get the formatter settings, either the default settings for this node
  41. // type or the View settings stored in $node->date_info.
  42. $options = date_formatter_get_settings($field_name, $type_name, $context);
  43. if (!empty($node->date_info) && !empty($node->date_info->formatter_settings)) {
  44. $options = $node->date_info->formatter_settings;
  45. }
  46.  
  47. $delta_from = $delta_to = FALSE;
  48. // If this is a full node or a pseudo node created by grouping
  49. // multiple values, see exactly which values are supposed to be visible.
  50. if (isset($node->$field_name)) {
  51. $node = date_prepare_node($node, $field, $type_name, $context, $options);
  52. // Adjust the $element values to match the changes.
  53. $element['#node'] = $node;
  54. }
  55. else {
  56. $max_count = $options['multiple']['multiple_number'];
  57. // If no results should be shown, empty the values and return.
  58. if (is_numeric($max_count) && $max_count == 0) {
  59. $node->{$field_name} = array();
  60. return '';
  61. }
  62.  
  63. if (!empty($options['multiple']['multiple_from'])) {
  64. $delta_from = $options['multiple']['multiple_from'] - 1;
  65. if (is_numeric($max_count)) {
  66. $delta_to = $delta_from + $max_count;
  67. }
  68. }
  69.  
  70. if ($delta_to === FALSE AND !empty($options['multiple']['multiple_to'])) {
  71. $delta_to = $options['multiple']['multiple_to'] ;
  72. }
  73. }
  74.  
  75. $rows = array();
  76. $diff_sum = 0;
  77. $gran = 4;
  78. foreach (element_children($element) as $delta) {
  79. // Did the current value get removed by formatter settings?
  80. if (isset($node->$field_name)) {
  81. if (empty($node->{$field_name}[$delta])) continue;
  82. }
  83. elseif ($delta_from !== FALSE AND $delta_from > $delta) {
  84. continue;
  85. }
  86. elseif ($delta_to !== FALSE AND $delta_to <= $delta) {
  87. break;
  88. }
  89.  
  90. $element['#item'] = $element[$delta]['#item'];
  91. $dates = date_formatter_process($element);
  92. // Pull the timezone, if any, out of the formatted result and tack it
  93. // back on at the end, if it is in the current formatted date.
  94. $timezone = $dates['value']['formatted_timezone'];
  95. if ($timezone) {
  96. $timezone = ' ' . $timezone;
  97. }
  98.  
  99. $date1 = str_replace($timezone, '', $dates['value']['formatted']);
  100. $date2 = str_replace($timezone, '', $dates['value2']['formatted']);
  101. $row = array();
  102. switch ($options['fromto']['fromto']) {
  103. case 'value':
  104. $row['from']['data'] = theme('date_display_single', $date1, $timezone);
  105. break;
  106.  
  107. case 'value2':
  108. $row['to']['data'] = theme('date_display_single', $date2, $timezone);
  109. break;
  110.  
  111. default:
  112. $diff = strtotime($dates['value2']['local']['datetime']) - strtotime($dates['value']['local']['datetime']);
  113. $diff_sum += $diff;
  114. $row['from']['data'] = theme('date_display_single', $date1, $timezone);
  115. $row['to']['data'] = theme('date_display_single', $date2, $timezone);
  116. $row['diff']['data'] = format_interval($diff, $gran);
  117. break;
  118. }
  119.  
  120. $rows[] = array('data' => $row);
  121. }
  122.  
  123. $header = array(
  124. 0 => array('data' => t('From')),
  125. 1 => array('data' => t('To')),
  126. 2 => array('data' => t('Diff')),
  127. );
  128.  
  129.  
  130. if ($options['fromto']['fromto'] != 'both') {
  131. unset($header[2]);
  132. unset($header[(($options['fromto']['fromto'] == 'value') ? 1 : 0)]);
  133. return theme('table', $header, $rows);
  134. }
  135. else {
  136. $tfoot = sprintf(
  137. '</thead><tfoot><tr><td colspan="3" align="right"><strong>%s</strong> %s</td></tr></tfoot><tbody>',
  138. t('Summary'),
  139. format_interval($diff_sum, $gran)
  140. );
  141. return str_replace("</thead>\n<tbody>", $tfoot, theme('table', $header, $rows));
  142. }
  143. }

Csatolmányok: 

Reply

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: [code], [blockcode]. PHP source code can also be enclosed in <?php ... ?> or <% ... %>.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.