Hi,

I installed views 6.x-2.0-beta4 and I am able to create views. But when I tried to insert view into my page, I keep getting this error:
Fatal error: Call to undefined function views_build_view() in /home/viacitc/public_html/includes/common.inc(1537) : eval()'d code on line 14

Here is my code:

$view_name = 'comments_recent'; //name of view
$limit = 3; // number of returns
$view_args = array();
$view = views_get_view($view_name);
views_build_view('embed', $view, $view_args, false, 15); 

Could somebody please help?

Comments

merlinofchaos’s picture

views_build_view doesn't exist in Views 2.

Try

views_embed_view($view_name, 'default');

instead.

chenys_jason’s picture

it works. Thanks!

merlinofchaos’s picture

Status: Active » Fixed
Anonymous’s picture

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.

doublejosh’s picture

Hi. Doing my first view embeds in 6 and moving from views_build_view() which I used in 5.

When using views_embed_view() I'm curious about passing arguments and such things within a display_id

 $view_name= 'my_view';
 $view_args= array(0=>$nid); // ??? this doesn't work ???
 $view_display_id= 'views-page_1';
 print views_embed_view($view_name, $view_display_id, $view_args);

For now I'm using this (because it works)...

  $view_name= 'my_view';
  $view_display_id= 'views-page_1';
  $view_args = array($nid);
  $func_args = array_merge(array($view_name, $view_display_id), $view_args);
  print call_user_func_array('views_embed_view', $func_args);

...but I have a sneaking suspicion it's not the best practices solution.

Thanks Mr. Chaos ;)

merlinofchaos’s picture

No, it expects the arguments as a series of arguments, not an array of them.

If you need to do it that way, just break the function down and do what views_embed_view() does. views_embed_view is not a complex function.

tchouk’s picture

Hi,

I am using the following code in my .module document under the form that displays my content but it doesn't give me the required view:

$view_name = 'Chanel'; //name of view
$limit = 3; // number of returns
$view_args = array();
$view = views_get_view($view_name);
views_embed_view($view_name,'default');

Should I add a permission or something like that? Or add 'print' before 'views_embed_view'?

Thanks for replying...

Ghostthinker’s picture

Hi,

You can also do it this ways (more like d5):

  $viewname = 'view_name';
  $display_id = 'page_1'; // or any other display
  $args = array();
  $args [0] = 'foor';
  $args [1] = 'bar';
  
  $view = views_get_view($viewname);
  $view->set_display($display_id);
  $view->set_arguments($args);
  print $view->preview();

Have a llok at the contrib api http://drupalcontrib.org/api/function/views_embed_view

doublejosh’s picture

This is an ancient thread, but looks like this ability was potentially removed.

My use case is to take advantage of the views UI and configurations, etc. in order to create a list things, but actually call upon that view in PHP code to do the output via another means. I used to use views_build_view() to accomplish grabbing the content of a view and then doing other things with it. It's nice to leave the query building to views and just made use of the results.

Since $view->preview returns HTML rather than an array of item, is there a way to do this in D6 and/or D7?

dawehner’s picture

There is views_get_result_view if you need the actualy output of a view.

Everything else can be done with the preview() function and more stuff.