Skip to main content
did through a better way
Source Link
duru
  • 599
  • 7
  • 17

The below code combines two views based on message table which have different relationships. Same should apply on node table.

/**
 * Implements hook_views_pre_execute().
 */
function ext_message_views_pre_executeMY_MODULE_views_pre_execute(&$view) {
  if ($view->name == 'dis_streamer''VIEW_1' && $view->current_display == 'dis_streamer''DISPLAY_OF_VIEW_1') {
    
    $user_streamer$view2 = views_get_view('user_streamer''VIEW_2');
    $user_streamer$view2->build('user_streamer''DISPLAY_OF_VIEW_2');
    
    $view->build_info['query'] = db_select( //this is for cardinality errrors
      $view->build_info['query']
        ->fields('message''table_alias', array('timestamp'))
        ->range() //unset offset and limit to set them after the union
        ->union(
          $user_streamer$view2->build_info['query']
            ->fields('message''table_alias', array('timestamp'))
            ->range()
      ), 'message''table_alias'
    )
      ->fields('message''table_alias', array('mid', 'timestamp'))
      ->orderBy('timestamp', 'DESC') //you should order manually
      ->range(
        $view->query->pager->current_page * $view->query->limit, 
        $view->query->limit);
    
    $view->build_info['count_query'] = $view->build_info['query'];
  };
}

The evolution of above code has been like this:

/**
 * Implements hook_views_pre_execute().
 */
function MY_MODULE_views_pre_execute(&$view) {
  if ($view->name == 'VIEW_1' && $view->current_display == 'DISPLAY_OF_VIEW_1') {
    
    $view2 = views_get_view('VIEW_2');
    $view2->build('DISPLAY_OF_VIEW_2');

    $view->build_info['query']
      ->fields('table_alias', array('timestamp'))
      ->union(
          $view2->build_info['query']
            ->range()
            ->fields('table_alias', array('timestamp'))
            ->orderBy('timestamp', 'DESC')
      );
    
    $view->build_info['count_query']
      ->union(
          $view2->build_info['count_query']
          ->range()
      );
  };
}

The below code combines two views based on message table which have different relationships. Same should apply on node table.

/**
 * Implements hook_views_pre_execute().
 */
function ext_message_views_pre_execute(&$view) {
  if ($view->name == 'dis_streamer' && $view->current_display == 'dis_streamer') {
    
    $user_streamer = views_get_view('user_streamer');
    $user_streamer->build('user_streamer');
    
    $view->build_info['query'] = db_select( //this is for cardinality errrors
      $view->build_info['query']
        ->fields('message', array('timestamp'))
        ->range() //unset offset and limit to set them after the union
        ->union(
          $user_streamer->build_info['query']
            ->fields('message', array('timestamp'))
            ->range()
      ), 'message'
    )
      ->fields('message', array('mid', 'timestamp'))
      ->orderBy('timestamp', 'DESC') //you should order manually
      ->range(
        $view->query->pager->current_page * $view->query->limit, 
        $view->query->limit);
    
    $view->build_info['count_query'] = $view->build_info['query'];
  };
}

The below code combines two views based on message table which have different relationships. Same should apply on node table.

/**
 * Implements hook_views_pre_execute().
 */
function MY_MODULE_views_pre_execute(&$view) {
  if ($view->name == 'VIEW_1' && $view->current_display == 'DISPLAY_OF_VIEW_1') {
    
    $view2 = views_get_view('VIEW_2');
    $view2->build('DISPLAY_OF_VIEW_2');
    
    $view->build_info['query'] = db_select( //this is for cardinality errrors
      $view->build_info['query']
        ->fields('table_alias', array('timestamp'))
        ->range() //unset offset and limit to set them after the union
        ->union(
          $view2->build_info['query']
            ->fields('table_alias', array('timestamp'))
            ->range()
      ), 'table_alias'
    )
      ->fields('table_alias', array('mid', 'timestamp'))
      ->orderBy('timestamp', 'DESC') //you should order manually
      ->range(
        $view->query->pager->current_page * $view->query->limit, 
        $view->query->limit);
    
    $view->build_info['count_query'] = $view->build_info['query'];
  };
}

The evolution of above code has been like this:

/**
 * Implements hook_views_pre_execute().
 */
function MY_MODULE_views_pre_execute(&$view) {
  if ($view->name == 'VIEW_1' && $view->current_display == 'DISPLAY_OF_VIEW_1') {
    
    $view2 = views_get_view('VIEW_2');
    $view2->build('DISPLAY_OF_VIEW_2');

    $view->build_info['query']
      ->fields('table_alias', array('timestamp'))
      ->union(
          $view2->build_info['query']
            ->range()
            ->fields('table_alias', array('timestamp'))
            ->orderBy('timestamp', 'DESC')
      );
    
    $view->build_info['count_query']
      ->union(
          $view2->build_info['count_query']
          ->range()
      );
  };
}
Source Link
duru
  • 599
  • 7
  • 17

The below code combines two views based on message table which have different relationships. Same should apply on node table.

/**
 * Implements hook_views_pre_execute().
 */
function ext_message_views_pre_execute(&$view) {
  if ($view->name == 'dis_streamer' && $view->current_display == 'dis_streamer') {
    
    $user_streamer = views_get_view('user_streamer');
    $user_streamer->build('user_streamer');
    
    $view->build_info['query'] = db_select( //this is for cardinality errrors
      $view->build_info['query']
        ->fields('message', array('timestamp'))
        ->range() //unset offset and limit to set them after the union
        ->union(
          $user_streamer->build_info['query']
            ->fields('message', array('timestamp'))
            ->range()
      ), 'message'
    )
      ->fields('message', array('mid', 'timestamp'))
      ->orderBy('timestamp', 'DESC') //you should order manually
      ->range(
        $view->query->pager->current_page * $view->query->limit, 
        $view->query->limit);
    
    $view->build_info['count_query'] = $view->build_info['query'];
  };
}