Hi everyone,
I am trying to do exactly what was mentioned in this request. #614364: View block using results from a Page view. I'm just looking for some help from you guys!
I have a page view (called product_features) which will display a list of features of a product by taxonomy term. Specifically, the page view returns all feature nodes of term X out of possible terms X or Y or Z which reference product node A out of possible nodes A or B or C. No problems, it works.
Where it gets tricky is I am trying to put a block view on the side to show the other terms available on Product A (basically which show X Y and Z).
Now, you can't pass a variable onto a block view; you have to give it a default argument. The argument I want to pass it is not the Node ID from URL, it's the node ID passed to the page view. Therefore I need some PHP code.
Unfortunately, this is where my knowledge of of the $views object falls short. Here's the argument I have. I know it's not correct and I was looking for some help.
I think arg(1) should be the nid which is the basis for the page view and arg(2) is the taxonomy term, since I provide the arguments in that order. I really have no idea.
$views_page = views_get_page_view();
if ($views_page->name == 'product_features') {
$result = $views_page->result;
$product_node = $result->arg(1);
if (isnumeric($product_node)) {
return $product_node;
} else {
return false;
}
}
I've tested the block view on the Product Node page and tell it to take the Node ID from the URL and it works. If I provide the node ID as a default argument, it works. Once I can abstract the provided node ID from the view, I know it will work fine.
I can't display the entire vocabulary because there are some terms which do not apply to product A, so I don't want to display them. I need to give it a node ID to look through.
Also, I want to say that I really did make an effort to try to find the contents of $view->result, but no luck. I really appreciate the help.
Comments
Comment #1
nagiek commentedComment #2
merlinofchaos commentedYou're starting along the right path, but then stumble pretty seriously when you get to the arg(1) part. That is a function which works on $_GET and doesn't work on an arbitrary array.
If you want the actual results of the view, what you probably want is $result[0]->nid which should get you the nid on the first record retrieved (assuming there's only one node on the page).
It doesn't sound like you really want that, though. It is entirely possible that you just want to skip getting the view entirely and use arg(1) or arg(2) or something along those lines. You might get the page_view and confirm its name just to make sure your block only shows upon the right page, though.
Comment #3
nagiek commentedmerlinofchaos,
Beautiful, works great. Thanks for the help!
You're right in that I didn't actually want to look through the results though, as this data has already been supplied to the page view. arg(2) was the way to go. I didn't really understand the $result structure.
Here's the final PHP request for everyone.
I kept out any validation code for arg(2) since the validation occurs after.
Key point in the troubleshooting was in the code I posted before, where I had
$views_page->name == 'product_features'instead of$views_page->view->name == 'product_features'.As an aside, $result[0]->field_product_nid would have given me the proper node ID, as I was looking for the node ID of the referenced node, not the node ID of the returned nodes. This shouldn't be used due to problems where no nodes are returned.