Answers:
这是一些示例代码可以做到这一点:
function hook_form_alter(&$form, &$form_state, $form_id) {
switch($form_id) {
case 'views_exposed_form':
$allowed_categories = db_query("SELECT 1 as tid, "Term Name (2)" as `name`")->fetchAllKeyed();
$allowed_categories = array_reverse($allowed_categories, TRUE);
$allowed_categories['All'] = '- Any -';
$allowed_categories = array_reverse($allowed_categories, TRUE);
$form['field_category_tid']['#options'] = $allowed_categories;
break;
}
}
只需更改查询以返回2列-tid和带有计数的名称即可。
function hook_form_alter(&$form, &$form_state, $form_id) {
switch($form_id){
case 'views_exposed_form':
if ($form_state['view']->name == 'viewname') {
//must add some bool so this doesn't get into infinite loop
if(!isset($form_state['view']->gg)){
$form_state['view']->gg = TRUE;
$form_state['view']->execute();
}
$form['results_count'] = array(
'#markup' => t('!count results match your criteria', array('!count' => '<b>'.$form_state['view']->total_rows.'</b>')),
'#weight' => -99,
);
}
break;
}
}
这是我真实网站中的一个示例。field_marka_prochnosti是由分类词汇表表示的节点的字段。我使用phpMyAdmin来学习一个表的名称和该表的字段的名称以进行查询。bricksale_omega是我主题的名称。
function bricksale_omega_form_alter(&$form, &$form_state, $form_id) {
switch($form_id){
case 'views_exposed_form':
foreach ($form['field_marka_prochnosti_tid']['#options'] as $tid => &$value) {
$query = db_select('field_data_field_marka_prochnosti', 'f')
->condition('f.field_marka_prochnosti_tid', $tid);
$query->addExpression('COUNT(*)');
$count = $query->execute()->fetchField();
$value = $value . ' (' . $count . ')';
}
break;
}
}