View source
<?php
function views_customfield_views_tables() {
$tables['views_customfield'] = array(
'fields' => array(
'text' => array(
'name' => t('Customfield: PHP code'),
'help' => t('Display row-specific custom text using PHP.'),
'sortable' => FALSE,
'notafield' => TRUE,
'handler' => 'views_customfield_handler_field_phpcode',
'option' => array(
'value' => array(
'#type' => 'textarea',
'#title' => t('Value'),
'#description' => t('The text that should be displayed.') . ' ' . t('Include <?php ?> delimiters when using PHP code.') . ' ' . t('Available variables:') . '<br/>' . t('$data: contains the retrieved record from the database (e.g. $data->nid).') . '<br/>' . t('$static: can be used to store reusable data per row.'),
'#rows' => 5,
),
'#type' => 'views_customfield_option',
'#process' => array(
'_views_customfield_option_process' => array(),
),
'#after_build' => array(
'_views_customfield_option_after_build',
),
),
),
'rownumber' => array(
'name' => t('Customfield: Rownumber'),
'help' => t('Display rownumber.'),
'sortable' => FALSE,
'notafield' => TRUE,
'handler' => 'views_customfield_handler_field_rownumber',
'option' => 'string',
),
),
);
if (module_exists('validator_phpcode')) {
$tables['views_customfield']['fields']['text']['validate'] = 'views_customfield_handler_field_phpcode_validate';
}
return $tables;
}
function _views_customfield_option_process($element) {
$values = unserialize($element['#default_value']);
if (!is_array($values)) {
$values = array(
'value' => '',
);
}
$element['value']['#default_value'] = $values['value'];
return $element;
}
function _views_customfield_option_after_build($element) {
$values = array();
$values['value'] = $element['value']['#value'];
$element['#value'] = serialize($values);
form_set_value($element, $element['#value']);
return $element;
}
function theme_views_customfield_option($element) {
return '</td><td colspan="7"></td></tr><tr><td colspan="4">' . $element['#children'];
}
function views_customfield_handler_field_phpcode_validate($fielddata, $view, $form) {
$options = unserialize($fielddata['options']);
$code = $options['value'];
if ($error = validator_phpcode_has_errors($code)) {
for ($i = 0; $i < $view['field']['count']; $i++) {
if ($form['field'][$i]['fullname']['#value'] == 'views_customfield.text' && $form['field'][$i]['options']['value']['#value'] == $code) {
$msg = t('Views Custom field') . ': ' . $error[0] . ' on line ' . $error[1];
form_error($form['field'][$i]['options']['value'], $msg);
return;
}
}
}
}
function views_customfield_handler_field_phpcode($fieldinfo, $fielddata, $value, $data) {
static $static;
$options = unserialize($fielddata['options']);
return views_customfield_handler_field_phpcode_eval($options['value'], $static[$fielddata['position']], $data);
}
function views_customfield_handler_field_phpcode_eval($code, &$static, $data) {
global $theme_path, $theme_info, $conf;
$old_theme_path = $theme_path;
if (!isset($theme_info)) {
$theme_path = drupal_get_path('theme', $conf['theme_default']);
}
else {
$theme_path = dirname($theme_info->filename);
}
ob_start();
print eval('?>' . $code);
$output = ob_get_contents();
ob_end_clean();
$theme_path = $old_theme_path;
return $output;
}
function views_customfield_handler_field_rownumber($fieldinfo, $fielddata, $value, $data) {
global $current_view;
static $i = array();
if (!$i[$current_view->name . $fielddata['position']]) {
$i[$current_view->name . $fielddata['position']] = 0;
}
return $i[$current_view->name . $fielddata['position']]++;
}