You are here

function slickgrid_views_pre_view in Slickgrid 6

Same name and namespace in other branches
  1. 7.2 slickgrid.module \slickgrid_views_pre_view()
  2. 7 slickgrid.module \slickgrid_views_pre_view()

Implementation of hook_views_pre_view Slickgrid handles the paging internally so need to override

@author Ben Scott

Parameters

object $view :

Return value

void

File

./slickgrid.module, line 94

Code

function slickgrid_views_pre_view(&$view, $display_id) {
  global $conf;

  // TODO - Can this be moved into the views style plugin? See plugins.inc
  // Is this view a slickgrid view
  if ($view->display_handler
    ->get_option('style_plugin') == 'slickgrid') {

    // Really hacky, but ajax_load chokes on agrregated JS so I need to turn it off
    // TODO There must be a better way of working round this!
    $conf['preprocess_js'] = 0;
    $style_options = $view->display_handler
      ->get_option('style_options');
    if ($view->display_handler
      ->get_option('use_pager')) {

      // turn off views paging
      $view->display_handler
        ->set_option('use_pager', FALSE);

      // let slickgrid know we want to use it's own paging system by setting it as a view style option
      $style_options['pager'] = TRUE;
    }
    $view->display_handler
      ->set_option('style_options', $style_options);

    // If this is using a collapsible taxonomy field, we need to ensure the nodes are in the right order, and the parent TID is available
    // Much faster to do it now than in a preprocesser after the view data has been constructed
    if ($style_options['collapsible_taxonomy_field']) {

      // Define the parent relationship
      $parent_relationship = array(
        'label' => t('Slickgrid parent'),
        'required' => 0,
        'id' => 'parent',
        'table' => 'term_hierarchy',
        'field' => 'parent',
      );
      $view
        ->set_item($display_id, 'relationship', 'slickgrid_parent_relationship', $parent_relationship);

      // Ensure the tid of the parent term is available but hidden from the view
      $parent_tid_field = array(
        'exclude' => 1,
        'id' => 'slickgrid_parent_tid',
        'table' => 'term_data',
        'field' => 'tid',
        'relationship' => 'slickgrid_parent_relationship',
      );
      $view
        ->set_item($display_id, 'field', 'slickgrid_parent_tid', $parent_tid_field);

      // Ensure the tid of the actual term is available but hidden from the view
      $tid_field = array(
        'exclude' => 1,
        'id' => 'slickgrid_tid',
        'table' => 'term_data',
        'field' => 'tid',
      );
      $view
        ->set_item($display_id, 'field', 'slickgrid_tid', $tid_field);

      // The taxonomy sorts need to come first - so remove the exisitng sorts & re-add them after my own
      $existing_sorts = $view
        ->get_items('sort');

      // Unset all the existing sorts
      foreach (array_keys($existing_sorts) as $existing_sort_id) {
        $view
          ->set_item($display_id, 'sort', $existing_sort_id, null);
      }

      // Add two sorts to the view - one for the parent term, one for the child
      foreach (array(
        'slickgrid_parent_relationship',
        'none',
      ) as $relationship) {
        $id = 'slickgrid_sort_' . $relationship;
        $sort = array(
          'order' => 'ASC',
          'id' => $relationship,
          'table' => 'term_data',
          'field' => 'weight',
          'relationship' => $relationship,
        );
        $view
          ->set_item($display_id, 'sort', $id, $sort);
      }

      // Re add the original sorts
      // Unset all the existing sorts
      foreach ($existing_sorts as $existing_sort_id => $existing_sort) {
        $view
          ->set_item($display_id, 'sort', $existing_sort_id, $existing_sort);
      }
    }
  }
}