As a feature request, I'd like to see the ability to sort a contextual filter of taxonomy terms based on the weight of the terms.

Maybe I'm missing something, but it doesn't seem possible to do this in Views 3 at the moment.

The ability to create a contextual filter and sort by the weight of taxonomy terms would be a real bonus.

Sometimes taxonomy terms are presented in a specific, non-alphabetic order for a good reason.

For example, the terms might be numeric values that need to be presented in ascending order:

  • 1.000-5.000
  • 5.000-10.000
  • 10.000-50.000

However, using the default Sort by Alphabetical option, these terms are presented as:

  • 1.000-5.000
  • 10.000-50.000
  • 5.000-10.000

There does not appear to be any easy way to override this alphapetical sort and present the terms in ascending numeric order.

An option to sort by Taxonomy Term weight in the contextual filter editing pane would be an ideal way of adding this functionality.

Anyway, just a request. Thanks for all the work on Views. I'm constantly amazed by what this module can do.

Regards.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

merlinofchaos’s picture

Status: Active » Postponed (maintainer needs more info)

How do you sort a filter? I don't understand this request.

merlinofchaos’s picture

Status: Postponed (maintainer needs more info) » Closed (works as designed)

Oh, well. Ignoring the contextual filter, the database sorts strings as strings, not numbers. It's *really* hard to sort a string as a number. If you want to do that, you probably need to provide another hidden field that you do the sorting on. That is not something that will be 'fixed' because that's how databases work.

Strings and numbers sort differently. When you want to sort by your first list, what you're really sorting is the first number in the string. That is just not possible without some kind of transformation of the data.

moreorless’s picture

"How do you sort a filter?"

Well it is an option in the contextual filter editing pane - 'Sort order' and 'Sort by' - see attached screenshot (contextual-filter-editing-pane.png) - so I thought my request would have made sense. Sorry if my explanation wasn't sufficiently clear.

What I'm trying to do is illustrated in the other attachment (contextual-filter-example.png).

I have a list of links to content with associated taxonomy terms.

I have created a contextual filter to filter that list based on the taxonomy terms.

When those terms are numeric the list in the contextual filter may not sort in ascending numeric order.

Thanks for pointing out that it's not possible to prevent this. Now I can start looking for a work-around.

Roi Danton’s picture

Status: Closed (works as designed) » Active

I'm in the same situation wanting to sort taxonomy terms by weight used in a contextual filter.

Is it possible to add another sort option for contextual filter -> summary view than Alphabetical or Number of Records - e.g. taxonomy weight?

sander-martijn’s picture

Version: 7.x-3.x-dev » 7.x-3.5

I also need this and need it often. Since a few got caught up in the numbers being stored as strings, I'll bring it back to the op's question with another example where weight based sorting is the only option (weights are already stored for taxonomy so this isn't a stretch of a request).

A designer needs to show their collections from newest to oldest. The collections are named:
Spring 2013
Fall 2012
Spring 2011
Bespoke commission by Kate Moss
Fall 2010

etc.

Or someone wants to list their clients (clickable to a gallery) and wants to keep the most important ones on the top to feature them more prominently.

Or lots of other reasons why you wouldn't want to show a list of terms in alphabetical order.

nicholas.alipaz’s picture

I am just adding my two cents here. I feel this has more to do with summary sorting than it does with taxonomy. If I am understanding correctly, the request really is to allow specifying alternate sort criteria for the default summary options. I agree that as it stands, these options are not extendable or editable beyond what is there by default.

Here is the code that currently creates the default summary sort options:

  /**
   * Provide a form for selecting further summary options when the
   * default action is set to display one.
   */
  function default_summary_form(&$form, &$form_state) {
    $style_plugins = views_fetch_plugin_data('style');
    $summary_plugins = array();
    $format_options = array();
    foreach ($style_plugins as $key => $plugin) {
      if (isset($plugin['type']) && $plugin['type'] == 'summary') {
        $summary_plugins[$key] = $plugin;
        $format_options[$key] = $plugin['title'];
      }
    }

    $form['summary'] = array(
      // Views custom key, moves this element to the appropriate container
      // under the radio button.
      '#argument_option' => 'summary',
    );
    $form['summary']['sort_order'] = array(
      '#type' => 'radios',
      '#title' => t('Sort order'),
      '#options' => array('asc' => t('Ascending'), 'desc' => t('Descending')),
      '#default_value' => $this->options['summary']['sort_order'],
      '#dependency' => array('radio:options[default_action]' => array('summary')),
    );
    $form['summary']['number_of_records'] = array(
      '#type' => 'radios',
      '#title' => t('Sort by'),
      '#default_value' => $this->options['summary']['number_of_records'],
      '#options' => array(
        0 => $this->get_sort_name(),
        1 => t('Number of records')
      ),
      '#dependency' => array('radio:options[default_action]' => array('summary')),
    );
...

Note the fact that the array $form['summary']['number_of_records']['#options'] is hardcoded and cannot be extended for more options through views plugins. This is an oversight or maybe an untapped feature.

The direction I would like to see this take is to allow any field that is in the fields settings for a display would become available for sorting in these options. On a related note for implementation, it looks like the value of the setting is use to perform the ordering using:

  function summary_sort($order, $by = NULL) {
    $this->query->add_orderby(NULL, NULL, $order, (!empty($by) ? $by : $this->name_alias));
  }

Where $order is either asc or desc and $by is the named alias of the field in the db which is currently not configurable beyond 'num_records' or the named alias coming from the field you are using in the contextual filter. Would be great to have the option to pick something else that is being selected in the current query.

sander-martijn’s picture

nicholas.alipaz' suggestion makes sense and should indeed solve all people's requirements - not only could you choose from any field, but if none of those work for you choose weight and sort the taxonomy the way you want it (which solves the example I posed). I hope these new comments raise the priority on this - I recently ran across the issue again, and unfortunately though I'm good with drupal and php and can make some modification to modules, I've never learned the module API well enough to attempt at messing with a module as complex as views!

nicholas.alipaz’s picture

Version: 7.x-3.5 » 7.x-3.x-dev
Component: taxonomy data » Code

Changing component since it isn't related directly to taxonomy.

nicholas.alipaz’s picture

Title: Sort contextual filter of taxonomy terms by weight » Sort contextual filter of default summary by any field

Changing name to match the issue better.

Also, I had need to sort on taxonomy's weight in particular. I used the following to accomplish it:

/**
 * Implementation of hook_views_query_alter
 * @param type $view
 * @param type $query
 */
function MODULE_views_query_alter(&$view, &$query) {
  if ($view->name == 'release_glossary' && $view->current_display == 'attachment') {
    $query->fields['taxonomy_term_data_taxonomy_term_hierarchy_weight'] = array(
      'field' => 'weight',
      'table' => 'taxonomy_term_data_taxonomy_term_hierarchy',
      'alias' => 'taxonomy_term_data_taxonomy_term_hierarchy_weight',
    );

    $query->field_aliases['taxonomy_term_data_taxonomy_term_hierarchy'] = array(
      'weight' => 'taxonomy_term_data_taxonomy_term_hierarchy_weight',
    );

    $query->orderby[0]['field'] = 'taxonomy_term_data_taxonomy_term_hierarchy_weight';
  }
}

To ascertain the data in these queries I used devel to dpm($query); inside of the main views display page which had the field I wanted to sort by then I copied the field and field_alias from that view display. Lastly set the orderby field to the alias.

timwood’s picture

Seems related or possible duplicate of https://drupal.org/node/417312.

nattyweb’s picture

Issue summary: View changes

Interested in this - hoping to see a solution soon...