tags:

views:

132

answers:

1

I've got the following:

function view_sorter_views_pre_view(&$view) {  // don't need $items

   if ($view->name == 'MOST_RECENT') {

        $insert = array();
        $insert[order] = 'DESC'; //SORT ORDER
        $insert[id] = 'title';
        $insert[table] = 'node';
        $insert[field] = 'title';
        $insert[override] = array();
        $insert[override][button] = 'Override';
        $insert[relationship] = 'none';

        unset ($view->display['default']->display_options['sorts']['title']);
        $view->display['default']->display_options['sorts']['title'] = $insert;

   }

} 

Basically, I'm just changing the sort order... but this does not appear on the view when opening it. Any idea why?

+1  A: 

I believe what you want is

/**
 * Implementation of hook_views_pre_view().
 */
function view_sorter_views_pre_view(&$view) {
  if ($view->name == 'MOST_RECENT') {
    $view->display['default']->handler->options['sorts']['title']['order'] = 'DESC';
  }
}

Views uses the handler object to build the query instead of the display_options. The display_options contain all the options for every display type that the view contains (eg. default, page_1, block_1, etc...). The 'handler' object holds the options that will be used to actually build the current display.

Note: I simplified the code to only change the sort order. The rest of your code should work, just change the last two lines to

unset($view->display['default']->handler->options['sorts']['title']);
$view->display['default']->handler->options['sorts']['title'] = $insert;
calebthorne