I have a custom field declared and I am trying to make it sortable. The field holds date values already formatted ie: 06/10/12. How could I make this sortable? I thought this code would work but the header on the table doesn't become hyper linked even.

         function uc_order_views_data() {
          $data['uc_orders']['data'] = array(
            'group' => t('Order') . ':data',
            'title' => t('Ship Date'),
            'help' => t('The date to ship the order.'),
            'field' => array(
            'handler' =>'uc_order_handler_field_ship_date',
              'click sortable' => TRUE,
             ),
             'sort' => array(
               'handler' => 'views_handler_sort',
             ),
             'filter' => array(
               'handler' => 'views_handler_filter_string',
             ),
          );
         }

I do have the ability to send through a UNIX epoch timestamp instead if that would help me make this sortable so long as I can sort by the epoch but render it as a formatted date.
**The fact that this is an extension of ubercart bears no factor on the result. Treat this question as if it were just a simple implementation of the views module, which in essence it is.**

Comments

diggingrelic’s picture

I am putting a bounty on this issue, you can bid the amount you want to be paid to solve this issue at:
http://cofundos.org/project.php?id=115605

merlinofchaos’s picture

Status: Active » Fixed

The handler you use must implement the click_sort() method which is what actually implements the sorting. I don't know anything about the uc_order handlers. Typically the default click_sort() handler is all you need.

The default implementation is here:
http://api.drupal.org/api/views/handlers%21views_handler_field.inc/funct...

If that handler doesn't implement the method you'll need to extend that handler.

It's also possible that the uc handler overrides the default implementation of click_sortable:
http://api.drupal.org/api/views/handlers%21views_handler_field.inc/funct...

diggingrelic’s picture

Thank you for your comments merlinofchaos. I very much appreciate them. This should be enough information for me to figure this out.

diggingrelic’s picture

Well, I have it working, I didn't realize that 'click sortable' => TRUE just exposes the field to the UI sort radio button on the view settings. So, once I checked that it works as expected, almost.

  $data['uc_orders']['data'] = array(
    'group' => t('Order:data'),
    'title' => t('Ship Date'),
    'help' => t('The date to ship the order by so that it arrives when the customer wants.'),
    'field' => array(
      'handler' =>'uc_order_handler_field_ship_date',
      'click sortable' => TRUE,
     ),
     'sort' => array(
       'handler' => 'views_handler_sort_date',
     ),
     'filter' => array(
       'handler' => 'views_handler_filter_date',
     ),
  );

My handler extends views_handler_field_date

class uc_order_handler_field_ship_date extends views_handler_field_date {
  function render($values) {
    $response = "-";
    $data = unserialize($values->uc_orders_data);
    if (isset($data['fedex_calendar_ship_date'])) {
      $response = $data['fedex_calendar_ship_date'];
    }
    return $response;
  }
}

The data is not a time stamp and is already in Date format: 06/20/2012

It is not sorting properly. It is sorting like a string from left to right. How can I have it sort it as a date?

diggingrelic’s picture

Status: Fixed » Active
diggingrelic’s picture

Status: Active » Fixed

Okay, so I learned what the issue is with sorting a formatted date, it doesn't work unless you go YYYY-MM-DD. However, I decided to convert the data to unix time stamps and then use the standard views_handler_sort_date stuff. So, it is working.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.