Is it possible to use the argument field in embedded views or blocks? I want to embed some views based on the og_nid field from the og_views module, but I don't know how I can define the argument if I'm not using a page, but embedding the view within another page.

Comments

yched’s picture

If you embed your view using something like :

$view = views_get_view("my_view_name");
$output = views_build_view('embed', $view, $view_args, $view->use_pager, $view->nodes_per_page);

you can set the $view_arg parameter manually just before that, e.g :

$view_args = array('1st_arg', '2nd_arg', ...);

Just make sure you provide the arguments in the same order as in the view definition

jasonwhat’s picture

thanks for the tip. I tried this code but am not getting the same view as when I use the url. The only arg involved is the OG_nid which is just the node id of the organic group.

<?php
$view_args = array('557');
$view = views_get_view("groupnews");
$output = views_build_view('embed', $view, $view_args, $view->use_pager, $view->nodes_per_page);
?>

Do you see anything wrong in this? 557 is the ID and "groupnews" is the name of the view.

yched’s picture

I don't really know what can be wrong...

I am not getting the same view as when I use the url

Can you be more specific about that ?

jasonwhat’s picture

If I use a url and go to a page using arg. 557 in the url it works fine. If I try to get the same output using the code and embedding it in a page it just comes up blank.

yched’s picture

blank ?

Sorry if that sounds offending, but are you sure you are really outputting the content of $output ? :-)

Try setting the "default" behaviour for your argument as "Display all values", and run your code with an empty $view_args array. If you're still getting a blank page, I'd say the output doesn't make it to the page, since your view obviously is not empty...

yched’s picture

You could also try to empty drupal's cache table.
That's often a "magical" solution when something that should work doesn't...

jasonwhat’s picture

doesn't my code need some sort of "print output" after the last line that I have now?

jasonwhat’s picture

Yep, the finish code looks like this:

<?php
$view_args = array('557');
$view = views_get_view("groupnews");
$output = views_build_view('embed', $view, $view_args, $view->use_pager, $view->nodes_per_page);
print $output;
?>

I just forgot print output. If this example is correct this should definately go into the handbook.

yched’s picture

Status: Active » Fixed

I guess your code is a snippet you enter in a "php" textarea in a 'page' node ?

You should try return $output; instead of print $output;

It should work, and it's cleaner (probably a matter of taste, though :-) )

merlinofchaos’s picture

Thanks, yched, for supporting this!

jasonwhat’s picture

thanks for the help. I see a lot of uses for this. By using Drupals arg functions a user can pull arguments out of standard drupal urls (like taxonomy/term/3 or node/51) and pass them to a view. I could see this being a good way to layout things like taxonomy pages with separate node types in separate columns on the page, or in my case on an organic group homepage.

merlinofchaos’s picture

There is a field, the PHP args field I think, that you can put PHP code into and actually directly modify the args of the view. WHen the API docs get updated, an example of this will be included.

yched’s picture

In fact, that "Arguments handling code" works even with "block enabled" views - meaning that arguments are parsed from the url not only when you are on the url of a "page" view, but also when rendering any views-exported blocks (am I being confuse ?)

I'm not 100% sure though whether that's a real "feature" you can rely on, or a permissive behaviour that will get fixed some day.

Anyway, views.module is sooo rich and surprising, that's sometimes dizzying :-)

merlinofchaos’s picture

No, block views don't receive any arguments; unless the php arg snippet does the parsing for you.

Anonymous’s picture

Status: Fixed » Closed (fixed)