如何使用一个下拉小部件在视图中显示公开的排序?


10

默认情况下,Drupal 7的Views 3.0模块使用两个下拉菜单来显示公开的排序。第一个下拉菜单选择字段名称,另一个-选择排序方向。

默认视图窗口小部件进行排序

如何在一个下拉列表中显示排序?

新版本的暴露小部件进行排序

Answers:


13

为此,您可以使用更好的外露式过滤器模块。它具有将排序字段和方向组合在一个选择列表中的选项。


感谢其他解决方案!
尤金·菲德林

3
我花了30分钟才能找到该选项。在您的视图上,您​​可以在BEF设置=>高级排序选项=>将排序顺序与排序依据结合在一起下找到它。谢谢!!!
罗杰

10

您需要使用以下挂钩创建小模块:

/**
 * 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; ?>

如何将标签(例如“发布日期升序”和“发布日期降序”)更改为“最近的产品”和“较旧的产品”?
拉姆2013年
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.