| 1 |
<?php
|
| 2 |
// $Id: views_customfield.module,v 1.2 2008/10/30 18:45:23 casey Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* Implementation of hook_views_tables()
|
| 6 |
*
|
| 7 |
* @return array
|
| 8 |
*/
|
| 9 |
function views_customfield_views_tables() {
|
| 10 |
$tables['views_customfield'] = array(
|
| 11 |
'fields' => array(
|
| 12 |
'text' => array(
|
| 13 |
'name' => t('Views Custom field: Text'),
|
| 14 |
'help' => t('Display row-specific custom text.'),
|
| 15 |
'sortable' => FALSE,
|
| 16 |
'notafield' => TRUE,
|
| 17 |
'handler' => 'views_customfield_text_field_handler',
|
| 18 |
/**
|
| 19 |
* @see http://drupal.org/node/99792#option (complex option fields)
|
| 20 |
*/
|
| 21 |
'option' => array(
|
| 22 |
'value' => array(
|
| 23 |
'#type' => 'textarea',
|
| 24 |
'#title' => t('Value'),
|
| 25 |
'#description' =>
|
| 26 |
t('The text that should be displayed.')
|
| 27 |
.' '.t('Include <?php ?> delimiters when using PHP code.')
|
| 28 |
.' '.t('Available variables:').'<br/>'
|
| 29 |
.t('$data: contains the entire record from the database (e.g. $data->nid).').'<br/>'
|
| 30 |
.t('$static: can be used to store reusable data per row.')
|
| 31 |
,
|
| 32 |
'#rows' => 5,
|
| 33 |
),
|
| 34 |
'#type' => 'views_customfield_option',
|
| 35 |
'#process' => array('_views_customfield_text_option_process' => array()),
|
| 36 |
'#after_build' => array('_views_customfield_text_option_after_build'),
|
| 37 |
),
|
| 38 |
)
|
| 39 |
)
|
| 40 |
);
|
| 41 |
|
| 42 |
if (module_exists('validator_phpcode')) {
|
| 43 |
$tables['views_customfield']['fields']['text']['validate'] = 'views_customfield_text_field_validate';
|
| 44 |
}
|
| 45 |
|
| 46 |
return $tables;
|
| 47 |
}
|
| 48 |
|
| 49 |
/**
|
| 50 |
* Validate field settings.
|
| 51 |
*
|
| 52 |
* Make sure PHP code doesn't contain any errors.
|
| 53 |
*
|
| 54 |
* @see http://drupal.org/node/99565#fields
|
| 55 |
*
|
| 56 |
* @param array $fielddata
|
| 57 |
* @param array $view
|
| 58 |
* @param array $form
|
| 59 |
*/
|
| 60 |
function views_customfield_text_field_validate($fielddata, $view, $form) {
|
| 61 |
$options = unserialize($fielddata['options']);
|
| 62 |
$code = $options['value'];
|
| 63 |
|
| 64 |
if (($error = validator_phpcode_has_errors($code))) {
|
| 65 |
// TODO not very efficient, but $fielddata doesn't contain position-info
|
| 66 |
// Some other way possible?
|
| 67 |
for ($i=0;$i<$view['field']['count'];$i++) {
|
| 68 |
if ($form['field'][$i]['fullname']['#value'] == 'views_customfield.text'
|
| 69 |
&& $form['field'][$i]['options']['value']['#value'] == $code) {
|
| 70 |
$msg = t('Views Custom field') .': '. $error[0] .' on line '. $error[1];
|
| 71 |
form_error($form['field'][$i]['options']['value'], $msg);
|
| 72 |
return;
|
| 73 |
}
|
| 74 |
}
|
| 75 |
}
|
| 76 |
}
|
| 77 |
|
| 78 |
/**
|
| 79 |
* Format a text-customfield.
|
| 80 |
*
|
| 81 |
* @param array $fieldinfo
|
| 82 |
* @param array $fielddata
|
| 83 |
* @param string $value
|
| 84 |
* @param object $data
|
| 85 |
* @return string
|
| 86 |
*/
|
| 87 |
function views_customfield_text_field_handler($fieldinfo, $fielddata, $value, $data) {
|
| 88 |
static $static;
|
| 89 |
// TODO should be possible to unserialize just once per view/field
|
| 90 |
$options = unserialize($fielddata['options']);
|
| 91 |
|
| 92 |
return _views_customfield_eval($options['value'], $static[$fielddata['position']], $data);
|
| 93 |
}
|
| 94 |
|
| 95 |
/**
|
| 96 |
* Evaluate a string of PHP code. Mostly copied from drupal_eval().
|
| 97 |
*
|
| 98 |
* @param string $code
|
| 99 |
* @param mixed $static
|
| 100 |
* @param array $data
|
| 101 |
* @return string
|
| 102 |
*/
|
| 103 |
function _views_customfield_eval($code, &$static, $data) {
|
| 104 |
global $theme_path, $theme_info, $conf;
|
| 105 |
|
| 106 |
// Store current theme path.
|
| 107 |
$old_theme_path = $theme_path;
|
| 108 |
|
| 109 |
// Restore theme_path to the theme, as long as drupal_eval() executes,
|
| 110 |
// so code evaluted will not see the caller module as the current theme.
|
| 111 |
// If theme info is not initialized get the path from theme_default.
|
| 112 |
if (!isset($theme_info)) {
|
| 113 |
$theme_path = drupal_get_path('theme', $conf['theme_default']);
|
| 114 |
}
|
| 115 |
else {
|
| 116 |
$theme_path = dirname($theme_info->filename);
|
| 117 |
}
|
| 118 |
|
| 119 |
ob_start();
|
| 120 |
print eval('?>'. $code);
|
| 121 |
$output = ob_get_contents();
|
| 122 |
ob_end_clean();
|
| 123 |
|
| 124 |
// Recover original theme path.
|
| 125 |
$theme_path = $old_theme_path;
|
| 126 |
|
| 127 |
return $output;
|
| 128 |
}
|
| 129 |
|
| 130 |
|
| 131 |
/**
|
| 132 |
* The #process takes the serialized #default_value and feeds
|
| 133 |
* the forms beneath it.
|
| 134 |
*
|
| 135 |
* @param array $element
|
| 136 |
* @return array
|
| 137 |
*/
|
| 138 |
function _views_customfield_text_option_process($element) {
|
| 139 |
$values = unserialize($element['#default_value']);
|
| 140 |
if (!is_array($values)) {
|
| 141 |
// set default values for options that have no stored value.
|
| 142 |
$values = array(
|
| 143 |
'value' => '');
|
| 144 |
}
|
| 145 |
$element['value']['#default_value'] = $values['value'];
|
| 146 |
return $element;
|
| 147 |
}
|
| 148 |
|
| 149 |
/**
|
| 150 |
* Put the value back.
|
| 151 |
*
|
| 152 |
* @param array $element
|
| 153 |
* @return array
|
| 154 |
*/
|
| 155 |
function _views_customfield_text_option_after_build($element) {
|
| 156 |
$values = array();
|
| 157 |
$values['value'] = $element['value']['#value'];
|
| 158 |
|
| 159 |
$element['#value'] = serialize($values);
|
| 160 |
form_set_value($element, $element['#value']);
|
| 161 |
return $element;
|
| 162 |
}
|
| 163 |
|
| 164 |
/**
|
| 165 |
* Creates output for the views_customfield_option form-element type.
|
| 166 |
* Contains a small hack to improve the UI when editing a view.
|
| 167 |
*
|
| 168 |
* @return string
|
| 169 |
*/
|
| 170 |
function theme_views_customfield_option($element) {
|
| 171 |
return '</td><td colspan="7"></td></tr><tr><td colspan="4">'.$element['#children'];
|
| 172 |
}
|
| 173 |
|