9 * @defgroup views_field_handlers Views field handlers
11 * Handlers to tell Views how to build and display fields.
16 * Indicator of the render_text() method for rendering a single item.
17 * (If no render_item() is present).
19 define('VIEWS_HANDLER_RENDER_TEXT_PHASE_SINGLE_ITEM', 0);
22 * Indicator of the render_text() method for rendering the whole element.
23 * (if no render_item() method is available).
25 define('VIEWS_HANDLER_RENDER_TEXT_PHASE_COMPLETELY', 1);
28 * Indicator of the render_text() method for rendering the empty text.
30 define('VIEWS_HANDLER_RENDER_TEXT_PHASE_EMPTY', 2);
33 * Base field handler that has no options and renders an unformatted field.
36 * - additional fields: An array of fields that should be added to the query
37 * for some purpose. The array is in the form of:
38 * array('identifier' => array('table' => tablename,
39 * 'field' => fieldname); as many fields as are necessary
40 * may be in this array.
41 * - click sortable: If TRUE, this field may be click sorted.
43 * @ingroup views_field_handlers
45 class views_handler_field extends views_handler {
46 var $field_alias = 'unknown';
47 var $aliases = array();
50 * The field value prior to any rewriting.
54 public $original_value = NULL;
58 * Stores additional fields which get's added to the query.
59 * The generated aliases are stored in $aliases.
61 var $additional_fields = array();
64 * Construct a new field handler.
66 function construct() {
69 $this->additional_fields = array();
70 if (!empty($this->definition['additional fields'])) {
71 $this->additional_fields = $this->definition['additional fields'];
74 if (!isset($this->options['exclude'])) {
75 $this->options['exclude'] = '';
80 * Determine if this field can allow advanced rendering.
82 * Fields can set this to FALSE if they do not wish to allow
83 * token based rewriting or link-making.
85 function allow_advanced_render() {
89 function init(&$view, &$options) {
90 parent::init($view, $options);
94 * Called to add the field to a query.
97 $this->ensure_my_table();
99 $params = $this->options['group_type'] != 'group' ? array('function' => $this->options['group_type']) : array();
100 $this->field_alias = $this->query->add_field($this->table_alias, $this->real_field, NULL, $params);
102 $this->add_additional_fields();
106 * Add 'additional' fields to the query.
109 * An array of fields. The key is an identifier used to later find the
110 * field alias used. The value is either a string in which case it's
111 * assumed to be a field on this handler's table; or it's an array in the
113 * @code array('table' => $tablename, 'field' => $fieldname) @endcode
115 function add_additional_fields($fields = NULL) {
116 if (!isset($fields)) {
118 if (empty($this->additional_fields)) {
121 $fields = $this->additional_fields;
124 $group_params = array();
125 if ($this->options['group_type'] != 'group') {
126 $group_params = array(
127 'function' => $this->options['group_type'],
131 if (!empty($fields) && is_array($fields)) {
132 foreach ($fields as $identifier => $info) {
133 if (is_array($info)) {
134 if (isset($info['table'])) {
135 $table_alias = $this->query->ensure_table($info['table'], $this->relationship);
138 $table_alias = $this->table_alias;
141 if (empty($table_alias)) {
142 debug(t('Handler @handler tried to add additional_field @identifier but @table could not be added!', array('@handler' => $this->definition['handler'], '@identifier' => $identifier, '@table' => $info['table'])));
143 $this->aliases[$identifier] = 'broken';
148 if (!empty($info['params'])) {
149 $params = $info['params'];
152 $params += $group_params;
153 $this->aliases[$identifier] = $this->query->add_field($table_alias, $info['field'], NULL, $params);
156 $this->aliases[$info] = $this->query->add_field($this->table_alias, $info, NULL, $group_params);
163 * Called to determine what to tell the clicksorter.
165 function click_sort($order) {
166 if (isset($this->field_alias)) {
167 // Since fields should always have themselves already added, just
168 // add a sort on the field.
169 $params = $this->options['group_type'] != 'group' ? array('function' => $this->options['group_type']) : array();
170 $this->query->add_orderby(NULL, NULL, $order, $this->field_alias, $params);
175 * Determine if this field is click sortable.
177 function click_sortable() {
178 return !empty($this->definition['click sortable']);
182 * Get this field's label.
185 if (!isset($this->options['label'])) {
188 return $this->options['label'];
192 * Return an HTML element based upon the field's element type.
194 function element_type($none_supported = FALSE, $default_empty = FALSE, $inline = FALSE) {
195 if ($none_supported) {
196 if ($this->options['element_type'] === '0') {
200 if ($this->options['element_type']) {
201 return check_plain($this->options['element_type']);
204 if ($default_empty) {
212 if (isset($this->definition['element type'])) {
213 return $this->definition['element type'];
220 * Return an HTML element for the label based upon the field's element type.
222 function element_label_type($none_supported = FALSE, $default_empty = FALSE) {
223 if ($none_supported) {
224 if ($this->options['element_label_type'] === '0') {
228 if ($this->options['element_label_type']) {
229 return check_plain($this->options['element_label_type']);
232 if ($default_empty) {
240 * Return an HTML element for the wrapper based upon the field's element type.
242 function element_wrapper_type($none_supported = FALSE, $default_empty = FALSE) {
243 if ($none_supported) {
244 if ($this->options['element_wrapper_type'] === '0') {
248 if ($this->options['element_wrapper_type']) {
249 return check_plain($this->options['element_wrapper_type']);
252 if ($default_empty) {
260 * Provide a list of elements valid for field HTML.
262 * This function can be overridden by fields that want more or fewer
263 * elements available, though this seems like it would be an incredibly
266 function get_elements() {
267 static $elements = NULL;
268 if (!isset($elements)) {
269 $elements = variable_get('views_field_rewrite_elements', array(
270 '' => t('- Use default -'),
271 '0' => t('- None -'),
281 'strong' => 'STRONG',
290 * Return the class of the field.
292 function element_classes($row_index = NULL) {
293 $classes = explode(' ', $this->options['element_class']);
294 foreach ($classes as &$class) {
295 $class = $this->tokenize_value($class, $row_index);
296 $class = views_clean_css_identifier($class);
298 return implode(' ', $classes);
302 * Replace a value with tokens from the last field.
304 * This function actually figures out which field was last and uses its
305 * tokens so they will all be available.
307 function tokenize_value($value, $row_index = NULL) {
308 if (strpos($value, '[') !== FALSE || strpos($value, '!') !== FALSE || strpos($value, '%') !== FALSE) {
310 'alter_text' => TRUE,
314 // Use isset() because empty() will trigger on 0 and 0 is
316 if (isset($row_index) && isset($this->view->style_plugin->render_tokens[$row_index])) {
317 $tokens = $this->view->style_plugin->render_tokens[$row_index];
320 // Get tokens from the last field.
321 $last_field = end($this->view->field);
322 if (isset($last_field->last_tokens)) {
323 $tokens = $last_field->last_tokens;
326 $tokens = $last_field->get_render_tokens($fake_item);
330 $value = strip_tags($this->render_altered($fake_item, $tokens));
331 if (!empty($this->options['alter']['trim_whitespace'])) {
332 $value = trim($value);
340 * Return the class of the field's label.
342 function element_label_classes($row_index = NULL) {
343 $classes = explode(' ', $this->options['element_label_class']);
344 foreach ($classes as &$class) {
345 $class = $this->tokenize_value($class, $row_index);
346 $class = views_clean_css_identifier($class);
348 return implode(' ', $classes);
352 * Return the class of the field's wrapper.
354 function element_wrapper_classes($row_index = NULL) {
355 $classes = explode(' ', $this->options['element_wrapper_class']);
356 foreach ($classes as &$class) {
357 $class = $this->tokenize_value($class, $row_index);
358 $class = views_clean_css_identifier($class);
360 return implode(' ', $classes);
364 * Get the value that's supposed to be rendered.
366 * This api exists so that other modules can easy set the values of the field
367 * without having the need to change the render method as well.
370 * An object containing all retrieved values.
372 * Optional name of the field where the value is stored.
374 function get_value($values, $field = NULL) {
375 $alias = isset($field) ? $this->aliases[$field] : $this->field_alias;
376 if (isset($values->{$alias})) {
377 return $values->{$alias};
382 * Determines if this field will be available as an option to group the result
383 * by in the style settings.
386 * TRUE if this field handler is groupable, otherwise FALSE.
388 function use_string_group_by() {
392 function option_definition() {
393 $options = parent::option_definition();
395 $options['label'] = array('default' => $this->definition['title'], 'translatable' => TRUE);
396 $options['exclude'] = array('default' => FALSE, 'bool' => TRUE);
397 $options['alter'] = array(
399 'alter_text' => array('default' => FALSE, 'bool' => TRUE),
400 'text' => array('default' => '', 'translatable' => TRUE),
401 'make_link' => array('default' => FALSE, 'bool' => TRUE),
402 'path' => array('default' => ''),
403 'absolute' => array('default' => FALSE, 'bool' => TRUE),
404 'external' => array('default' => FALSE, 'bool' => TRUE),
405 'replace_spaces' => array('default' => FALSE, 'bool' => TRUE),
406 'path_case' => array('default' => 'none', 'translatable' => FALSE),
407 'trim_whitespace' => array('default' => FALSE, 'bool' => TRUE),
408 'alt' => array('default' => '', 'translatable' => TRUE),
409 'rel' => array('default' => ''),
410 'link_class' => array('default' => ''),
411 'prefix' => array('default' => '', 'translatable' => TRUE),
412 'suffix' => array('default' => '', 'translatable' => TRUE),
413 'target' => array('default' => ''),
414 'nl2br' => array('default' => FALSE, 'bool' => TRUE),
415 'max_length' => array('default' => ''),
416 'word_boundary' => array('default' => TRUE, 'bool' => TRUE),
417 'ellipsis' => array('default' => TRUE, 'bool' => TRUE),
418 'more_link' => array('default' => FALSE, 'bool' => TRUE),
419 'more_link_text' => array('default' => '', 'translatable' => TRUE),
420 'more_link_path' => array('default' => ''),
421 'strip_tags' => array('default' => FALSE, 'bool' => TRUE),
422 'trim' => array('default' => FALSE, 'bool' => TRUE),
423 'preserve_tags' => array('default' => ''),
424 'html' => array('default' => FALSE, 'bool' => TRUE),
427 $options['element_type'] = array('default' => '');
428 $options['element_class'] = array('default' => '');
430 $options['element_label_type'] = array('default' => '');
431 $options['element_label_class'] = array('default' => '');
432 $options['element_label_colon'] = array('default' => TRUE, 'bool' => TRUE);
434 $options['element_wrapper_type'] = array('default' => '');
435 $options['element_wrapper_class'] = array('default' => '');
437 $options['element_default_classes'] = array('default' => TRUE, 'bool' => TRUE);
439 $options['empty'] = array('default' => '', 'translatable' => TRUE);
440 $options['hide_empty'] = array('default' => FALSE, 'bool' => TRUE);
441 $options['empty_zero'] = array('default' => FALSE, 'bool' => TRUE);
442 $options['hide_alter_empty'] = array('default' => TRUE, 'bool' => TRUE);
448 * Performs some cleanup tasks on the options array before saving it.
450 function options_submit(&$form, &$form_state) {
451 $options = &$form_state['values']['options'];
452 $types = array('element_type', 'element_label_type', 'element_wrapper_type');
453 $classes = array_combine(array('element_class', 'element_label_class', 'element_wrapper_class'), $types);
455 foreach ($types as $type) {
456 if (!$options[$type . '_enable']) {
457 $options[$type] = '';
461 foreach ($classes as $class => $type) {
462 if (!$options[$class . '_enable'] || !$options[$type . '_enable']) {
463 $options[$class] = '';
467 if (empty($options['custom_label'])) {
468 $options['label'] = '';
469 $options['element_label_colon'] = FALSE;
474 * Default options form that provides the label widget that all fields
477 function options_form(&$form, &$form_state) {
478 parent::options_form($form, $form_state);
480 $label = $this->label();
481 $form['custom_label'] = array(
482 '#type' => 'checkbox',
483 '#title' => t('Create a label'),
484 '#description' => t('Enable to create a label for this field.'),
485 '#default_value' => $label !== '',
488 $form['label'] = array(
489 '#type' => 'textfield',
490 '#title' => t('Label'),
491 '#default_value' => $label,
492 '#dependency' => array(
493 'edit-options-custom-label' => array(1),
497 $form['element_label_colon'] = array(
498 '#type' => 'checkbox',
499 '#title' => t('Place a colon after the label'),
500 '#default_value' => $this->options['element_label_colon'],
501 '#dependency' => array(
502 'edit-options-custom-label' => array(1),
507 $form['exclude'] = array(
508 '#type' => 'checkbox',
509 '#title' => t('Exclude from display'),
510 '#default_value' => $this->options['exclude'],
511 '#description' => t('Enable to load this field as hidden. Often used to group fields, or to use as token in another field.'),
515 $form['style_settings'] = array(
516 '#type' => 'fieldset',
517 '#title' => t('Style settings'),
518 '#collapsible' => TRUE,
519 '#collapsed' => TRUE,
523 $form['element_type_enable'] = array(
524 '#type' => 'checkbox',
525 '#title' => t('Customize field HTML'),
526 '#default_value' => !empty($this->options['element_type']) || (string) $this->options['element_type'] == '0' || !empty($this->options['element_class']) || (string) $this->options['element_class'] == '0',
527 '#fieldset' => 'style_settings',
529 $form['element_type'] = array(
530 '#title' => t('HTML element'),
531 '#options' => $this->get_elements(),
533 '#default_value' => $this->options['element_type'],
534 '#description' => t('Choose the HTML element to wrap around this field, e.g. H1, H2, etc.'),
535 '#dependency' => array(
536 'edit-options-element-type-enable' => array(1),
538 '#fieldset' => 'style_settings',
541 $form['element_class_enable'] = array(
542 '#type' => 'checkbox',
543 '#title' => t('Create a CSS class'),
544 '#dependency' => array(
545 'edit-options-element-type-enable' => array(1),
547 '#default_value' => !empty($this->options['element_class']) || (string) $this->options['element_class'] == '0',
548 '#fieldset' => 'style_settings',
550 $form['element_class'] = array(
551 '#title' => t('CSS class'),
552 '#description' => t('You may use token substitutions from the rewriting section in this class.'),
553 '#type' => 'textfield',
554 '#default_value' => $this->options['element_class'],
555 '#dependency' => array(
556 'edit-options-element-class-enable' => array(1),
557 'edit-options-element-type-enable' => array(1),
559 '#dependency_count' => 2,
560 '#fieldset' => 'style_settings',
563 $form['element_label_type_enable'] = array(
564 '#type' => 'checkbox',
565 '#title' => t('Customize label HTML'),
566 '#default_value' => !empty($this->options['element_label_type']) || (string) $this->options['element_label_type'] == '0' || !empty($this->options['element_label_class']) || (string) $this->options['element_label_class'] == '0',
567 '#fieldset' => 'style_settings',
569 $form['element_label_type'] = array(
570 '#title' => t('Label HTML element'),
571 '#options' => $this->get_elements(FALSE),
573 '#default_value' => $this->options['element_label_type'],
574 '#description' => t('Choose the HTML element to wrap around this label, e.g. H1, H2, etc.'),
575 '#dependency' => array(
576 'edit-options-element-label-type-enable' => array(1),
578 '#fieldset' => 'style_settings',
580 $form['element_label_class_enable'] = array(
581 '#type' => 'checkbox',
582 '#title' => t('Create a CSS class'),
583 '#dependency' => array(
584 'edit-options-element-label-type-enable' => array(1)
586 '#default_value' => !empty($this->options['element_label_class']) || (string) $this->options['element_label_class'] == '0',
587 '#fieldset' => 'style_settings',
589 $form['element_label_class'] = array(
590 '#title' => t('CSS class'),
591 '#description' => t('You may use token substitutions from the rewriting section in this class.'),
592 '#type' => 'textfield',
593 '#default_value' => $this->options['element_label_class'],
594 '#dependency' => array(
595 'edit-options-element-label-class-enable' => array(1),
596 'edit-options-element-label-type-enable' => array(1),
598 '#dependency_count' => 2,
599 '#fieldset' => 'style_settings',
602 $form['element_wrapper_type_enable'] = array(
603 '#type' => 'checkbox',
604 '#title' => t('Customize field and label wrapper HTML'),
605 '#default_value' => !empty($this->options['element_wrapper_type']) || (string) $this->options['element_wrapper_type'] == '0' || !empty($this->options['element_wrapper_class']) || (string) $this->options['element_wrapper_class'] == '0',
606 '#fieldset' => 'style_settings',
608 $form['element_wrapper_type'] = array(
609 '#title' => t('Wrapper HTML element'),
610 '#options' => $this->get_elements(FALSE),
612 '#default_value' => $this->options['element_wrapper_type'],
613 '#description' => t('Choose the HTML element to wrap around this field and label, e.g. H1, H2, etc. This may not be used if the field and label are not rendered together, such as with a table.'),
614 '#dependency' => array(
615 'edit-options-element-wrapper-type-enable' => array(1),
617 '#fieldset' => 'style_settings',
620 $form['element_wrapper_class_enable'] = array(
621 '#type' => 'checkbox',
622 '#title' => t('Create a CSS class'),
623 '#dependency' => array(
624 'edit-options-element-wrapper-type-enable' => array(1),
626 '#default_value' => !empty($this->options['element_wrapper_class']) || (string) $this->options['element_wrapper_class'] == '0',
627 '#fieldset' => 'style_settings',
629 $form['element_wrapper_class'] = array(
630 '#title' => t('CSS class'),
631 '#description' => t('You may use token substitutions from the rewriting section in this class.'),
632 '#type' => 'textfield',
633 '#default_value' => $this->options['element_wrapper_class'],
634 '#dependency' => array(
635 'edit-options-element-wrapper-class-enable' => array(1),
636 'edit-options-element-wrapper-type-enable' => array(1),
638 '#dependency_count' => 2,
639 '#fieldset' => 'style_settings',
642 $form['element_default_classes'] = array(
643 '#type' => 'checkbox',
644 '#title' => t('Add default classes'),
645 '#default_value' => $this->options['element_default_classes'],
646 '#description' => t('Use default Views classes to identify the field, field label and field content.'),
647 '#fieldset' => 'style_settings',
650 $form['alter'] = array(
651 '#title' => t('Rewrite results'),
652 '#type' => 'fieldset',
653 '#collapsible' => TRUE,
654 '#collapsed' => TRUE,
658 if ($this->allow_advanced_render()) {
659 $form['alter']['#tree'] = TRUE;
660 $form['alter']['alter_text'] = array(
661 '#type' => 'checkbox',
662 '#title' => t('Rewrite the output of this field'),
663 '#description' => t('Enable to override the output of this field with custom text or replacement tokens.'),
664 '#default_value' => $this->options['alter']['alter_text'],
667 $form['alter']['text'] = array(
668 '#title' => t('Text'),
669 '#type' => 'textarea',
670 '#default_value' => $this->options['alter']['text'],
671 '#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.'),
672 '#dependency' => array(
673 'edit-options-alter-alter-text' => array(1),
677 $form['alter']['make_link'] = array(
678 '#type' => 'checkbox',
679 '#title' => t('Output this field as a link'),
680 '#description' => t('If checked, this field will be made into a link. The destination must be given below.'),
681 '#default_value' => $this->options['alter']['make_link'],
683 $form['alter']['path'] = array(
684 '#title' => t('Link path'),
685 '#type' => 'textfield',
686 '#default_value' => $this->options['alter']['path'],
687 '#description' => t('The Drupal path or absolute URL for this link. You may enter data from this view as per the "Replacement patterns" below.'),
688 '#dependency' => array(
689 'edit-options-alter-make-link' => array(1),
693 $form['alter']['absolute'] = array(
694 '#type' => 'checkbox',
695 '#title' => t('Use absolute path'),
696 '#default_value' => $this->options['alter']['absolute'],
697 '#dependency' => array(
698 'edit-options-alter-make-link' => array(1),
701 $form['alter']['replace_spaces'] = array(
702 '#type' => 'checkbox',
703 '#title' => t('Replace spaces with dashes'),
704 '#default_value' => $this->options['alter']['replace_spaces'],
705 '#dependency' => array(
706 'edit-options-alter-make-link' => array(1)
709 $form['alter']['external'] = array(
710 '#type' => 'checkbox',
711 '#title' => t('External server URL'),
712 '#default_value' => $this->options['alter']['external'],
713 '#description' => t("Links to an external server using a full URL: e.g. 'http://www.example.com' or 'www.example.com'."),
714 '#dependency' => array(
715 'edit-options-alter-make-link' => array(1),
718 $form['alter']['path_case'] = array(
720 '#title' => t('Transform the case'),
721 '#description' => t('When printing url paths, how to transform the case of the filter value.'),
722 '#dependency' => array(
723 'edit-options-alter-make-link' => array(1),
726 'none' => t('No transform'),
727 'upper' => t('Upper case'),
728 'lower' => t('Lower case'),
729 'ucfirst' => t('Capitalize first letter'),
730 'ucwords' => t('Capitalize each word'),
732 '#default_value' => $this->options['alter']['path_case'],
734 $form['alter']['link_class'] = array(
735 '#title' => t('Link class'),
736 '#type' => 'textfield',
737 '#default_value' => $this->options['alter']['link_class'],
738 '#description' => t('The CSS class to apply to the link.'),
739 '#dependency' => array(
740 'edit-options-alter-make-link' => array(1),
743 $form['alter']['alt'] = array(
744 '#title' => t('Title text'),
745 '#type' => 'textfield',
746 '#default_value' => $this->options['alter']['alt'],
747 '#description' => t('Text to place as "title" text which most browsers display as a tooltip when hovering over the link.'),
748 '#dependency' => array(
749 'edit-options-alter-make-link' => array(1),
752 $form['alter']['rel'] = array(
753 '#title' => t('Rel Text'),
754 '#type' => 'textfield',
755 '#default_value' => $this->options['alter']['rel'],
756 '#description' => t('Include Rel attribute for use in lightbox2 or other javascript utility.'),
757 '#dependency' => array(
758 'edit-options-alter-make-link' => array(1),
761 $form['alter']['prefix'] = array(
762 '#title' => t('Prefix text'),
763 '#type' => 'textfield',
764 '#default_value' => $this->options['alter']['prefix'],
765 '#description' => t('Any text to display before this link. You may include HTML.'),
766 '#dependency' => array(
767 'edit-options-alter-make-link' => array(1),
770 $form['alter']['suffix'] = array(
771 '#title' => t('Suffix text'),
772 '#type' => 'textfield',
773 '#default_value' => $this->options['alter']['suffix'],
774 '#description' => t('Any text to display after this link. You may include HTML.'),
775 '#dependency' => array(
776 'edit-options-alter-make-link' => array(1),
779 $form['alter']['target'] = array(
780 '#title' => t('Target'),
781 '#type' => 'textfield',
782 '#default_value' => $this->options['alter']['target'],
783 '#description' => t("Target of the link, such as _blank, _parent or an iframe's name. This field is rarely used."),
784 '#dependency' => array(
785 'edit-options-alter-make-link' => array(1),
790 // Get a list of the available fields and arguments for token replacement.
792 foreach ($this->view->display_handler->get_handlers('field') as $field => $handler) {
793 $options[t('Fields')]["[$field]"] = $handler->ui_name();
794 // We only use fields up to (and including) this one.
795 if ($field == $this->options['id']) {
799 $count = 0; // This lets us prepare the key as we want it printed.
800 foreach ($this->view->display_handler->get_handlers('argument') as $arg => $handler) {
801 $options[t('Arguments')]['%' . ++$count] = t('@argument title', array('@argument' => $handler->ui_name()));
802 $options[t('Arguments')]['!' . $count] = t('@argument input', array('@argument' => $handler->ui_name()));
805 $this->document_self_tokens($options[t('Fields')]);
808 $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>');
809 // We have some options, so make a list.
810 if (!empty($options)) {
811 $output = t('<p>The following tokens are available for this field. 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.
812 If you would like to have the characters \'[\' and \']\' please use the html entity codes \'%5B\' or \'%5D\' or they will get replaced with empty space.</p>');
813 foreach (array_keys($options) as $type) {
814 if (!empty($options[$type])) {
816 foreach ($options[$type] as $key => $value) {
817 $items[] = $key . ' == ' . check_plain($value);
819 $output .= theme('item_list',
827 // This construct uses 'hidden' and not markup because process doesn't
828 // run. It also has an extra div because the dependency wants to hide
829 // the parent in situations like this, so we need a second div to
831 $form['alter']['help'] = array(
832 '#type' => 'fieldset',
833 '#title' => t('Replacement patterns'),
834 '#collapsible' => TRUE,
835 '#collapsed' => TRUE,
837 '#dependency' => array(
838 'edit-options-alter-make-link' => array(1),
839 'edit-options-alter-alter-text' => array(1),
840 'edit-options-alter-more-link' => array(1),
844 $form['alter']['trim'] = array(
845 '#type' => 'checkbox',
846 '#title' => t('Trim this field to a maximum length'),
847 '#description' => t('Enable to trim the field to a maximum length of characters'),
848 '#default_value' => $this->options['alter']['trim'],
851 $form['alter']['max_length'] = array(
852 '#title' => t('Maximum length'),
853 '#type' => 'textfield',
854 '#default_value' => $this->options['alter']['max_length'],
855 '#description' => t('The maximum number of characters this field can be.'),
856 '#dependency' => array(
857 'edit-options-alter-trim' => array(1),
861 $form['alter']['word_boundary'] = array(
862 '#type' => 'checkbox',
863 '#title' => t('Trim only on a word boundary'),
864 '#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.'),
865 '#default_value' => $this->options['alter']['word_boundary'],
866 '#dependency' => array(
867 'edit-options-alter-trim' => array(1),
871 $form['alter']['ellipsis'] = array(
872 '#type' => 'checkbox',
873 '#title' => t('Add an ellipsis'),
874 '#description' => t('If checked, a "..." will be added if a field was trimmed.'),
875 '#default_value' => $this->options['alter']['ellipsis'],
876 '#dependency' => array(
877 'edit-options-alter-trim' => array(1),
881 $form['alter']['more_link'] = array(
882 '#type' => 'checkbox',
883 '#title' => t('Add a read-more link if output is trimmed.'),
884 '#description' => t('If checked, a read-more link will be added at the end of the trimmed output'),
885 '#default_value' => $this->options['alter']['more_link'],
886 '#dependency' => array(
887 'edit-options-alter-trim' => array(1),
891 $form['alter']['more_link_text'] = array(
892 '#type' => 'textfield',
893 '#title' => t('More link text'),
894 '#default_value' => $this->options['alter']['more_link_text'],
895 '#description' => t('The text which will be displayed on the more link. You may enter data from this view as per the "Replacement patterns" above.'),
896 '#dependency_count' => 2,
897 '#dependency' => array(
898 'edit-options-alter-trim' => array(1),
899 'edit-options-alter-more-link' => array(1),
902 $form['alter']['more_link_path'] = array(
903 '#type' => 'textfield',
904 '#title' => t('More link path'),
905 '#default_value' => $this->options['alter']['more_link_path'],
906 '#description' => t('The path which is used for the more link. You may enter data from this view as per the "Replacement patterns" above.'),
907 '#dependency_count' => 2,
908 '#dependency' => array(
909 'edit-options-alter-trim' => array(1),
910 'edit-options-alter-more-link' => array(1),
914 $form['alter']['html'] = array(
915 '#type' => 'checkbox',
916 '#title' => t('Field can contain HTML'),
917 '#description' => t('If checked, HTML corrector will be run to ensure tags are properly closed after trimming.'),
918 '#default_value' => $this->options['alter']['html'],
919 '#dependency' => array(
920 'edit-options-alter-trim' => array(1),
924 $form['alter']['strip_tags'] = array(
925 '#type' => 'checkbox',
926 '#title' => t('Strip HTML tags'),
927 '#description' => t('If checked, all HTML tags will be stripped.'),
928 '#default_value' => $this->options['alter']['strip_tags'],
931 $form['alter']['preserve_tags'] = array(
932 '#type' => 'textfield',
933 '#title' => t('Preserve certain tags'),
934 '#description' => t('List the tags that need to be preserved during the stripping process. example "<p> <br>" which will preserve all p and br elements'),
935 '#default_value' => $this->options['alter']['preserve_tags'],
936 '#dependency' => array(
937 'edit-options-alter-strip-tags' => array(1),
941 $form['alter']['trim_whitespace'] = array(
942 '#type' => 'checkbox',
943 '#title' => t('Remove whitespace'),
944 '#description' => t('If checked, all whitespaces at the beginning and the end of the output will be removed.'),
945 '#default_value' => $this->options['alter']['trim_whitespace'],
948 $form['alter']['nl2br'] = array(
949 '#type' => 'checkbox',
950 '#title' => t('Convert newlines to HTML <br> tags'),
951 '#description' => t('If checked, all newlines chars (e.g. \n) are converted into HTML <br> tags.'),
952 '#default_value' => $this->options['alter']['nl2br'],
956 $form['empty_field_behavior'] = array(
957 '#type' => 'fieldset',
958 '#title' => t('No results behavior'),
959 '#collapsible' => TRUE,
960 '#collapsed' => TRUE,
964 $form['empty'] = array(
965 '#type' => 'textarea',
966 '#title' => t('No results text'),
967 '#default_value' => $this->options['empty'],
968 '#description' => t('Provide text to display if this field contains an empty result. You may include HTML. You may enter data from this view as per the "Replacement patterns" in the "Rewrite Results" section below.'),
969 '#fieldset' => 'empty_field_behavior',
972 $form['empty_zero'] = array(
973 '#type' => 'checkbox',
974 '#title' => t('Count the number 0 as empty'),
975 '#default_value' => $this->options['empty_zero'],
976 '#description' => t('Enable to display the "no results text" if the field contains the number 0.'),
977 '#fieldset' => 'empty_field_behavior',
980 $form['hide_empty'] = array(
981 '#type' => 'checkbox',
982 '#title' => t('Hide if empty'),
983 '#default_value' => $this->options['hide_empty'],
984 '#description' => t('Enable to hide this field if it is empty. Note that the field label or rewritten output may still be displayed. To hide labels, check the style or row style settings for empty fields. To hide rewritten content, check the "Hide rewriting if empty" checkbox.'),
985 '#fieldset' => 'empty_field_behavior',
988 $form['hide_alter_empty'] = array(
989 '#type' => 'checkbox',
990 '#title' => t('Hide rewriting if empty'),
991 '#default_value' => $this->options['hide_alter_empty'],
992 '#description' => t('Do not display rewritten content if this field is empty.'),
993 '#fieldset' => 'empty_field_behavior',
998 * Provide extra data to the administration form
1000 function admin_summary() {
1001 return $this->label();
1005 * Run before any fields are rendered.
1007 * This gives the handlers some time to set up before any handler has
1011 * An array of all objects returned from the query.
1013 function pre_render(&$values) { }
1019 * The values retrieved from the database.
1021 function render($values) {
1022 $value = $this->get_value($values);
1023 return $this->sanitize_value($value);
1027 * Render a field using advanced settings.
1029 * This renders a field normally, then decides if render-as-link and
1030 * text-replacement rendering is necessary.
1032 function advanced_render($values) {
1033 if ($this->allow_advanced_render() && method_exists($this, 'render_item')) {
1034 $raw_items = $this->get_items($values);
1035 // If there are no items, set the original value to NULL.
1036 if (empty($raw_items)) {
1037 $this->original_value = NULL;
1041 $value = $this->render($values);
1042 if (is_array($value)) {
1043 $value = drupal_render($value);
1045 $this->last_render = $value;
1046 $this->original_value = $value;
1049 if ($this->allow_advanced_render()) {
1051 if (method_exists($this, 'render_item')) {
1053 foreach ($raw_items as $count => $item) {
1054 $value = $this->render_item($count, $item);
1055 if (is_array($value)) {
1056 $value = drupal_render($value);
1058 $this->last_render = $value;
1059 $this->original_value = $this->last_render;
1061 $alter = $item + $this->options['alter'];
1062 $alter['phase'] = VIEWS_HANDLER_RENDER_TEXT_PHASE_SINGLE_ITEM;
1063 $items[] = $this->render_text($alter);
1066 $value = $this->render_items($items);
1069 $alter = array('phase' => VIEWS_HANDLER_RENDER_TEXT_PHASE_COMPLETELY) + $this->options['alter'];
1070 $value = $this->render_text($alter);
1073 if (is_array($value)) {
1074 $value = drupal_render($value);
1076 // This happens here so that render_as_link can get the unaltered value of
1077 // this field as a token rather than the altered value.
1078 $this->last_render = $value;
1081 if (empty($this->last_render)) {
1082 if ($this->is_value_empty($this->last_render, $this->options['empty_zero'], FALSE)) {
1083 $alter = $this->options['alter'];
1084 $alter['alter_text'] = 1;
1085 $alter['text'] = $this->options['empty'];
1086 $alter['phase'] = VIEWS_HANDLER_RENDER_TEXT_PHASE_EMPTY;
1087 $this->last_render = $this->render_text($alter);
1091 return $this->last_render;
1095 * Checks if a field value is empty.
1099 * @param bool $empty_zero
1100 * Whether or not this field is configured to consider 0 as empty.
1101 * @param bool $no_skip_empty
1102 * Whether or not to use empty() to check the value.
1105 * TRUE if the value is considered empty, FALSE otherwise.
1107 function is_value_empty($value, $empty_zero, $no_skip_empty = TRUE) {
1108 if (!isset($value)) {
1112 $empty = ($empty_zero || ($value !== 0 && $value !== '0'));
1115 if ($no_skip_empty) {
1116 $empty = empty($value) && $empty;
1122 * Perform an advanced text render for the item.
1124 * This is separated out as some fields may render lists, and this allows
1125 * each item to be handled individually.
1127 function render_text($alter) {
1128 $value = $this->last_render;
1130 if (!empty($alter['alter_text']) && $alter['text'] !== '') {
1131 $tokens = $this->get_render_tokens($alter);
1132 $value = $this->render_altered($alter, $tokens);
1135 if (!empty($this->options['alter']['trim_whitespace'])) {
1136 $value = trim($value);
1139 // Check if there should be no further rewrite for empty values.
1140 $no_rewrite_for_empty = $this->options['hide_alter_empty'] && $this->is_value_empty($this->original_value, $this->options['empty_zero']);
1142 // Check whether the value is empty and return nothing, so the field isn't rendered.
1143 // First check whether the field should be hidden if the value(hide_alter_empty = TRUE) /the rewrite is empty (hide_alter_empty = FALSE).
1144 // For numeric values you can specify whether "0"/0 should be empty.
1145 if ((($this->options['hide_empty'] && empty($value))
1146 || ($alter['phase'] != VIEWS_HANDLER_RENDER_TEXT_PHASE_EMPTY && $no_rewrite_for_empty))
1147 && $this->is_value_empty($value, $this->options['empty_zero'], FALSE)) {
1150 // Only in empty phase.
1151 if ($alter['phase'] == VIEWS_HANDLER_RENDER_TEXT_PHASE_EMPTY && $no_rewrite_for_empty) {
1152 // If we got here then $alter contains the value of "No results text"
1153 // and so there is nothing left to do.
1157 if (!empty($alter['strip_tags'])) {
1158 $value = strip_tags($value, $alter['preserve_tags']);
1162 if (!empty($alter['trim']) && !empty($alter['max_length'])) {
1163 $length = strlen($value);
1164 $value = $this->render_trim_text($alter, $value);
1165 if ($this->options['alter']['more_link'] && strlen($value) < $length) {
1166 $tokens = $this->get_render_tokens($alter);
1167 $more_link_text = $this->options['alter']['more_link_text'] ? $this->options['alter']['more_link_text'] : t('more');
1168 $more_link_text = strtr(filter_xss_admin($more_link_text), $tokens);
1169 $more_link_path = $this->options['alter']['more_link_path'];
1170 $more_link_path = strip_tags(decode_entities(strtr($more_link_path, $tokens)));
1172 // Take sure that paths which was runned through url() does work as well.
1173 $base_path = base_path();
1174 // Checks whether the path starts with the base_path.
1175 if (strpos($more_link_path, $base_path) === 0) {
1176 $more_link_path = drupal_substr($more_link_path, drupal_strlen($base_path));
1179 $more_link = l($more_link_text, $more_link_path, array('attributes' => array('class' => array('views-more-link'))));
1181 $suffix .= " " . $more_link;
1185 if (!empty($alter['nl2br'])) {
1186 $value = nl2br($value);
1188 $this->last_render_text = $value;
1190 if (!empty($alter['make_link']) && !empty($alter['path'])) {
1191 if (!isset($tokens)) {
1192 $tokens = $this->get_render_tokens($alter);
1194 $value = $this->render_as_link($alter, $value, $tokens);
1197 return $value . $suffix;
1201 * Render this field as altered text, from a fieldset set by the user.
1203 function render_altered($alter, $tokens) {
1204 // Filter this right away as our substitutions are already sanitized.
1205 $value = filter_xss_admin($alter['text']);
1206 $value = strtr($value, $tokens);
1212 * Trim the field down to the specified length.
1214 function render_trim_text($alter, $value) {
1215 if (!empty($alter['strip_tags'])) {
1216 // NOTE: It's possible that some external fields might override the
1217 // element type so if someone from, say, CCK runs into a bug here,
1218 // this may be why =)
1219 $this->definition['element type'] = 'span';
1221 return views_trim_text($alter, $value);
1225 * Render this field as a link, with the info from a fieldset set by
1228 function render_as_link($alter, $text, $tokens) {
1231 if (!empty($alter['prefix'])) {
1232 $value .= filter_xss_admin(strtr($alter['prefix'], $tokens));
1237 'absolute' => !empty($alter['absolute']) ? TRUE : FALSE,
1240 // $path will be run through check_url() by l() so we do not need to
1241 // sanitize it ourselves.
1242 $path = $alter['path'];
1244 // strip_tags() removes <front>, so check whether its different to front.
1245 if ($path != '<front>') {
1246 // Use strip tags as there should never be HTML in the path.
1247 // However, we need to preserve special characters like " that
1248 // were removed by check_plain().
1249 $path = strip_tags(decode_entities(strtr($path, $tokens)));
1251 if (!empty($alter['path_case']) && $alter['path_case'] != 'none') {
1252 $path = $this->case_transform($path, $this->options['alter']['path_case']);
1255 if (!empty($alter['replace_spaces'])) {
1256 $path = str_replace(' ', '-', $path);
1260 // Parse the URL and move any query and fragment parameters out of the path.
1261 $url = parse_url($path);
1263 // Seriously malformed URLs may return FALSE or empty arrays.
1268 // If the path is empty do not build a link around the given text and return
1270 // http://www.example.com URLs will not have a $url['path'], so check host as well.
1271 if (empty($url['path']) && empty($url['host']) && empty($url['fragment'])) {
1275 // If no scheme is provided in the $path, assign the default 'http://'.
1276 // This allows a url of 'www.example.com' to be converted to 'http://www.example.com'.
1277 // Only do this on for external URLs.
1278 if ($alter['external']){
1279 if (!isset($url['scheme'])) {
1280 // There is no scheme, add the default 'http://' to the $path.
1281 $path = "http://$path";
1282 // Reset the $url array to include the new scheme.
1283 $url = parse_url($path);
1287 if (isset($url['query'])) {
1288 $path = strtr($path, array('?' . $url['query'] => ''));
1289 $query = drupal_get_query_array($url['query']);
1290 // Remove query parameters that were assigned a query string replacement
1291 // token for which there is no value available.
1292 foreach ($query as $param => $val) {
1293 if ($val == '%' . $param) {
1294 unset($query[$param]);
1297 $options['query'] = $query;
1299 if (isset($url['fragment'])) {
1300 $path = strtr($path, array('#' . $url['fragment'] => ''));
1301 // If the path is empty we want to have a fragment for the current site.
1303 $options['external'] = TRUE;
1305 $options['fragment'] = $url['fragment'];
1308 $alt = strtr($alter['alt'], $tokens);
1309 // Set the title attribute of the link only if it improves accessibility
1310 if ($alt && $alt != $text) {
1311 $options['attributes']['title'] = decode_entities($alt);
1314 $class = strtr($alter['link_class'], $tokens);
1316 $options['attributes']['class'] = array($class);
1319 if (!empty($alter['rel']) && $rel = strtr($alter['rel'], $tokens)) {
1320 $options['attributes']['rel'] = $rel;
1323 $target = check_plain(trim(strtr($alter['target'],$tokens)));
1324 if (!empty($target)) {
1325 $options['attributes']['target'] = $target;
1328 // Allow the addition of arbitrary attributes to links. Additional attributes
1329 // currently can only be altered in preprocessors and not within the UI.
1330 if (isset($alter['link_attributes']) && is_array($alter['link_attributes'])) {
1331 foreach ($alter['link_attributes'] as $key => $attribute) {
1332 if (!isset($options['attributes'][$key])) {
1333 $options['attributes'][$key] = strtr($attribute, $tokens);
1338 // If the query and fragment were programatically assigned overwrite any
1340 if (isset($alter['query'])) {
1341 // Convert the query to a string, perform token replacement, and then
1342 // convert back to an array form for l().
1343 $options['query'] = drupal_http_build_query($alter['query']);
1344 $options['query'] = strtr($options['query'], $tokens);
1345 $options['query'] = drupal_get_query_array($options['query']);
1347 if (isset($alter['alias'])) {
1348 // Alias is a boolean field, so no token.
1349 $options['alias'] = $alter['alias'];
1351 if (isset($alter['fragment'])) {
1352 $options['fragment'] = strtr($alter['fragment'], $tokens);
1354 if (isset($alter['language'])) {
1355 $options['language'] = $alter['language'];
1358 // If the url came from entity_uri(), pass along the required options.
1359 if (isset($alter['entity'])) {
1360 $options['entity'] = $alter['entity'];
1362 if (isset($alter['entity_type'])) {
1363 $options['entity_type'] = $alter['entity_type'];
1366 $value .= l($text, $path, $options);
1368 if (!empty($alter['suffix'])) {
1369 $value .= filter_xss_admin(strtr($alter['suffix'], $tokens));
1376 * Get the 'render' tokens to use for advanced rendering.
1378 * This runs through all of the fields and arguments that
1379 * are available and gets their values. This will then be
1380 * used in one giant str_replace().
1382 function get_render_tokens($item) {
1384 if (!empty($this->view->build_info['substitutions'])) {
1385 $tokens = $this->view->build_info['substitutions'];
1388 foreach ($this->view->display_handler->get_handlers('argument') as $arg => $handler) {
1389 $token = '%' . ++$count;
1390 if (!isset($tokens[$token])) {
1391 $tokens[$token] = '';
1394 // Use strip tags as there should never be HTML in the path.
1395 // However, we need to preserve special characters like " that
1396 // were removed by check_plain().
1397 $tokens['!' . $count] = isset($this->view->args[$count - 1]) ? strip_tags(decode_entities($this->view->args[$count - 1])) : '';
1400 // Get flattened set of tokens for any array depth in $_GET parameters.
1401 $tokens += $this->get_token_values_recursive($_GET);
1403 // Now add replacements for our fields.
1404 foreach ($this->view->display_handler->get_handlers('field') as $field => $handler) {
1405 if (isset($handler->last_render)) {
1406 $tokens["[$field]"] = $handler->last_render;
1409 $tokens["[$field]"] = '';
1411 if (!empty($item)) {
1412 $this->add_self_tokens($tokens, $item);
1415 // We only use fields up to (and including) this one.
1416 if ($field == $this->options['id']) {
1421 // Store the tokens for the row so we can reference them later if necessary.
1422 $this->view->style_plugin->render_tokens[$this->view->row_index] = $tokens;
1423 $this->last_tokens = $tokens;
1429 * Recursive function to add replacements for nested query string parameters.
1431 * E.g. if you pass in the following array:
1445 * Would yield the following array of tokens:
1447 * '%foo_a' => 'value'
1448 * '%foo_b' => 'value'
1449 * '%bar_a' => 'value'
1450 * '%bar_b_c' => 'value'
1454 * An array of values.
1456 * @param $parent_keys
1457 * An array of parent keys. This will represent the array depth.
1460 * An array of available tokens, with nested keys representative of the array structure.
1462 function get_token_values_recursive(array $array, array $parent_keys = array()) {
1465 foreach ($array as $param => $val) {
1466 if (is_array($val)) {
1467 // Copy parent_keys array, so we don't afect other elements of this iteration.
1468 $child_parent_keys = $parent_keys;
1469 $child_parent_keys[] = $param;
1470 // Get the child tokens.
1471 $child_tokens = $this->get_token_values_recursive($val, $child_parent_keys);
1472 // Add them to the current tokens array.
1473 $tokens += $child_tokens;
1476 // Create a token key based on array element structure.
1477 $token_string = !empty($parent_keys) ? implode('_', $parent_keys) . '_' . $param : $param;
1478 $tokens['%' . $token_string] = strip_tags(decode_entities($val));
1486 * Add any special tokens this field might use for itself.
1488 * This method is intended to be overridden by items that generate
1489 * fields as a list. For example, the field that displays all terms
1490 * on a node might have tokens for the tid and the term.
1492 * By convention, tokens should follow the format of [token-subtoken]
1493 * where token is the field ID and subtoken is the field. If the
1494 * field ID is terms, then the tokens might be [terms-tid] and [terms-name].
1496 function add_self_tokens(&$tokens, $item) { }
1499 * Document any special tokens this field might use for itself.
1501 * @see add_self_tokens()
1503 function document_self_tokens(&$tokens) { }
1506 * Call out to the theme() function, which probably just calls render() but
1507 * allows sites to override output fairly easily.
1509 function theme($values) {
1510 return theme($this->theme_functions(),
1512 'view' => $this->view,
1518 function theme_functions() {
1520 $hook = 'views_view_field';
1522 $display = $this->view->display[$this->view->current_display];
1524 if (!empty($display)) {
1525 $themes[] = $hook . '__' . $this->view->name . '__' . $display->id . '__' . $this->options['id'];
1526 $themes[] = $hook . '__' . $this->view->name . '__' . $display->id;
1527 $themes[] = $hook . '__' . $display->id . '__' . $this->options['id'];
1528 $themes[] = $hook . '__' . $display->id;
1529 if ($display->id != $display->display_plugin) {
1530 $themes[] = $hook . '__' . $this->view->name . '__' . $display->display_plugin . '__' . $this->options['id'];
1531 $themes[] = $hook . '__' . $this->view->name . '__' . $display->display_plugin;
1532 $themes[] = $hook . '__' . $display->display_plugin . '__' . $this->options['id'];
1533 $themes[] = $hook . '__' . $display->display_plugin;
1536 $themes[] = $hook . '__' . $this->view->name . '__' . $this->options['id'];
1537 $themes[] = $hook . '__' . $this->view->name;
1538 $themes[] = $hook . '__' . $this->options['id'];
1544 function ui_name($short = FALSE) {
1545 return $this->get_field(parent::ui_name($short));
1550 * A special handler to take the place of missing or broken handlers.
1552 * @ingroup views_field_handlers
1554 class views_handler_field_broken extends views_handler_field {
1555 function ui_name($short = FALSE) {
1556 return t('Broken/missing handler');
1559 function ensure_my_table() { /* No table to ensure! */ }
1560 function query($group_by = FALSE) { /* No query to run */ }
1561 function options_form(&$form, &$form_state) {
1562 $form['markup'] = array(
1563 '#markup' => '<div class="form-item description">' . 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.') . '</div>',
1568 * Determine if the handler is considered 'broken'
1570 function broken() { return TRUE; }
1574 * Render a numeric value as a size.
1576 * @ingroup views_field_handlers
1578 class views_handler_field_file_size extends views_handler_field {
1579 function option_definition() {
1580 $options = parent::option_definition();
1582 $options['file_size_display'] = array('default' => 'formatted');
1587 function options_form(&$form, &$form_state) {
1588 parent::options_form($form, $form_state);
1589 $form['file_size_display'] = array(
1590 '#title' => t('File size display'),
1591 '#type' => 'select',
1592 '#options' => array(
1593 'formatted' => t('Formatted (in KB or MB)'),
1594 'bytes' => t('Raw bytes'),
1599 function render($values) {
1600 $value = $this->get_value($values);
1602 switch ($this->options['file_size_display']) {
1607 return format_size($value);
1617 * A handler to run a field through simple XSS filtering.
1619 * @ingroup views_field_handlers
1621 class views_handler_field_xss extends views_handler_field {
1622 function render($values) {
1623 $value = $this->get_value($values);
1624 return $this->sanitize_value($value, 'xss');