Answers:
您需要使用以下挂钩创建小模块:
/**
* Implements hook_form_alter().
*
* Alter exposed filter form in views
*/
function MODULE_form_views_exposed_form_alter(&$form, &$form_state, $form_id) {
if (isset($form['sort_by'])) {
// Combine sort drop-downs into one.
$form['sorting'] = array(
'#type' => 'select',
'#id' => 'sort',
'#title' => $form['sort_by']['#title'],
);
foreach ($form['sort_by']['#options'] as $sort_by_key => $sort_by_title) {
foreach ($form['sort_order']['#options'] as $sort_order_key => $sort_order_title) {
$form['sorting']['#options'][$sort_by_key . '|' . $sort_order_key] = $sort_by_title . ' ' . $sort_order_title;
}
}
// Get default value for combined sort.
$sort_by_keys = array_keys($form['sort_by']['#options']);
$form['sorting']['#default_value'] = $sort_by_keys[0] . '|' . $form['sort_order']['#default_value'];
}
// Explode combined sort field into two values that are appropriate for views.
if (isset($form_state['input']['sorting'])) {
$sorting = explode('|', $form_state['input']['sorting']);
$form_state['input']['sort_by'] = $sorting[0];
$form_state['input']['sort_order'] = $sorting[1];
}
}
/**
* Default preprocess function for all filter forms.
*/
function MODULE_preprocess_views_exposed_form(&$vars) {
$form = &$vars['form'];
// Render new created sort field.
if (isset($form['sorting'])) {
$form['sorting']['#printed'] = FALSE;
$vars['sorting'] = drupal_render($form['sorting']);
// Need to rebuild the submit button.
$form['submit']['#printed'] = FALSE;
$vars['button'] = drupal_render_children($form);
}
}
然后,您需要将“ views / theme / views-exposed-form.tpl.php”文件复制到主题文件夹中,并用新的$ sorting替换$ sort_by和$ sort_order小部件
找:
<?php if (!empty($sort_by)): ?>
<div class="views-exposed-widget views-widget-sort-by">
<?php print $sort_by; ?>
</div>
<div class="views-exposed-widget views-widget-sort-order">
<?php print $sort_order; ?>
</div>
<?php endif; ?>
用。。。来代替:
<?php if (!empty($sorting)): ?>
<div class="views-exposed-widget views-widget-sort-by">
<?php print $sorting; ?>
</div>
<?php endif; ?>