4

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.

1 Answer 1

10

I finally figured out the answer. Only took me a week.

Under the view settings there is a "Format" option, and beneath that there is a "Settings" link. Upon clicking there are options to make your new fields sort-able or not to the user. Adding 'click sortable' => TRUE just exposes that radio button!

Good grief that was causing me some headaches! I was beating myself up on the code when it was a UI setting withing Drupal.

Something else to consider is the actual sorting. There are many different handlers for sorting fields. This particular field is a date. In order for this to work, the date needs to be stored as a UNIX time epoch. There are flags that I did not pass that can allow you to choose the granularity of the sort, (seconds, minutes, days, months, years).

This is what my code ended up looking like to make the date sort actually work as expected. When I stored the date as a UNIX time epoch.

/**
 * Implements hook_views_data().
 */
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_date',
     ),
     'filter' => array(
       'handler' => 'views_handler_filter_date',
     ),
  );
}

Now, lets take a look at the uc_order_handler_field_ship_date handler. You want to be sure that if you are not using the standard sort handler, that you extend the proper handler. If this date was not buried within some serialized data, I wouldn't even need this class and the default views handler would be sufficient.

class uc_order_handler_field_ship_date extends views_handler_field_date {
  /**
   * Overrides views_handler_field_date::render().
   */
  function render($values) {
    $data = unserialize($values->uc_orders_data);
    if (isset($data['fedex_calendar_ship_date'])) {
      $date = $data['fedex_calendar_ship_date'];
    }
    else {
      $date = "-";
    }
    return date('n/j/Y',$date);
  }
}

For more information on all of the sort handlers available see: http://api.drupal.org/api/views/handlers%21views_handler_sort.inc/class/views_handler_sort/7

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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