commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-new / entity / views / handlers / entity_views_handler_field_duration.inc
1 <?php
2
3 /**
4 * @file
5 * Contains the entity_views_handler_field_duration class.
6 */
7
8 /**
9 * A handler to provide proper displays for duration properties retrieved via data selection.
10 *
11 * This handler may only be used in conjunction with data selection based Views
12 * tables or other base tables using a query plugin that supports data
13 * selection.
14 *
15 * @see entity_views_field_definition()
16 * @ingroup views_field_handlers
17 */
18 class entity_views_handler_field_duration extends views_handler_field {
19
20 /**
21 * Stores the entity type of the result entities.
22 */
23 public $entity_type;
24
25 /**
26 * Stores the result entities' metadata wrappers.
27 */
28 public $wrappers = array();
29
30 /**
31 * Stores the current value when rendering list fields.
32 */
33 public $current_value;
34
35 /**
36 * Overridden to add the field for the entity ID (if necessary).
37 */
38 public function query() {
39 EntityFieldHandlerHelper::query($this);
40 }
41
42 /**
43 * Adds a click-sort to the query.
44 */
45 public function click_sort($order) {
46 EntityFieldHandlerHelper::click_sort($this, $order);
47 }
48
49 /**
50 * Load the entities for all rows that are about to be displayed.
51 */
52 public function pre_render(&$values) {
53 parent::pre_render($values);
54 EntityFieldHandlerHelper::pre_render($this, $values);
55 }
56
57 /**
58 * Overridden to use a metadata wrapper.
59 */
60 public function get_value($values, $field = NULL) {
61 return EntityFieldHandlerHelper::get_value($this, $values, $field);
62 }
63
64 public function option_definition() {
65 $options = parent::option_definition();
66 $options += EntityFieldHandlerHelper::option_definition($this);
67
68 $options['format_interval'] = array('default' => TRUE);
69 $options['granularity'] = array('default' => 2);
70 $options['prefix'] = array('default' => '', 'translatable' => TRUE);
71 $options['suffix'] = array('default' => '', 'translatable' => TRUE);
72
73 return $options;
74 }
75
76 public function options_form(&$form, &$form_state) {
77 parent::options_form($form, $form_state);
78 EntityFieldHandlerHelper::options_form($this, $form, $form_state);
79
80 $form['format_interval'] = array(
81 '#type' => 'checkbox',
82 '#title' => t('Format interval'),
83 '#description' => t('If checked, the value will be formatted as a time interval. Otherwise, just the number of seconds will be displayed.'),
84 '#default_value' => $this->options['format_interval'],
85 );
86 $form['granularity'] = array(
87 '#type' => 'textfield',
88 '#title' => t('Granularity'),
89 '#default_value' => $this->options['granularity'],
90 '#description' => t('Specify how many different units to display.'),
91 '#dependency' => array('edit-options-format-interval' => array(TRUE)),
92 '#size' => 2,
93 );
94 $form['prefix'] = array(
95 '#type' => 'textfield',
96 '#title' => t('Prefix'),
97 '#default_value' => $this->options['prefix'],
98 '#description' => t('Text to put before the duration text.'),
99 );
100 $form['suffix'] = array(
101 '#type' => 'textfield',
102 '#title' => t('Suffix'),
103 '#default_value' => $this->options['suffix'],
104 '#description' => t('Text to put after the duration text.'),
105 );
106 }
107
108 /**
109 * Render the field.
110 *
111 * @param $values
112 * The values retrieved from the database.
113 */
114 public function render($values) {
115 return EntityFieldHandlerHelper::render($this, $values);
116 }
117
118 /**
119 * Render a single field value.
120 */
121 public function render_single_value($value, $values) {
122 if ($this->options['format_interval']) {
123 $value = format_interval($value, (int) $this->options['granularity']);
124 }
125 // Value sanitization is handled by the wrapper, see
126 // EntityFieldHandlerHelper::get_value().
127 return $this->sanitize_value($this->options['prefix'], 'xss') .
128 $value .
129 $this->sanitize_value($this->options['suffix'], 'xss');
130 }
131
132 }