View source
<?php
class views_handler_field extends views_handler {
var $field_alias = 'unknown';
var $aliases = array();
function construct() {
parent::construct();
$this->additional_fields = array();
if (!empty($this->definition['additional fields'])) {
$this->additional_fields = $this->definition['additional fields'];
}
if (!isset($this->options['exclude'])) {
$this->options['exclude'] = '';
}
}
function allow_advanced_render() {
return TRUE;
}
function init(&$view, $options) {
parent::init($view, $options);
$this->options += array(
'exclude' => FALSE,
);
}
function query() {
$this
->ensure_my_table();
$this->field_alias = $this->query
->add_field($this->table_alias, $this->real_field);
$this
->add_additional_fields();
}
function add_additional_fields($fields = NULL) {
if (!isset($fields)) {
if (empty($this->additional_fields)) {
return;
}
$fields = $this->additional_fields;
}
if (!empty($fields) && is_array($fields)) {
foreach ($fields as $identifier => $info) {
if (is_array($info)) {
if (isset($info['table'])) {
$table_alias = $this->query
->ensure_table($info['table'], $this->relationship);
}
else {
$table_alias = $this->table_alias;
}
if (empty($table_alias)) {
vpr(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'],
)));
$this->aliases[$identifier] = 'broken';
continue;
}
$this->aliases[$identifier] = $this->query
->add_field($table_alias, $info['field']);
}
else {
$this->aliases[$info] = $this->query
->add_field($this->table_alias, $info);
}
}
}
}
function click_sort($order) {
$this->query
->add_orderby($this->table_alias, $this->real_field, $order, $this->field_alias);
}
function click_sortable() {
return !empty($this->definition['click sortable']);
}
function label() {
if (!isset($this->options['label'])) {
return '';
}
return $this->options['label'];
}
function element_type() {
if (isset($this->definition['element type'])) {
return $this->definition['element type'];
}
return 'span';
}
function option_definition() {
$options = parent::option_definition();
$options['label'] = array(
'default' => $this->definition['title'],
'translatable' => TRUE,
);
$options['alter'] = array(
'contains' => array(
'alter_text' => array(
'default' => FALSE,
),
'text' => array(
'default' => '',
'translatable' => TRUE,
),
'make_link' => array(
'default' => FALSE,
),
'path' => array(
'default' => '',
'translatable' => TRUE,
),
'absolute' => array(
'default' => '',
'translatable' => FALSE,
),
'alt' => array(
'default' => '',
'translatable' => TRUE,
),
'rel' => array(
'default' => '',
),
'link_class' => array(
'default' => '',
),
'prefix' => array(
'default' => '',
'translatable' => TRUE,
),
'suffix' => array(
'default' => '',
'translatable' => TRUE,
),
'target' => array(
'default' => '',
'translatable' => TRUE,
),
'trim' => array(
'default' => FALSE,
),
'max_length' => array(
'default' => '',
),
'word_boundary' => array(
'default' => TRUE,
),
'ellipsis' => array(
'default' => TRUE,
),
'strip_tags' => array(
'default' => FALSE,
),
'html' => array(
'default' => FALSE,
),
),
);
$options['empty'] = array(
'default' => '',
'translatable' => TRUE,
);
$options['hide_empty'] = array(
'default' => FALSE,
);
$options['empty_zero'] = array(
'default' => FALSE,
);
$options['hide_alter_empty'] = array(
'default' => TRUE,
);
return $options;
}
function options_form(&$form, &$form_state) {
$form['label'] = array(
'#type' => 'textfield',
'#title' => t('Label'),
'#default_value' => isset($this->options['label']) ? $this->options['label'] : '',
'#description' => t('The label for this field that will be displayed to end users if the style requires it.'),
);
$form['exclude'] = array(
'#type' => 'checkbox',
'#title' => t('Exclude from display'),
'#default_value' => $this->options['exclude'],
'#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.'),
);
if ($this
->allow_advanced_render()) {
$form['alter']['#tree'] = TRUE;
$form['alter']['alter_text'] = array(
'#type' => 'checkbox',
'#title' => t('Rewrite the output of this field'),
'#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.'),
'#default_value' => $this->options['alter']['alter_text'],
);
$form['alter']['text'] = array(
'#title' => t('Text'),
'#type' => 'textarea',
'#default_value' => $this->options['alter']['text'],
'#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.'),
'#process' => array(
'views_process_dependency',
),
'#dependency' => array(
'edit-options-alter-alter-text' => array(
1,
),
),
);
$form['alter']['make_link'] = array(
'#type' => 'checkbox',
'#title' => t('Output this field as a link'),
'#description' => t('If checked, this field will be made into a link. The destination must be given below.'),
'#default_value' => $this->options['alter']['make_link'],
);
$form['alter']['path'] = array(
'#title' => t('Link path'),
'#type' => 'textfield',
'#default_value' => $this->options['alter']['path'],
'#description' => t('The Drupal path or absolute URL for this link. You may enter data from this view as per the "Replacement patterns" below.'),
'#process' => array(
'views_process_dependency',
),
'#dependency' => array(
'edit-options-alter-make-link' => array(
1,
),
),
'#maxlength' => 255,
);
$form['alter']['absolute'] = array(
'#type' => 'checkbox',
'#title' => t('Use absolute path'),
'#default_value' => $this->options['alter']['absolute'],
'#process' => array(
'views_process_dependency',
),
'#dependency' => array(
'edit-options-alter-make-link' => array(
1,
),
),
);
$form['alter']['link_class'] = array(
'#title' => t('Link class'),
'#type' => 'textfield',
'#default_value' => $this->options['alter']['link_class'],
'#description' => t('The CSS class to apply to the link.'),
'#process' => array(
'views_process_dependency',
),
'#dependency' => array(
'edit-options-alter-make-link' => array(
1,
),
),
);
$form['alter']['alt'] = array(
'#title' => t('Alt text'),
'#type' => 'textfield',
'#default_value' => $this->options['alter']['alt'],
'#description' => t('Text to place as "alt" text which most browsers display as a tooltip when hovering over the link.'),
'#process' => array(
'views_process_dependency',
),
'#dependency' => array(
'edit-options-alter-make-link' => array(
1,
),
),
);
$form['alter']['rel'] = array(
'#title' => t('Rel Text'),
'#type' => 'textfield',
'#default_value' => $this->options['alter']['rel'],
'#description' => t('Include Rel attribute for use in lightbox2 or other javascript utility.'),
'#process' => array(
'views_process_dependency',
),
'#dependency' => array(
'edit-options-alter-make-link' => array(
1,
),
),
);
$form['alter']['prefix'] = array(
'#title' => t('Prefix text'),
'#type' => 'textfield',
'#default_value' => $this->options['alter']['prefix'],
'#description' => t('Any text to display before this link. You may include HTML.'),
'#process' => array(
'views_process_dependency',
),
'#dependency' => array(
'edit-options-alter-make-link' => array(
1,
),
),
);
$form['alter']['suffix'] = array(
'#title' => t('Suffix text'),
'#type' => 'textfield',
'#default_value' => $this->options['alter']['suffix'],
'#description' => t('Any text to display after this link. You may include HTML.'),
'#process' => array(
'views_process_dependency',
),
'#dependency' => array(
'edit-options-alter-make-link' => array(
1,
),
),
);
$form['alter']['target'] = array(
'#title' => t('Target'),
'#type' => 'textfield',
'#default_value' => $this->options['alter']['target'],
'#description' => t("Target of the link, such as _blank, _parent or an iframe's name. This field is rarely used."),
'#process' => array(
'views_process_dependency',
),
'#dependency' => array(
'edit-options-alter-make-link' => array(
1,
),
),
);
$options = array();
foreach ($this->view->display_handler
->get_handlers('field') as $field => $handler) {
$options[t('Fields')]["[{$field}]"] = $handler
->ui_name();
if ($field == $this->options['id']) {
break;
}
}
$count = 0;
foreach ($this->view->display_handler
->get_handlers('argument') as $arg => $handler) {
$options[t('Arguments')]['%' . ++$count] = t('@argument title', array(
'@argument' => $handler
->ui_name(),
));
$options[t('Arguments')]['!' . $count] = t('@argument input', array(
'@argument' => $handler
->ui_name(),
));
}
$this
->document_self_tokens($options[t('Fields')]);
$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>');
if (!empty($options)) {
$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.
If you would like to have the characters %5B and %5D please use the html entity codes \'%5B\' or \'%5D\' or they will get replaced with empty space.</p>');
foreach (array_keys($options) as $type) {
if (!empty($options[$type])) {
$items = array();
foreach ($options[$type] as $key => $value) {
$items[] = $key . ' == ' . $value;
}
$output .= theme('item_list', $items, $type);
}
}
}
$form['alter']['help'] = array(
'#type' => 'hidden',
'#id' => 'views-tokens-help',
'#prefix' => '<div><fieldset id="views-tokens-help"><legend>' . t('Replacement patterns') . '</legend>' . $output . '</fieldset></div>',
'#process' => array(
'views_process_dependency',
),
'#dependency' => array(
'edit-options-alter-make-link' => array(
1,
),
'edit-options-alter-alter-text' => array(
1,
),
),
);
$form['alter']['trim'] = array(
'#type' => 'checkbox',
'#title' => t('Trim this field to a maximum length'),
'#description' => t('If checked, this field be trimmed to a maximum length in characters.'),
'#default_value' => $this->options['alter']['trim'],
);
$form['alter']['max_length'] = array(
'#title' => t('Maximum length'),
'#type' => 'textfield',
'#default_value' => $this->options['alter']['max_length'],
'#description' => t('The maximum number of characters this field can be.'),
'#process' => array(
'views_process_dependency',
),
'#dependency' => array(
'edit-options-alter-trim' => array(
1,
),
),
);
$form['alter']['word_boundary'] = array(
'#type' => 'checkbox',
'#title' => t('Trim only on a word boundary'),
'#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.'),
'#default_value' => $this->options['alter']['word_boundary'],
'#process' => array(
'views_process_dependency',
),
'#dependency' => array(
'edit-options-alter-trim' => array(
1,
),
),
);
$form['alter']['ellipsis'] = array(
'#type' => 'checkbox',
'#title' => t('Add an ellipsis'),
'#description' => t('If checked, a "..." will be added if a field was trimmed.'),
'#default_value' => $this->options['alter']['ellipsis'],
'#process' => array(
'views_process_dependency',
),
'#dependency' => array(
'edit-options-alter-trim' => array(
1,
),
),
);
$form['alter']['html'] = array(
'#type' => 'checkbox',
'#title' => t('Field can contain HTML'),
'#description' => t('If checked, HTML corrector will be run to ensure tags are properly closed after trimming.'),
'#default_value' => $this->options['alter']['html'],
'#process' => array(
'views_process_dependency',
),
'#dependency' => array(
'edit-options-alter-trim' => array(
1,
),
),
);
$form['alter']['strip_tags'] = array(
'#type' => 'checkbox',
'#title' => t('Strip HTML tags'),
'#description' => t('If checked, all HTML tags will be stripped.'),
'#default_value' => $this->options['alter']['strip_tags'],
'#process' => array(
'views_process_dependency',
),
);
}
$form['empty'] = array(
'#type' => 'textfield',
'#title' => t('Empty text'),
'#default_value' => $this->options['empty'],
'#description' => t('If the field is empty, display this text instead.'),
);
$form['empty_zero'] = array(
'#type' => 'checkbox',
'#title' => t('Count the number 0 as empty'),
'#default_value' => $this->options['empty_zero'],
'#description' => t('If the field contains the number zero, display the empty text instead'),
);
$form['hide_empty'] = array(
'#type' => 'checkbox',
'#title' => t('Hide if empty'),
'#default_value' => $this->options['hide_empty'],
'#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.'),
);
$form['hide_alter_empty'] = array(
'#type' => 'checkbox',
'#title' => t('Do not rewrite if empty'),
'#default_value' => $this->options['hide_alter_empty'],
);
}
function admin_summary() {
return $this
->label();
}
function pre_render(&$values) {
}
function render($values) {
$value = $values->{$this->field_alias};
return check_plain($value);
}
function advanced_render($values) {
if ($this
->allow_advanced_render() && method_exists($this, 'render_item')) {
$raw_items = $this
->get_items($values);
}
else {
$this->last_render = $value = $this
->render($values);
$this->original_value = $value;
}
if ($this
->allow_advanced_render()) {
$tokens = NULL;
if (method_exists($this, 'render_item')) {
$items = array();
foreach ($raw_items as $count => $item) {
$this->last_render = $this
->render_item($count, $item);
$this->original_value = $this->last_render;
$alter = $item + $this->options['alter'];
$items[] = $this
->render_text($alter);
}
$value = $this
->render_items($items);
}
else {
$value = $this
->render_text($this->options['alter']);
}
$this->last_render = $value;
}
if (empty($this->last_render)) {
if ($this->last_render !== 0 && $this->last_render !== '0' || !empty($this->options['empty_zero'])) {
$alter = $this->options['alter'];
$alter['alter_text'] = 1;
$alter['text'] = $this->options['empty'];
$this->last_render = $this
->render_text($alter);
}
}
return $this->last_render;
}
function render_text($alter) {
$value = trim($this->last_render);
if (!empty($alter['alter_text']) && $alter['text'] !== '') {
$tokens = $this
->get_render_tokens($alter);
$value = $this
->render_altered($alter, $tokens);
}
if (($this->options['hide_empty'] && empty($value) || $this->options['hide_alter_empty'] && empty($this->original_value)) && ($value !== 0 || $this->options['empty_zero'])) {
return '';
}
if (!empty($alter['strip_tags'])) {
$value = strip_tags($value);
}
if (!empty($alter['trim']) && !empty($alter['max_length'])) {
$value = $this
->render_trim_text($alter, $value);
}
if (!empty($alter['make_link']) && !empty($alter['path'])) {
if (!isset($tokens)) {
$tokens = $this
->get_render_tokens($alter);
}
$value = $this
->render_as_link($alter, $value, $tokens);
}
return $value;
}
function render_altered($alter, $tokens) {
$value = filter_xss_admin($alter['text']);
$value = strtr($value, $tokens);
return $value;
}
function render_trim_text($alter, $value) {
if (!empty($alter['strip_tags'])) {
$this->definition['element type'] = 'span';
}
return views_trim_text($alter, $value);
}
function render_as_link($alter, $text, $tokens) {
$value = '';
if (!empty($alter['prefix'])) {
$value .= filter_xss_admin(strtr($alter['prefix'], $tokens));
}
$options = array(
'html' => TRUE,
'absolute' => !empty($alter['absolute']) ? TRUE : FALSE,
);
$path = $alter['path'];
if ($path != '<front>') {
$path = strip_tags(html_entity_decode(strtr($path, $tokens)));
}
if (empty($path)) {
return $text;
}
$url = parse_url($path);
if (isset($url['query'])) {
$path = strtr($path, array(
'?' . $url['query'] => '',
));
$options['query'] = $url['query'];
}
if (isset($url['fragment'])) {
$path = strtr($path, array(
'#' . $url['fragment'] => '',
));
if ($path == '') {
$options['external'] = TRUE;
}
$options['fragment'] = $url['fragment'];
}
$alt = strtr($alter['alt'], $tokens);
if ($alt && $alt != $text) {
$options['attributes']['title'] = html_entity_decode($alt, ENT_QUOTES);
}
$class = strtr($alter['link_class'], $tokens);
if ($class) {
$options['attributes']['class'] = $class;
}
if (!empty($alter['rel']) && ($rel = strtr($alter['rel'], $tokens))) {
$options['attributes']['rel'] = $rel;
}
$target = check_plain(trim(strtr($alter['target'], $tokens)));
if (!empty($target)) {
$options['attributes']['target'] = $target;
}
if (isset($alter['link_attributes']) && is_array($alter['link_attributes'])) {
foreach ($alter['link_attributes'] as $key => $attribute) {
if (!isset($options['attributes'][$key])) {
$options['attributes'][$key] = strtr($attribute, $tokens);
}
}
}
if (isset($alter['query'])) {
$options['query'] = strtr($alter['query'], $tokens);
}
if (isset($alter['alias'])) {
$options['alias'] = $alter['alias'];
}
if (isset($alter['fragment'])) {
$options['fragment'] = strtr($alter['fragment'], $tokens);
}
if (isset($this->options['alter']['language'])) {
$options['language'] = $this->options['alter']['language'];
}
$value .= l($text, $path, $options);
if (!empty($alter['suffix'])) {
$value .= filter_xss_admin(strtr($alter['suffix'], $tokens));
}
return $value;
}
function get_render_tokens($item) {
$tokens = array();
if (!empty($this->view->build_info['substitutions'])) {
$tokens = $this->view->build_info['substitutions'];
}
$count = 0;
foreach ($this->view->display_handler
->get_handlers('argument') as $arg => $handler) {
$token = '%' . ++$count;
if (!isset($tokens[$token])) {
$tokens[$token] = '';
}
$tokens['!' . $count] = isset($this->view->args[$count - 1]) ? strip_tags(html_entity_decode($this->view->args[$count - 1])) : '';
}
foreach ($this->view->display_handler
->get_handlers('field') as $field => $handler) {
if (isset($handler->last_render)) {
$tokens["[{$field}]"] = $handler->last_render;
}
else {
$tokens["[{$field}]"] = '';
}
$this
->add_self_tokens($tokens, $item);
if ($field == $this->options['id']) {
break;
}
}
return $tokens;
}
function add_self_tokens(&$tokens, $item) {
}
function document_self_tokens(&$tokens) {
}
function theme($values) {
return theme($this
->theme_functions(), $this->view, $this, $values);
}
function theme_functions() {
$themes = array();
$hook = 'views_view_field';
$display = $this->view->display[$this->view->current_display];
if (!empty($display)) {
$themes[] = $hook . '__' . $this->view->name . '__' . $display->id . '__' . $this->options['id'];
$themes[] = $hook . '__' . $this->view->name . '__' . $display->id;
$themes[] = $hook . '__' . $display->id . '__' . $this->options['id'];
$themes[] = $hook . '__' . $display->id;
if ($display->id != $display->display_plugin) {
$themes[] = $hook . '__' . $this->view->name . '__' . $display->display_plugin . '__' . $this->options['id'];
$themes[] = $hook . '__' . $this->view->name . '__' . $display->display_plugin;
$themes[] = $hook . '__' . $display->display_plugin . '__' . $this->options['id'];
$themes[] = $hook . '__' . $display->display_plugin;
}
}
$themes[] = $hook . '__' . $this->view->name . '__' . $this->options['id'];
$themes[] = $hook . '__' . $this->view->name;
$themes[] = $hook . '__' . $this->options['id'];
$themes[] = $hook;
return $themes;
}
}
class views_handler_field_broken extends views_handler_field {
function ui_name($short = FALSE) {
return t('Broken/missing handler');
}
function ensure_my_table() {
}
function query() {
}
function options_form(&$form, &$form_state) {
$form['markup'] = array(
'#prefix' => '<div class="form-item description">',
'#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.'),
);
}
function broken() {
return TRUE;
}
}
class views_handler_field_file_size extends views_handler_field {
function option_definition() {
$options = parent::option_definition();
$options['file_size_display'] = array(
'default' => 'formatted',
);
return $options;
}
function options_form(&$form, &$form_state) {
parent::options_form($form, $form_state);
$form['file_size_display'] = array(
'#title' => t('File size display'),
'#type' => 'select',
'#options' => array(
'formatted' => t('Formatted (in KB or MB)'),
'bytes' => t('Raw bytes'),
),
);
}
function render($values) {
if ($values->{$this->field_alias}) {
switch ($this->options['file_size_display']) {
case 'bytes':
return $values->{$this->field_alias};
case 'formatted':
default:
return format_size($values->{$this->field_alias});
}
}
else {
return '';
}
}
}
class views_handler_field_xss extends views_handler_field {
function render($values) {
$value = $values->{$this->field_alias};
return filter_xss($value);
}
}