-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocess.view.fields.inc
More file actions
96 lines (93 loc) · 3.53 KB
/
preprocess.view.fields.inc
File metadata and controls
96 lines (93 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
/**
* Implements template_preprocess_views_view_fields.
*
* I was developing this code to change the format of start and end dates
* for event nodes displayed by a view. It turned out there is a Drupal
* module that provides this functionality, which is better than changing
* the HTML markup on the fly like this.
* This code depends on markup and CSS class names remaining static which
* may not be the case over the long term.
* Despite the drawbacks, I wanted to preserve the code because it uses things
* like DOMDocument which I've never used before.
*
* I had this code in the template.php file for the active theme of the D7 site.
*/
function TEMPLATE_preprocess_views_view_fields(&$vars) {
if ($vars['view']->name == 'events') {
// First remove 'to' from start and end date markup.
if (strpos($vars['fields']['field_event_date']->content, 'to') !== FALSE) {
$vars['fields']['field_event_date']->content = str_replace('to', '', $vars['fields']['field_event_date']->content);
}
$dom = new DOMDocument();
$dom->loadHTML($vars['fields']['field_event_date']->content);
$spans = $dom->getElementsByTagName('span');
$date_info = array();
foreach ($spans as $span) {
$date_info[$span->getAttribute('class')] = array(
'span' => $dom->saveHTML($span),
'raw_date' => $span->getAttribute('content'),
'value' => $span->nodeValue,
);
}
if (array_key_exists('date-display-single', $date_info)) {
$single_date = TEMPLATE_process_single_event_date($date_info['date-display-single']['value']);
$dom->getElementsByTagName('span')->item(0)->nodeValue = $single_date;
$vars['fields']['field_event_date']->content = $dom->saveHTML();
}
elseif (array_key_exists('date-display-start', $date_info) && array_key_exists('date-display-end', $date_info)) {
$dates_array = TEMPLATE_process_start_end_event_dates($date_info['date-display-start']['value'], $date_info['date-display-end']['value']);
$dom->getElementsByTagName('span')->item(0)->nodeValue = $dates_array['start_date'];
$dom->getElementsByTagName('span')->item(1)->nodeValue = $dates_array['end_date'];
$vars['fields']['field_event_date']->content = $dom->saveHTML();
}
}
}
/**
* Format date output for a single date.
*
* @param string $date
* The input date string.
*
* @return string $date
* A formatted date string.
*/
function TEMPLATE_process_single_event_date($date) {
if (($timestamp = strtotime($date)) != FALSE) {
$date = date('M d Y', $timestamp);
}
return $date;
}
/**
* Format date output for start and end dates.
*
* @param string $start_date
* The input start date string.
* @param string $end_date
* The input end date string.
*
* @return array
* An associative array containing the formatted start and end dates.
*/
function TEMPLATE_process_start_end_event_dates($start_date, $end_date) {
$start_date_fmt = '';
$end_date_fmt = '';
if (($start_timestamp = strtotime($start_date)) != FALSE) {
if (($end_timestamp = strtotime($end_date)) != FALSE) {
$start_month = date('M', $start_timestamp);
$end_month = date('M', $end_timestamp);
if ($start_month == $end_month) {
$start_date_fmt = date('M d', $start_timestamp);
$end_date_fmt .= date(' - d Y', $end_timestamp);
}
else {
$start_date_fmt = date('M d', $start_timestamp);
$end_date_fmt .= date(' - M d Y', $end_timestamp);
}
}
}
return array(
'start_date' => $start_date_fmt,
'end_date' => $end_date_fmt,
);
}