10

By default Views 3.0 module for Drupal 7 uses two drop-downs to display exposed sort. First drop-down to select field name, and other one - to select sort direction.

Default Views widget for sorting

How do i can exposed sort in one drop-down?

New version of exposed widget for sorting

13

You can use Better exposed filters module for that purpose. It has such option to combine sort field and directions in one select list.

  • Thanx for other solution! – Eugene Fidelin Jul 17 '12 at 10:55
  • 3
    It took me 30 minutes to find that option. On your view, you will find it under BEF settings => Advanced sort options => Combine sort order with sort by. Thanks!!! – Roger May 25 '15 at 12:27
10

You need to create small module with following hooks:

/**
 * 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);
  }
}

Then you need copy "views/theme/views-exposed-form.tpl.php" file into your theme folder and replace $sort_by and $sort_order widgets with new $sorting

Find:

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

Replace with:

<?php if (!empty($sorting)): ?>
  <div class="views-exposed-widget views-widget-sort-by">
    <?php print $sorting; ?>
  </div>
<?php endif; ?>
  • How do i change the label for example "Date de publication ascending" and "Date de publication descending" to "Recent Prodcuts" and "Older Products"? – Ram Sep 25 '13 at 4:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.