user824624
user824624

Reputation: 8080

in drupal 7, views_get_view('VIEWNAME') get a error?

I want to embed a view in the tpl.php, so I am using the views_get_view('VIEWNAME') function.

Here is what I am doing the tpl.php:

<?php
   $view = views_get_view('MapView');  
   print $view->preview('default');
?>

"MapView" is the view's name, I am not sure the argument in the views_get_view is the machine name or human readable name, in fact in my example is using the human readable name, because I can't find the machine name in the drupal view configuration.

Anyway, I get a error saying "Fatal error: Call to a member function preview() on a non-object", seems like I didn't fetch the view in the correct?

any idea?

Upvotes: 1

Views: 2848

Answers (2)

BasH
BasH

Reputation: 1

An old question but I was having the same issue and I couldn't find the right answer: you have to use the machinename and it is case sensitive! You can find it in the database in the views_view table in the "name" field.

Upvotes: 0

Sergey Litvinenko
Sergey Litvinenko

Reputation: 593

View id you can easly get from edit view url. Like any key it consists of undescores and lowercase. Also to output view in needed place try it:

$view = views_embed_view('view_id', $display_id = 'block');
print $view;

It worked for me for node content, i added view to the nodes of needed type in my module:

function YOUR_MODULE_node_view($node, $view_mode, $langcode) {
  if($node->type=="type"){
  $view = views_embed_view('view_id', $display_id = 'block');
  $node->content['myfield'] = array(
    '#markup' => '<h3>Title</h3>'.$view, 
    '#weight' => 1,
  );

  return $node;
  }
}

Upvotes: 4

Related Questions