0

I have a long list of files to be downloaded with different categories.

So I created a node for each file.

And assigned a term field for the categories

A file can be used in different categories so I cant sort by posted date or the order might be wrong.

Is it possible to create a text field on nodes and then order them in a view based on that number? Example

  • 1.1.1
  • 1.1.2
  • 2.1.1
  • 2.1.2

Views Natural Sort might work for this but I can only get it working on titles.

4

You can do this with hook_views_query_alter(). I had a similar requirement few time ago where i have to sort nodes where title those have numbers should display before the nodes those have alphabets. Here is a snippet i created at dropbucket.rog

http://dropbucket.org/node/1484

Steps:

  • Add one new text field to the content type you using for files. use textfield because integer or float type field will not allow you value in format 1.1.1.
  • Create a view.
  • Add a sort criteria the field you added either desc or asc. because you are going to alter this in hook_views_query_alter().

I tried with textfield name "sort". Change the field name whatever you use in content type, in query alter. You can get field name in view query display if query display is enabled in views settings.

Create a custom module.

function custom_views_query_alter(&$view, &$query) {
  if($view->name == 'your_view_name'){
      // Remove all the sort order fields.
      $query->orderby = array();
      // Show number before the alphabets in titles.
      // This is trick which add 0 to the field to cast it to number then check the not equal to condition.
      $query->orderby['0']['field'] = 'field_data_field_sort_field_sort_value+0<>0';
      $query->orderby['0']['direction'] = 'DESC';
      $query->orderby['1']['field'] = 'field_data_field_sort_field_sort_value';
      $query->orderby['1']['direction'] = 'ASC';
  }
}

view custom natural sort

  • Amazing thank you so much for the in detail answer – sasdev Jul 29 '14 at 7:50

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.