Answers:
其实很容易。只需查看views.views.inc中可用的Views Global处理程序实现即可。
下面是示例,您可以在没有表的情况下添加字段处理程序,
在hook_views_data中指定您的自定义处理程序,如下所示,
/**
* Implements hook_views_data().
*/
function my_module_views_data() {
$data['custom']['table']['group'] = t('Custom');
$data['custom']['table']['join'] = array(
// #global is a special flag which let's a table appear all the time.
'#global' => array(),
);
$data['custom']['custom_handler'] = array(
'title' => t('Custom Handler'),
'help' => t('Custom Handler.'),
'field' => array(
'handler' => 'views_handler_custom_handler',
),
);
return $data;
}
为您的处理程序创建一个inc文件,然后在其中放置您的逻辑。如下所示,文件名称为views_handler_custom_handler.inc
/**
* A handler to provide a field that is completely custom by the administrator.
*
* @ingroup views_field_handlers
*/
class views_handler_custom_handler extends views_handler_field {
function query() {
// do nothing -- to override the parent query.
}
function option_definition() {
$options = parent::option_definition();
// Override the alter text option to always alter the text.
$options['alter']['contains']['alter_text'] = array('default' => TRUE);
return $options;
}
function options_form(&$form, &$form_state) {
parent::options_form($form, $form_state);
// Remove the checkbox
unset($form['alter']['alter_text']);
unset($form['alter']['text']['#dependency']);
unset($form['alter']['text']['#process']);
}
function render($values) {
// Render your content.
return 'Sample';
}
}
在模块的信息文件中指定此新处理程序文件
现在,新的字段处理程序将出现在字段列表中。
要完整:您还需要添加hook_views_api
function my_module_views_api() {
return array(
'api' => 3,
'path' => drupal_get_path('module', 'my_module'),
);
}