Embed View :: How the heck do we embed views in nodes in D6?

pegleglax - April 15, 2008 - 03:15
Project:Views
Version:6.x-2.0-rc3
Component:Miscellaneous
Category:support request
Priority:normal
Assigned:Unassigned
Status:closed
Description

Hello everyone,

I'm can't wait for Panels, Views, and CCK to be finished in D6. Until then, I need a way to manually embed views within nodes using the PHP filter option on nodes in Drupal 6. Anyone know how to do that?

Please do not refer me to these pages:
http://drupal.org/node/47417
http://drupal.org/node/48816

I get undefined function calls: Fatal error: Call to undefined function views_build_view()

THANKS! LOVE THE COMMUNITY!

#1

merlinofchaos - April 15, 2008 - 05:00

Try this:

<?php
$view
= views_get_view('viewname');
print
$view->execute_display('default', $args);
?>

#2

merlinofchaos - April 15, 2008 - 05:00
Status:active» fixed

#3

pegleglax - April 15, 2008 - 13:47

merlin I absolutely love you! thanks! and GREAT work on views!!! The UI has made leaps and bounds!

#4

pegleglax - April 15, 2008 - 14:08
Status:fixed» active (needs more info)

merlin, check this out: (its my testing site)

pegleglax.homeip.net/sertoma

see the error views creates?

#5

merlinofchaos - April 15, 2008 - 14:55

Be sure the $args you pass is an array. I think you can just leave the argument off if you don't have args at all.

#6

pegleglax - April 15, 2008 - 15:35
Status:active (needs more info)» fixed

works!!!! Thanks!

<?php
$view
= views_get_view('viewname');
print
$view->execute_display('default');
?>

#7

Anonymous (not verified) - April 29, 2008 - 15:43
Status:fixed» closed

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

#8

vj0914 - May 9, 2008 - 18:34

what should i do if I want to embed 2 views on same page?

#9

vj0914 - May 9, 2008 - 18:51

I figured out, Thanks

#10

Matthew Davidson - August 12, 2008 - 03:53

And just to elaborate, you can change 'default' to whatever display id you want ('page_1', 'block_1', 'block_2'), eg.:

<?php
  $view
= views_get_view('viewname');
 
$display = $view->execute_display('block_1');
  print
$display['content'];
?>

This is necessary if you want your embedded view to pick up any of your customised theming.

#11

deviantintegral - August 17, 2008 - 22:23
Version:6.x-2.0-alpha5» 6.x-2.0-rc1
Status:closed» active

Has anyone else had issues with node titles being changed when you call execute_display? I'm calling it at the end of my node.tpl.php, and it seems to somehow be overriding the node's title with it's own, even though the title field should have all ready been printed. It's like the call to execute_display is getting the entire theme engine reset and redone. I'll keep on it, but I was hoping somehow that this isn't an issue with views and is just my site :)

Thanks,
--Andrew

#12

Matthew Davidson - August 18, 2008 - 00:03
Status:active» closed

You should create a new issue for this. It's a different question to the one in the parent post, which has been resolved. Be nice to merlin.

#13

merlinofchaos - August 18, 2008 - 01:58

Never ever ever use execute_display() to embed a view. Always use preview(). See views_embed_view() for the canonical example.

view::execute_display() should only be used where the display handler was intended to be used. i.e, a 'page' display should only use execute_display via a proper page callback, and a 'block' display should only use execute_display via hook_block.

#14

deviantintegral - August 18, 2008 - 16:41

Thank you - calling views_embed_view() is working perfectly!

For reference, my snippet:

<?php
$url
= explode('/', trim(request_uri(), '/'));
$url[0]= str_replace( '?q=', '', $url[0]);

$args = array($url[1]);
print
views_embed_view('nodequeue_1', 'page', $url[1]);
?>

--Andrew

#15

merlinofchaos - August 19, 2008 - 22:43

I think you'll find that the first part of all that can be eliminated by using the arg() function.

arg(1) perhaps.

#16

deviantintegral - August 20, 2008 - 01:09

Yeah, I used to hope so too Merlin :). The problem is that I'm grabbing the argument from a URL alias set with the pathauto. When you call arg(), you get the internal system path of the node you're looking at, and last I looked there was no Drupal API to easily get the unchanged path to the current page. Though it's probably better to call drupal_get_path_alias() with $_GET['q'], and then you avoid the removal of ?q=.

--Andrew

#17

merlinofchaos - August 20, 2008 - 01:37

actually no, you're right the first time. If you call drupal_get_path_alias() you'll do a database lookup and if it's a URL with multiple aliases you will always get the same one regardless of which alias was actually used.

#18

Archie Bunker - September 23, 2008 - 07:48

I'd like to hook a filter into this view in my node-games.tpl.php file as I can't figure out how to do this with argument handling directly in the views 2 admin tool. Can I do something like this? Can't seem to get it to work. So close.....Yet so far.

<?php
$sql
= "SELECT field_hometeam_nid FROM {content_type_games} WHERE nid = %d";
$myhometeam = db_result(db_query($sql, arg(1)));
$view = views_get_view('schedules_home');
function
yourmodule_views_query_alter(&$view, &$query) {$query->where[0]['clauses'][3] = "node_data_field_hometeam.field_hometeam_nid = '$myhometeam'";}
print
$view->execute_display('Block');
?>

If not, where can I place this function so that this works? FYI - Basically what this is saying is "Take a [game content type] node and find all the games for the home team [home team is a node reference field to a [team content type] node that references the home team for the game. Then I am trying to render this view in a block within the game node [Displays the home team's season schedule]

Thanks for any help.

Tim

#19

gausarts - September 27, 2008 - 21:39
Version:6.x-2.0-rc1» 6.x-2.0-rc3
Assigned to:pegleglax» Anonymous

Sorry if this is closed. But it's the nearest issue I have. I just don't want to hijack nor duplicate, only extend the question.

I managed to display views of the current user using this:
views_embed_view('status', 'block_1', $uid)

But I want to display the limit in that line as well, say to 1 or 3 nodes, while at the same I can have the same block with unlimited nodes display.

After reading the function views_embed_view in the module, and searching all around the world, I give up now, really.

I tried views_embed_view('status', 'block_1', $uid, 1) and views_embed_view('status', 'block_1', $uid, $limit = 1) and several blind break downs to no luck.

Please tell me if this is possible with that function to add the limit. If not how do I make it?

In the meantime, I have two options:
1. Two different blocks with each own Items to display.
2. Two different views with different Items to display.

But I considered both tedious. Any easier way?
Thanks.

#20

deviantintegral - September 28, 2008 - 13:25

You should just be able to do a theme override for your view. I haven't pulled up code, but I'm sure that you can do something like sizeof($view->rows) or whatever contains all of the nodes to theme. It shouldn't have anything to with embedding specifically.

--Andrew

#21

mooffie - September 28, 2008 - 13:58

In the meantime, I have two options:
1. Two different blocks with each own Items to display.
[...]
But I considered both tedious.

What's tedious in that? Just add another "Block" display to your view, and override its "item per page" option. Nothing tedious. Then pass 'block_2', or whatever, to views_embed_view().

I managed to display views of the current user using this:
views_embed_view('status', 'block_1', $uid)

But I want to display the limit

You could try:

$view = views_get_view('status') or die('No "status" view.');
$view->set_display('block_1');
$view->display_handler->set_option('items_per_page', 3);
print $view->preview('block_1', array($uid));

I haven't tried this, though. Expect typos. The "tedious" way would be the better choice for non-programmers.

#22

gausarts - September 28, 2008 - 14:19

You are right. Thanks for the light.

 
 

Drupal is a registered trademark of Dries Buytaert.