Answers:
我潜伏在互联网上,试图找到相同问题的答案,这就是我得到的结果:
在自定义模块中实现多个挂钩。用您的真实姓名替换modulename
和filtername
。
/**
* Implements hook_views_api().
*/
function modulename_views_api() {
return array(
'api' => 2,
'path' => drupal_get_path('module', 'modulename') . '/inc',
);
}
/**
* Implementation of hook_views_handlers() to register all of the basic handlers
* views uses.
*/
function modulename_views_handlers() {
return array(
'info' => array(
// path to handler files
'path' => drupal_get_path('module', 'modulename') . '/inc',
),
'handlers' => array(
// register our custom filter, with the class/file name and parent class
'modulename_handler_filter_filtername' => array(
'parent' => 'views_handler_filter',
),
),
);
}
function modulename_views_data() {
$data = array();
$data['node']['filtername'] = array(
'group' => t('Custom'),
'real field' => 'my_custom_filter_field',
'title' => t('My custom filter'),
'help' => t('Some more detailed description if you need it.'),
'filter' => array(
'handler' => 'modulename_handler_filter_filtername',
),
);
return $data;
}
inc
在模块文件夹内创建名为的文件夹,并在其中命名为文件modulename_handler_filter_filtername.inc
(有关隐式引用,请参见上面的代码)。不要忘记使用实际的模块和过滤器名称。
将以下代码粘贴到该modulename_handler_filter_filtername.inc
文件中。我在此示例中使用的代码创建了一组显示年份的单选按钮。因此,您可以仅使用创建节点的年份按创建年份过滤节点。
class modulename_handler_filter_filtername extends views_handler_filter {
/**
* Options form subform for setting exposed filter options.
*/
function value_form(&$form, &$form_state) {
parent::value_form($form, $form_state);
// get list of years from database
$query = db_select('node', 'n');
$query->addExpression("FROM_UNIXTIME(n.created, '%Y')", 'year');
if (isset($this->view->filter['type'])) {
$query->condition('n.type', $this->view->filter['type']->value, 'IN');
}
$result = $query->orderBy('year', 'ASC')
->execute()
->fetchAllAssoc('year');
$years = array(
'0' => t('All'),
);
foreach ($result as $k => $v) {
$years[$k] = $k;
}
// create form element with options retrieved from database
$form['value']['year'] = array(
'#type' => 'radios',
'#options' => $years,
'#default_value' => end($years),
);
}
/**
* Alters Views query when filter is used.
*/
function query() {
// make sure base table is included in the query
$this->ensure_my_table();
// retrieve real filter name from view options
// this requires 'real field' filter option to be set (see code above)
$real_field_name = $this->real_field;
// get the value of the submitted filter
$value = $this->view->exposed_data[$real_field_name];
// finally, alter Views query
if (is_numeric($value) && $value != 0) {
/*
Having several custom exposed filters, make sure subsitution patterns
(e.g. :filtername_value below) don't match across different filters.
I spent some time figuring out why all my filters had the same value.
It looks like the query skeleton is built first and then all replacements
are made in bulk. Prefixing value with filter name looks good imo.
*/
$this->query->add_where_expression($this->options['group'],
"FROM_UNIXTIME(node.created, '%Y') = :filtername_value",
array(':filtername_value' => $value));
}
}
}
这就是使最简单的自定义暴露滤镜工作所需的一切!
请注意,FROM_UNIXTIME
在SELECT
查询条件下使用可能会降低数据库速度。
$this->query->add_where($this->options['group'], $real_field_name, $this->value['value'], $this->operator);
。可以在上面的链接中找到。