2010-09-09 51 views
2

我正在使用自定義模塊來處理此視圖。我做了很多嘗試沒有作品,也排序。只有參數代碼的魅力。引導我以正確的方式/幫助我解決方案。以編程方式處理Drupal中的視圖

,這裏是導出的視圖鏈接http://pastebin.com/YzzzLEbW

function custom_module(){ 
     views_include("view"); 
     $view = new view(); 
     $display = 'default'; 
     $display_id = 'page_1'; 
     $view = views_get_view('master'); 

     // ensure view exists 
     if (!$view) 
     print("Not real"); 
     // set display_id 
     $view->set_display('page'); 
     $view->set_use_ajax(true); 
     $view->set_use_pager('mini'); 
     $view->pager['items_per_page'] = 5; 
       $handler->override_option('path', 'ajaxlisting'); 
       $view->is_cacheable = FALSE; 

     //Try 1 for filtering 
     $view->init_handlers(); 
     $handler = $view->new_display('default', 'Defaults', 'default'); 

     $handler->override_option('use_pager', '1'); 

     $new_view_filters = $view->display_handler->get_option('filters'); 
     if (empty($new_view_filters)) { 
      $new_view_filters = array(); 
      } 
       $new_view_filters['field_internationalcode_value'] = array(
       'id' => 'field_internationalcode_value', 
      'table' => 'node_data_field_internationalcode', 
       'field' => 'field_internationalcode_value', 
       'operator' => 'or', 
      'value' => 'US', 
       'group' => '0', 
      'exposed' => FALSE, 
       'relationship' => 'none', 
       ); 
     $view_overrides = array(); 
     $view_overrides['filters'] = $new_view_filters; 
      foreach ($view_overrides as $option => $definition) { 
       $view->display_handler->override_option($option, $definition); 
      } 

     //Try 1 filter ends & Try 2 filter starts 


     $view->display_handler->options['handlers']['filters']['field_internationalcode_value']->value = 'US'; 

      //Try 2 filter ends & Try 1 sorting starts 

       $view->display['default']->handler- >options['sorts']['field_internationalcode_value']['order'] = 'DESC'; 

      //Try 1 sorting ends & try 2 sorting starts 

     $sort['field_volatility_value']['order'] = 'ASC'; 
     $view->display_handler->set_option('sorts', $sort); 

      // Try 2 sorting ends & Arguments Works good below 

     if (is_object($view)) { 
     if (is_array($args)) { 
      //$view->set_arguments($args); 
      $view->set_arguments(array($filt[0],$filt[1])); 
      $view->build($display_id); 
     } 

    // try 3 for filtering starts here 

     if (is_string($display_id)) { 
      $view->set_display($display_id); 
      $filter = $view->get_item($display_id, 'filter', 'field_internationalcode_value'); 
      $filter['value'] = 'US'; 
      $view->set_item($display_id, 'filter', 'field_internationalcode_value', $filter); 
     } else { 
      $view->init_display(); 
     } 
    // try 3 ends here 

     $view->pre_execute(); 
     $final = $view->execute($display_id); 
     $output = $view->render(); 
     // Clean up after processing 
     $output = $view->post_execute(); 
     } 


     return $view->result; 
      } 
+0

你究竟想要做什麼? 「處理」不夠具體。 – 2010-09-09 08:41:53

+0

即時通訊試圖從視圖中獲取數據數組來創建一個JavaScript嵌入小部件,它顯示了小部件,如果您將該腳本放入您的網站 – Nithish 2010-09-13 11:57:05

+1

我認爲馬克的意思是:你想做什麼* * * *視圖* ?如在,爲什麼你必須以編程方式做到這一點:你不能在視圖UI中配置它,然後只使用參數?我並不是說「你不應該這樣做」:)但是一旦更清楚你爲什麼以編程方式處理視圖,我們可能會提供幫助! – 2011-04-21 10:52:56

回答

0

如果你想從一個視圖返回的數據的數組,你可以使用views_get_view_result()函數。 enter link description here。但如果我是你,我會直接查詢數據庫,而不是使用視圖,速度要快得多。