After hours of reading code, docs and googling around i came to the conclusion to come here and beg for help.
Im trying to make a custom sort handler (Something like a random sort per day). I'm proud to have already figured out a few things, but it seems I'm missing something. I get the following error when trying to apply the handler:
Error: handler for mecom > random doesn't exist!

Here are the pieces I've wrote so far (For the sake of easyness, I'll ommit what's not important to this issue:

file: mecom.module

function mecom_views_api() {
  return array(
    'api' => 2,
    'path' => drupal_get_path('module', 'mecom'),
  );
}

file: mecom.views.inc

function mecom_views_data()
{
  $data['mecom']['table']['group'] = t('Mecom');
  $data['mecom']['table']['join'] = array(
    '#global' => array(),
  );

  $data['mecom']['random'] = array(
    'title' => t('Random daily'),
    'help' => t('Daily randomize the display order.'),
    'sort' => array(
      'handler' => 'views_handler_sort_random_daily',
      ),
  );
  return $data;
}

function mecom_views_handlers()
{
  return array(
    'info' => array(
      'path' => drupal_get_path('module', 'views'),
    ),
    'handlers' => array(
    'views_handler_sort_random_daily' => array(
      'parent' => 'views_handler_filter_numeric',
       ),
    ),
  );
}

file: views_handler_sort_random_daily.inc

class views_handler_sort_random_daily extends views_handler_sort {
  function query() {
    global $db_type;
    switch ($db_type) {
      case 'mysql':
      case 'mysqli':
        $t=getdate();
        $randseed = mktime(0, 0, 0, $t[mday], $t[mon], $t[year]);
        $formula = 'RAND('.$randseed.')';
        break;
    }
    if (!empty($formula)) {
      $this->query->add_orderby(NULL, $formula, $this->options['order'], '_' . $this->field);
    }
  }

  function options_form(&$form, &$form_state) {
    parent::options_form($form, $form_state);
    $form['order']['#access'] = FALSE;
  }
}

I really would be happy if somone can help me get this working.

Regards

Btw: I'm using Drupal 6.8 and Views 6.x-2.2

Comments

merlinofchaos’s picture

    'info' => array(
      'path' => drupal_get_path('module', 'views'),
    ),

That's telling Views to look for your handlers in the Views module directory. I suspect that won't work. I think you can just leave that out and it'll look in your module's directory.

merlinofchaos’s picture

FYI otherwise I think you're doing pretty well with this.

Nexotap’s picture

If i ommit that part I'm getting following error:

Fatal error: Class 'views_handler_sort' not found in .........\sites\all\modules\mecom\views_handler_sort_random_daily.inc on line 7

merlinofchaos’s picture

Ah! That's because the parent is wrong:

    'views_handler_sort_random_daily' => array(
      'parent' => 'views_handler_filter_numeric',
       ),
class views_handler_sort_random_daily extends views_handler_sort {

Those need to match. =)

Nexotap’s picture

Status: Active » Closed (fixed)

That was it! Works like a charm.
Many thanks :)

JimNastic’s picture

Hi Nexotap,

Can you share the rest of the code for this. I'd like to do exactly the same thing

Cheers,
jim