-
Notifications
You must be signed in to change notification settings - Fork 0
/
d7dev.module
57 lines (49 loc) · 1.7 KB
/
d7dev.module
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
<?php
/**
* Implements hook_field_formatter_info().
*/
function d7dev_field_formatter_info() {
return array(
'd7dev_integer_duration' => array(
'label' => t('Duration'),
'field types' => array('number_integer'),
)
);
}
/**
* Implements hook_field_formatter_view().
*/
function d7dev_field_formatter_view($entity_type, $entity, $field,
$instance, $langcode, $items, $display) {
$element = array();
$settings = $display['settings'];
switch ($display['type']) {
case 'd7dev_integer_duration':
foreach ($items as $delta => $item) {
//some simple math to covert the duration minutes to hours
//and the remainder as minutes
//divide by minutes in 1 hour and get floor
$hours = floor($item['value']/60);
//use the modulus to get the remainder of minutes
$minutes = $item['value']%60;
//get greatest common denominator of minutes to convert to fraction of hours
$minutes_gcd = gcd($minutes, 60);
//⁄ is the html entity for the fraction separator, and we
// use the sup and sub html element to give the appearance of a
// fraction
$minutes_fraction = '<sup>' . $minutes/$minutes_gcd .
'</sup>⁄<sub>' . 60/$minutes_gcd . '</sub>';
//markup change color to red if hours > 0 (TRUE)
$markup = $hours > 0 ? '<span style="color: #ff0000;">' . $hours . ' and ' . $minutes_fraction . ' hours' . '</span>' : $minutes_fraction . ' hour';
//finally, return our formatted value as the markup for this field
//formatter
$element[$delta] = array('#markup' => $markup);
}
break;
}
return $element;
}
function gcd($a, $b) {
$b = ( $a == 0)? 0 : $b;
return ($a % $b)? gcd($b, abs($a - $b)) :$b;
}