2
// $Id: views_handler_field.inc,v 1.14 2009/04/08 05:23:56 merlinofchaos Exp $
4
* @defgroup views_field_handlers Views' field handlers
6
* Handlers to tell Views how to build and display fields.
11
* Base field handler that has no options and renders an unformatted field.
14
* - additional fields: An array of fields that should be added to the query
15
* for some purpose. The array is in the form of:
16
* array('identifier' => array('table' => tablename,
17
* 'field' => fieldname); as many fields as are necessary
18
* may be in this array.
19
* - click sortable: If TRUE, this field may be click sorted.
21
class views_handler_field extends views_handler {
22
var $field_alias = 'unknown';
23
var $aliases = array();
26
* Construct a new field handler.
28
function construct() {
31
$this->additional_fields = array();
32
if (!empty($this->definition['additional fields'])) {
33
$this->additional_fields = $this->definition['additional fields'];
36
if (!isset($this->options['exclude'])) {
37
$this->options['exclude'] = '';
42
* Determine if this field can allow advanced rendering.
44
* Fields can set this to FALSE if they do not wish to allow
45
* token based rewriting or link-making.
47
function allow_advanced_render() {
51
function init(&$view, $options) {
52
parent::init($view, $options);
54
$this->options += array(
60
* Called to add the field to a query.
63
$this->ensure_my_table();
65
$this->field_alias = $this->query->add_field($this->table_alias, $this->real_field);
67
$this->add_additional_fields();
71
* Add 'additional' fields to the query.
74
* An array of fields. The key is an identifier used to later find the
75
* field alias used. The value is either a string in which case it's
76
* assumed to be a field on this handler's table; or it's an array in the
78
* @code array('table' => $tablename, 'field' => $fieldname) @endcode
80
function add_additional_fields($fields = NULL) {
81
if (!isset($fields)) {
83
if (empty($this->additional_fields)) {
86
$fields = $this->additional_fields;
88
if (!empty($fields) && is_array($fields)) {
89
foreach ($fields as $identifier => $info) {
90
if (is_array($info)) {
91
if (isset($info['table'])) {
92
$table_alias = $this->query->ensure_table($info['table'], $this->relationship);
95
$table_alias = $this->table_alias;
97
$this->aliases[$identifier] = $this->query->add_field($table_alias, $info['field']);
100
$this->aliases[$info] = $this->query->add_field($this->table_alias, $info);
107
* Called to determine what to tell the clicksorter.
109
function click_sort($order) {
110
$this->query->add_orderby($this->table, $this->field, $order, $this->field_alias);
114
* Determine if this field is click sortable.
116
function click_sortable() {
117
return !empty($this->definition['click sortable']);
121
* Get this field's label.
124
if (!isset($this->options['label'])) {
127
return $this->options['label'];
131
* Return DIV or SPAN based upon the field's element type.
133
function element_type() {
134
if (isset($this->definition['element type'])) {
135
return $this->definition['element type'];
141
function option_definition() {
142
$options = parent::option_definition();
144
$options['label'] = array('default' => $this->definition['title'], 'translatable' => TRUE);
145
$options['alter'] = array(
147
'alter_text' => array('default' => FALSE),
148
'text' => array('default' => '', 'translatable' => TRUE),
149
'make_link' => array('default' => FALSE),
150
'path' => array('default' => '', 'translatable' => TRUE),
151
'alt' => array('default' => '', 'translatable' => TRUE),
152
'prefix' => array('default' => '', 'translatable' => TRUE),
153
'suffix' => array('default' => '', 'translatable' => TRUE),
154
'trim' => array('default' => FALSE),
155
'max_length' => array('default' => ''),
156
'word_boundary' => array('default' => TRUE),
157
'ellipsis' => array('default' => TRUE),
158
'strip_tags' => array('default' => FALSE),
159
'html' => array('default' => FALSE),
167
* Default options form that provides the label widget that all fields
170
function options_form(&$form, &$form_state) {
171
$form['label'] = array(
172
'#type' => 'textfield',
173
'#title' => t('Label'),
174
'#default_value' => isset($this->options['label']) ? $this->options['label'] : '',
175
'#description' => t('The label for this field that will be displayed to end users if the style requires it.'),
177
$form['exclude'] = array(
178
'#type' => 'checkbox',
179
'#title' => t('Exclude from display'),
180
'#default_value' => $this->options['exclude'],
181
'#description' => t('Check this box to not display this field, but still load it in the view. Use this option to not show a grouping field in each record, or when doing advanced theming.'),
184
if ($this->allow_advanced_render()) {
185
$form['alter']['#tree'] = TRUE;
186
$form['alter']['alter_text'] = array(
187
'#type' => 'checkbox',
188
'#title' => t('Rewrite the output of this field'),
189
'#description' => t('If checked, you can alter the output of this field by specifying a string of text with replacement tokens that can use any existing field output.'),
190
'#default_value' => $this->options['alter']['alter_text'],
193
$form['alter']['text'] = array(
194
'#title' => t('Text'),
195
'#type' => 'textarea',
196
'#default_value' => $this->options['alter']['text'],
197
'#description' => t('The text to display for this field. You may include HTML. You may enter data from this view as per the "Replacement patterns" below.'),
198
'#process' => array('views_process_dependency'),
199
'#dependency' => array(
200
'edit-options-alter-alter-text' => array(1)
204
$form['alter']['make_link'] = array(
205
'#type' => 'checkbox',
206
'#title' => t('Output this field as a link'),
207
'#description' => t('If checked, this field will be made into a link. The destination must be given below.'),
208
'#default_value' => $this->options['alter']['make_link'],
210
$form['alter']['path'] = array(
211
'#title' => t('Link path'),
212
'#type' => 'textfield',
213
'#default_value' => $this->options['alter']['path'],
214
'#description' => t('The Drupal path or absolute URL for this link. You may enter data from this view as per the "Replacement patterns" below.'),
215
'#process' => array('views_process_dependency'),
216
'#dependency' => array(
217
'edit-options-alter-make-link' => array(1)
220
$form['alter']['alt'] = array(
221
'#title' => t('Alt text'),
222
'#type' => 'textfield',
223
'#default_value' => $this->options['alter']['alt'],
224
'#description' => t('Text to place as "alt" text which most browsers display as a tooltip when hovering over the link.'),
225
'#process' => array('views_process_dependency'),
226
'#dependency' => array(
227
'edit-options-alter-make-link' => array(1)
230
$form['alter']['prefix'] = array(
231
'#title' => t('Prefix text'),
232
'#type' => 'textfield',
233
'#default_value' => $this->options['alter']['prefix'],
234
'#description' => t('Any text to display before this link. You may include HTML.'),
235
'#process' => array('views_process_dependency'),
236
'#dependency' => array(
237
'edit-options-alter-make-link' => array(1)
240
$form['alter']['suffix'] = array(
241
'#title' => t('Suffix text'),
242
'#type' => 'textfield',
243
'#default_value' => $this->options['alter']['suffix'],
244
'#description' => t('Any text to display after this link. You may include HTML.'),
245
'#process' => array('views_process_dependency'),
246
'#dependency' => array(
247
'edit-options-alter-make-link' => array(1)
251
// Get a list of the available fields and arguments for token replacement.
253
foreach ($this->view->display_handler->get_handlers('field') as $field => $handler) {
254
$options[t('Fields')]["[$field]"] = $handler->ui_name();
255
// We only use fields up to (and including) this one.
256
if ($field == $this->options['id']) {
260
$count = 0; // This lets us prepare the key as we want it printed.
261
foreach ($this->view->display_handler->get_handlers('argument') as $arg => $handler) {
262
$options[t('Arguments')]['%' . ++$count] = $handler->ui_name();
266
$output = t('<p>You must add some additional fields to this display before using this field. These fields may be marked as <em>Exclude from display</em> if you prefer. Note that due to rendering order, you cannot use fields that come after this field; if you need a field not listed here, rearrange your fields.</p>');
267
// We have some options, so make a list.
268
if (!empty($options)) {
269
$output = t('<p>The following substitution patterns are available for this display. Use the pattern shown on the left to display the value indicated on the right. Note that due to rendering order, you cannot use fields that come after this field; if you need a field not listed here, rearrange your fields.</p>');
270
foreach (array_keys($options) as $type) {
271
if (!empty($options[$type])) {
273
$title = t(ucwords($type));
274
foreach ($options[$type] as $key => $value) {
275
$items[] = $key .' == '. $value;
277
$output .= theme('item_list', $items, $title);
281
// This construct uses 'hidden' and not markup because process doesn't
282
// run. It also has an extra div because the dependency wants to hide
283
// the parent in situations like this, so we need a second div to
285
$form['alter']['help'] = array(
287
'#id' => 'views-tokens-help',
288
'#prefix' => '<div><fieldset id="views-tokens-help"><legend>' . t('Replacement patterns') . '</legend>' . $output . '</fieldset></div>',
289
'#process' => array('views_process_dependency'),
290
'#dependency' => array(
291
'edit-options-alter-make-link' => array(1),
292
'edit-options-alter-alter-text' => array(1),
296
$form['alter']['trim'] = array(
297
'#type' => 'checkbox',
298
'#title' => t('Trim this field to a maximum length'),
299
'#description' => t('If checked, this field be trimmed to a maximum length in characters.'),
300
'#default_value' => $this->options['alter']['trim'],
303
$form['alter']['max_length'] = array(
304
'#title' => t('Maximum length'),
305
'#type' => 'textfield',
306
'#default_value' => $this->options['alter']['max_length'],
307
'#description' => t('The maximum number of characters his field can be.'),
308
'#process' => array('views_process_dependency'),
309
'#dependency' => array(
310
'edit-options-alter-trim' => array(1)
314
$form['alter']['word_boundary'] = array(
315
'#type' => 'checkbox',
316
'#title' => t('Trim only on a word boundary'),
317
'#description' => t('If checked, this field be trimmed only on a word boundary. This is guaranteed to be the maximum characters stated or less. If there are no word boundaries this could trim a field to nothing.'),
318
'#default_value' => $this->options['alter']['word_boundary'],
319
'#process' => array('views_process_dependency'),
320
'#dependency' => array(
321
'edit-options-alter-trim' => array(1)
325
$form['alter']['ellipsis'] = array(
326
'#type' => 'checkbox',
327
'#title' => t('Add an ellipsis'),
328
'#description' => t('If checked, a "..." will be added if a field was trimmed.'),
329
'#default_value' => $this->options['alter']['ellipsis'],
330
'#process' => array('views_process_dependency'),
331
'#dependency' => array(
332
'edit-options-alter-trim' => array(1)
336
$form['alter']['strip_tags'] = array(
337
'#type' => 'checkbox',
338
'#title' => t('Strip HTML tags'),
339
'#description' => t('If checked, all HTML tags will be stripped.'),
340
'#default_value' => $this->options['alter']['strip_tags'],
341
'#process' => array('views_process_dependency'),
342
'#dependency' => array(
343
'edit-options-alter-trim' => array(1)
347
$form['alter']['html'] = array(
348
'#type' => 'checkbox',
349
'#title' => t('Field can contain HTML'),
350
'#description' => t('If checked, HTML corrector will be run to ensure tags are properly closed after trimming.'),
351
'#default_value' => $this->options['alter']['html'],
352
'#process' => array('views_process_dependency'),
353
'#dependency' => array(
354
'edit-options-alter-trim' => array(1)
361
* Provide extra data to the administration form
363
function admin_summary() {
364
return $this->label();
368
* Run before any fields are rendered.
370
* This gives the handlers some time to set up before any handler has
374
* An array of all objects returned from the query.
376
function pre_render($values) { }
382
* The values retrieved from the database.
384
function render($values) {
385
$value = $values->{$this->field_alias};
386
return check_plain($value);
390
* Render a field using advanced settings.
392
* This renders a field normally, then decides if render-as-link and
393
* text-replacement rendering is necessary.
395
function advanced_render($values) {
396
$this->last_render = $value = $this->render($values);
397
$this->original_value = $value;
399
if ($this->allow_advanced_render()) {
401
if (!empty($this->options['alter']['alter_text']) && !empty($this->options['alter']['text'])) {
402
$tokens = $this->get_render_tokens();
403
$value = $this->render_altered($tokens);
406
if (!empty($this->options['alter']['trim']) && !empty($this->options['alter']['max_length'])) {
407
$value = $this->render_trim_text($value);
410
if (!empty($this->options['alter']['make_link']) && !empty($this->options['alter']['path'])) {
411
if (!isset($tokens)) {
412
$tokens = $this->get_render_tokens();
414
$value = $this->render_as_link($value, $tokens);
417
// This happens here so that render_as_link can get the unaltered value of
418
// this field as a token rather than the altered value.
419
$this->last_render = $value;
422
return $this->last_render;
426
* Render this field as altered text, from a fieldset set by the user.
428
function render_altered($tokens) {
429
// Filter this right away as our substitutions are already sanitized.
430
$value = filter_xss_admin($this->options['alter']['text']);
431
$value = strtr($value, $tokens);
437
* Trim the field down to the specified length.
439
function render_trim_text($value) {
440
if (!empty($this->options['alter']['strip_tags'])) {
441
$value = strip_tags($value);
442
// NOTE: It's possible that some external fields might override the
443
// element type so if someone from, say, CCK runs into a bug here,
444
// this may be why =)
445
$this->definition['element type'] = 'span';
448
if (drupal_strlen($value) <= $this->options['alter']['max_length']) {
452
$value = drupal_substr($value, 0, $this->options['alter']['max_length']);
454
if (!empty($this->options['alter']['word_boundary'])) {
455
if (preg_match ("/(.*)\b.+/us", $value, $matches)) {
456
// Remove scraps of HTML entities from the end of a strings
457
$value = preg_replace('/(?:<(?!.+>)|&(?!.+;))/us', '', $matches[1]);
458
// Strip spare space at the end.
459
$value = rtrim($value);
463
if (!empty($this->options['alter']['ellipsis'])) {
467
if (!empty($this->options['alter']['html'])) {
468
$value = _filter_htmlcorrector($value);
475
* Render this field as a link, with the info from a fieldset set by
478
function render_as_link($text, $tokens) {
479
// $path will be run through check_url() by l() so we do not need to
480
// sanitize it ourselves.
481
$path = $this->options['alter']['path'];
483
// Use strip tags as there should never be HTML in the path.
484
$path = strip_tags(strtr($path, $tokens));
486
$alt = $this->options['alter']['alt'];
487
$alt = strtr($alt, $tokens);
491
if (!empty($this->options['alter']['prefix'])) {
492
$value .= filter_xss_admin($this->options['alter']['prefix']);
500
$options['attributes']['title'] = $alt;
501
$options['attributes']['alt'] = $alt;
504
// These can only be set internally at this time.
505
if (isset($this->options['alter']['query'])) {
506
$options['query'] = $this->options['alter']['query'];
508
if (isset($this->options['alter']['fragment'])) {
509
$options['fragment'] = $this->options['alter']['fragment'];
512
$value .= l($text, $path, $options);
514
if (!empty($this->options['alter']['suffix'])) {
515
$value .= filter_xss_admin($this->options['alter']['suffix']);
522
* Get the 'render' tokens to use for advanced rendering.
524
* This runs through all of the fields and arguments that
525
* are available and gets their values. This will then be
526
* used in one giant str_replace().
528
function get_render_tokens() {
530
if (!empty($this->view->build_info['substitutions'])) {
531
$tokens = $this->view->build_info['substitutions'];
534
foreach ($this->view->display_handler->get_handlers('argument') as $arg => $handler) {
535
$token = '%' . ++$count;
536
if (!isset($tokens[$token])) {
537
$tokens[$token] = '';
541
// Now add replacements for our fields.
543
foreach ($this->view->display_handler->get_handlers('field') as $field => $handler) {
544
if (isset($handler->last_render)) {
545
$tokens["[$field]"] = $handler->last_render;
548
$tokens["[$field]"] = '';
550
// We only use fields up to (and including) this one.
551
if ($field == $this->options['id']) {
560
* Call out to the theme() function, which probably just calls render() but
561
* allows sites to override output fairly easily.
563
function theme($values) {
564
return theme($this->theme_functions(), $this->view, $this, $values);
567
function theme_functions() {
569
$hook = 'views_view_field';
571
$display = $this->view->display[$this->view->current_display];
573
if (!empty($display)) {
574
$themes[] = $hook . '__' . $this->view->name . '__' . $display->id . '__' . $this->options['id'];
575
$themes[] = $hook . '__' . $this->view->name . '__' . $display->id;
576
$themes[] = $hook . '__' . $display->id . '__' . $this->options['id'];
577
$themes[] = $hook . '__' . $display->id;
578
if ($display->id != $display->display_plugin) {
579
$themes[] = $hook . '__' . $this->view->name . '__' . $display->display_plugin . '__' . $this->options['id'];
580
$themes[] = $hook . '__' . $this->view->name . '__' . $display->display_plugin;
581
$themes[] = $hook . '__' . $display->display_plugin . '__' . $this->options['id'];
582
$themes[] = $hook . '__' . $display->display_plugin;
585
$themes[] = $hook . '__' . $this->view->name . '__' . $this->options['id'];
586
$themes[] = $hook . '__' . $this->view->name;
587
$themes[] = $hook . '__' . $this->options['id'];
595
* A special handler to take the place of missing or broken handlers.
597
class views_handler_field_broken extends views_handler_field {
599
return t('Broken/missing handler');
602
function ensure_my_table() { /* No table to ensure! */ }
603
function query() { /* No query to run */ }
604
function options_form(&$form, &$form_state) {
605
$form['markup'] = array(
606
'#prefix' => '<div class="form-item description">',
607
'#value' => t('The handler for this item is broken or missing and cannot be used. If a module provided the handler and was disabled, re-enabling the module may restore it. Otherwise, you should probably delete this item.'),
612
* Determine if the handler is considered 'broken'
614
function broken() { return TRUE; }
618
* Render a numeric value as a size.
620
class views_handler_field_file_size extends views_handler_field {
621
function render($values) {
622
if ($values->{$this->field_alias}) {
623
return format_size($values->{$this->field_alias});
632
* A handler to run a field through simple XSS filtering
634
class views_handler_field_xss extends views_handler_field {
635
function render($values) {
636
$value = $values->{$this->field_alias};
637
return filter_xss($value);