7

In Drupal 6 a view can be embedded into a node as follows:

$viewName = 'LatestNews';
print views_embed_view($viewName);

This does not work for drupal 7. What is the function to embed views in drupal 7?

1
  • According to this documentation link of views module this function is equally available on Drupal 6 and 7. Try with second param as $display_id of your view. Oct 25 '11 at 22:41
9

In Drupal 7 you can use this code.

  • For a view page

    print views_embed_view('name_of_view','page_1', $node->nid);
    
  • For a view block

    print views_embed_view('faculty_core','block_1', $node->nid);
    
1
5

The EVA module can also attach views to nodes.

0
5

To display a view in a node template, you use the following code.

// Load the view. 
$view = views_get_view('machine_name_of_view');
// Set the active display.
$view->set_display('your_view_display_name');
// Set any needed argument.
$view->set_arguments(array(arg(0), arg(1)));
// Execute the view.
$view->execute();
// Render the view.
print $view->render();
2
  • 1
    You should always call $view->pre_execute(); before $view->execute();
    – Teegan
    Apr 4 '14 at 7:47
  • @Teegan - Didn't know that, using only execute seems to work, but I will look into using pre_execute() prior to execute(). Apr 4 '14 at 7:55
4

Why not:

print views_embed_view($view_machine_name, $view_display_name, $arg1, $arg2);

where $arg, $arg2, etc are optional.

Or use Viewfield module to embed a view as a field.

3
  • VIEWFIELD!!!! OMG!!! I have looked for days for this functionality, trying many modules, this saves me! :D thanks!
    – DrCord
    Nov 1 '13 at 21:33
  • @DrCord, also see Eva and views_field_view modules, they're pretty much the same thing. Nov 10 '13 at 15:10
  • I couldn't get EVA to do what I wanted.
    – DrCord
    Nov 12 '13 at 0:49
1

One solution is to use Panelizer to override the content of the respective node.

Lets say for example you have a handfull of one-off pages that need a view attached.

1) You could use the Page content type or maybe create a new content type that will specifically be panelized.

2) Enable Panelizer for that content type, make sure to set it up to "Allow per-record display choice" that way you can setup multiple panelizer displays that will be tailored to each one off page. (each panelizer display can have it's own set of views attached)

3) From here you simply create a view (I like to use Content Panes for this) and add it to the Panelizer content display of choice.

This allows you to re-use a single content type that doesn't really need it's own set of fields, but might potentially be used for either referencing other entities (as a field) or referencing views.

Throw in the Paragraphs module and you have a Swiss Army Knife content type. Speaking of, that would make a perfect name for the custom content type used for this purpose... Hmm....

0

Let's just mention that first parameter is view name, second display name and after that you can pass contextual filter parameters.

So that "$node-nid", the third parameter depends on you view...what it expects for first contextual filter - id doesn't have to be node id. Of course, you are not limited to one parameter - pass as many as you have in view...or pass none if your view doesn't have contextual filters.

0

A very configurable Drupal 7 and 8 module is views_field_formatter which allows you to embed a view as a fields formatter

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.