Hello,

I am trying to figure out a way to render a certain view display in my module. Here is the code I am using but it doesn't work.

$view = views_get_view('view name');
print $view->render('display id');

This, however, executes for 30 seconds and then PHP stops saying that the execution limit has been reached. Anyone have a tip for me what I am doing wrong? FYI, I have views caching disabled.

Regards,
Khaled

Comments

KhaledBlah’s picture

I've managed to get my code working by changing it the way described below. I hope this helps someone else with the same problem and I would still be interested in comments from other developers!

$my_view_name = 'view name';
$my_display_name = 'display name';

$my_view = views_get_view($my_view_name);
if ( is_object($my_view) ) { 
  $my_view->set_display($my_display_name);
  $my_view->pre_execute();
  return $my_view->render($my_display_name);
}
Jsan2020’s picture

I am looking to do something similar, so what made this work for you? Do you need to have the pre_execute?

Your code works for me, but in addition to the results of the view, I am getting edit, export, and clone links.

Jsan2020’s picture

After looking around I see that you can disable these buttons via:
Administer -> Site Building -> Views -> Tools (Tab) -> Do not show hover links over views.

But I don't want to get rid of these all together, they are useful.

KhaledBlah’s picture

Yes, the pre_execute() call was needed indeed.

ashique12009’s picture

Hi, 

This is not working from my controller :(
Any suggestion?

Thanks

Raf’s picture

Why not use views_embed_view('view_name', 'display_id') ?

KhaledBlah’s picture

Good question, Raf. I haven't been programming very long with Views so I may have overlooked this function. I'll have a look at it. Thx for letting me know.

Jsan2020’s picture

I am also using views_embed_view now. It works great, thanks for the tip.

emilkarl’s picture

Is there some way to also include exposed filter values when rendering a view programmatically?

Something like but working? This does not work..is does render the view but the filters is not applied to the result...?!

$filters = array('nid' => 5,'title' => 'foo');

$view = views_get_view('product_result_exposed');
$view->set_exposed_input($filters); // Set exposed filters

$view->set_display('block_view');
$view->execute();
echo $view->render('block_view');
pratip.ghosh’s picture

I am having the same problem, I have tried rendering the view using views_embed_view and $view->preview function. But in both cases, the exposed filter did not work. It gets rendered but wont work for the first time. One thing I noticed is the, though the filters are getting populated, but the exposed filter form is not. It only comes once I navigate to the 2nd page using my AJAX pagination. Then it starts coming for all pages. Actually besides this exposed filter form, I have another form in that page. I am not sure if the problem is because of that.

Any help?

-- Pratip Ghosh

altrugon’s picture

-------------------------------------------------
take a look -> www.altrugon.com

deepak_mishra’s picture

<?php
/**
* @file
* Module file for student.
*/

/*
* Implement hook_help().
*/
function student_help($path, $arg){
switch($path) {
case 'admin/help#student':
return '

' . t('student') . '

';
}
}

/**
* Implement hook_menu().
*/
function student_menu() {

$items = array();

$items['student-details'] = array(
'title' => 'Student Details',
'page callback' => 'student_view',
'access callback' => TRUE,
);

return $items;
}

/**
* Menu callback function.
*/
function student_view() {

$blockObject = block_load('views', 'student-block');
$block_render_blocks = _block_render_blocks(array($blockObject));
$block_get_renderable_array = _block_get_renderable_array($block_render_blocks);
$output = drupal_render($block_get_renderable_array);
return $output;

}

sodu.parsiev’s picture

  $view = views_get_view('sample_view');
  $view->set_display('some_display');
  $view->set_exposed_input((array) $_GET);
  $view->pre_execute(array('some argument'));
  $view->execute();
  $result = $view->result;