Currently, there is no way to enable natural sorting for use with View's tables with clickable headers. This would be a great feature to round out full compatibility with the different sort options provided by Views.

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

esolano’s picture

Hi Poieo,

I was able to add the Natural Sort to Table Headers. This how I did it:

  1. Add both the "Content: Title" and the "Node: Title - Natural" fields to the view.
  2. Exclude "Content: Title" from the display
  3. Rewrite the result on the "Node: Title - Natural" field with the excluded "Content: Title" from above ( [title] )
  4. Edit the table settings, and make the "Node: Title - Natural" sortable

That did the trick for me. I hope this helps.

Regards,

socialform’s picture

Thanks ensolan, that worked great. For others, be aware that it doesnt work in the 2.0 versions because Node:Title - Natural has gone away. I had to go back to 7.x-1.4 to find this option. It would be great if the latest versions could accommodate this feature.

generalredneck’s picture

Assigned: Unassigned » generalredneck
Priority: Normal » Major

intriguing notion. Don't know why I thought that this would have already been taken care of with the normal sort handler. Let me put this on my radar as high priority. It does make sense... and I assume that you should be able to select whether you want the arrows on the table header to sort Naturally or normally in the interface some how.

generalredneck’s picture

FYI Poieo did mention this in https://www.drupal.org/node/2176035#comment-8440523

My bad for being a bad maintainer.

glottus’s picture

This is what I'm looking for as well (natural sorting of a text field that actually contains numerical data) from the clickable views table header. Unfortunately, I cannot seem to get v7.x-1.4 to work at all - installing the module provides admin menu entries to re-index and configure, but I don't see the expected checkboxes on fields or any references to natural sorting anywhere within the views interface.

socialform’s picture

Glottus, if i remember right it provides a different field... an alternative to the usual title field for instance. "Node: Title - Natural" for instance. Take a closer look.

sansui’s picture

Hrm - so it appears there's no way to add natural sort index to a text field, and then allow that text field to be a clickable table header in views with 2x?

generalredneck’s picture

Version: 7.x-2.0-alpha5 » 7.x-2.x-dev

yeah that is the case so far. bumping this to the dev branch. Patches are welcome if anyone wants to work through it. Thanks!

dunx’s picture

Looking for this too. Happy coding!

generalredneck’s picture

Some notes on what I've done so far and what I still need to do.

Work has started in 7.x-2.x-table-sort. I've created a new table style plugin that extends views_plugin_style_table. I've also used hook_views_plugins_alter to take over the default views table style plugin and replace it with Views Natural Sort's plugin. I don't know if this pattern will work in D8, but should be fine for D7... and we will cross that bridge later. With that said, I had to overload the Settings Form theme function and replace it with theme_views_natural_sort_style_plugin_table since they use a themeing function that has little to offer in the way of preprocessing... I've added the column "Sort Naturally" so that you will be able to choose if the field is to be sorted naturally asc/desc when clicked... since there are only 2 states.

Work that still needs to be done...

Add functionatlity that will check to see if a field can be natural sorted... and if It can, show a checkbox, kinda like the "Sortable" column in settings.
Add functionality that will actually do the sort based off the totally different "order" and "sort" query parameters provided by the table style plugin.

moonstorm’s picture

Hi,

I made a quick fix which should cover all table clickables with a simple patch. This is in no way robust and I don't think it's clean enough to be committed as a patch BUT can bring resolve to people struggling with the issue right now.

/**
 * Implements hook_views_query_alter().
 */
function MYMODULE_views_query_alter(&$view, &$query) {
  if ($view->plugin_name  === 'table') {
    if (isset($query->orderby[0])) {
      $field = $query->orderby[0]['field'];
      $direction = $query->orderby[0]['direction'];
      $field_parts = explode('.', $field);
      $field_id = substr($field_parts[0], 11);
      if (in_array($field_id, _views_natural_sort_get_field_names())) {
        $join = new views_join();
        $join->table = 'views_natural_sort';
        $join->field = 'eid';
        $join->left_table = 'node';
        $join->left_field = 'nid';
        $join->type = 'left';
        $query->add_relationship('node_views_natural_sort', $join, 'node');
        $query->orderby[0] = array(
          'field' => 'content',
          'direction' => $direction,
        );
      }
    }
  }
}

/**
 * Helper function to get field names.
 */
function _views_natural_sort_get_field_names() {
  $fields = array();
  foreach (views_natural_sort_text_field_get_configured_fields() as $field) {
    $fields[] = $field['field_name'];
  }

  return $fields;
}

pawel_r’s picture

#11 worked for me. Have on mind that #11 is designed for nodes, if you want to use it with any other entity type than alternations must be done.

There is also one thing missing: join should be done by field name

        $join->extra = array(
		array(
			'field' => 'field',
			'value' => $field_id,
		)
	);
lunk rat’s picture

Thanks to @moonstorm for the start in #11. Anyone here willing to roll a patch and close this issue? Table header click-sorting has become an expected UI pattern for many Drupal web apps, and a lot of D7 tables could likely benefit from natural sort clickable headers.

MustangGB made their first commit to this issue’s fork.

mustanggb’s picture

Status: Active » Needs review

Created a merge request for the 7.x-2.x-table-sort branch that adds the checkboxes to the views table format settings and does the actual natural sort for the case of node base fields, which is basically limits it to the node title.

https://git.drupalcode.org/project/views_natural_sort/-/merge_requests/6

Next things to do would be add the ability to natural sort field api fields, then add support for other field handlers like taxonomy and such if that's wanted.

Marking as Needs review for generalredneck's attention to hopefully get this committed to the test/wip branch.

mustanggb’s picture

@generalredneck Any chance of getting my merge request in to move this forward?

generalredneck’s picture

Yeah.

Sorry about being silent. I've not done a lot D7 wise for a while... but since views stuff usually translates D7 to D8,9,10 pretty well... this should be a good start. I think we do need to think of a way to extend this without instantiating hard classes for each entity plugin type... but that is totally a refactor situation. This works for a first draft.

If we can get some more eyes on it, that would be awesome.

generalredneck’s picture

Title: Enable Natural Sorts for Clickable Table Headers » Enable Natural Sorts for Clickable Table Headers (Drupal 7)
Status: Needs review » Needs work

For Drupal 7. I'm going to merge it AS IS for just nodes into the main branch and release it as 2.8. I did some testing and it looks good. Big shout out to you MustangGB. I'm renaming this issue and creating a follow up for the 8.x branch. yet going to move this one back to "needs work" to address the following

Next things to do would be add the ability to natural sort field api fields, then add support for other field handlers like taxonomy and such if that's wanted.

generalredneck’s picture

Status: Needs work » Closed (outdated)