开发者

Programmatically handling a view in Drupal

I'm using custom module to handle this view. I did many try nothing works and also sorting too. Only arguments code works charm. Guide me to right way / help me with solutions.

and here is the exported view link 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_int开发者_JS百科ernationalcode_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;
           }


If you want an array with the returned data from a view, you can use views_get_view_result() function. enter link description here. But if i were you, i would make a direct query to the database instead using views, is much faster.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜