Nodereference does not work with views and arguments

j0k3z - February 24, 2007 - 08:05
Project:Content Construction Kit (CCK)
Version:5.x-1.3
Component:nodereference.module
Category:support request
Priority:critical
Assigned:Unassigned
Status:active (needs more info)
Description

I cannot get node reference to work with views when using arguments.

Im trying to make a view that returns all nodes that reference another node. I want it to accept an argument to which nodes I want returned but it doesnt work.

I think it may be because no reference is printing out HTML links in reference to nodes but my arguments are plain text, so perhaps they dont match.

Any idea how to fix this?

#1

j0k3z - February 24, 2007 - 10:23
Status:active» closed

not a bug afterall, sorry.

I needed to add a single line of code to the argument box within views that sets arg(0) to arg(1) to grab my argument.

#2

yched - February 24, 2007 - 16:28

that's exactly it :-)

#3

j0k3z - March 5, 2007 - 09:29
Status:closed» active

Im having issues again and its more then just adding a line to the argument box.

I have a node type called 'Country Information' where my users add information about a country. This is a very simple content type that contains a title, a body, and a node reference field to select a Country (this is a node type) that this information is related to.

Now, on the 'Country' content type template I print a table view that shows all information about this country. I do this passing the node_reference as the argument. I use the arg(0) = arg(1) code to do this correctly.

THE PROBLEM IS I also want to add this same views table on the 'country info' page itself to show 'other info about this country' but it will not work.

I cant figure out why it will work on the country page but not on the info page iteself. I am passing the node_reference as arguments the same way but it just doesnt work.

Can someone please let me know why this is not working.

#4

yched - March 10, 2007 - 12:56
Category:bug report» support request
Status:active» fixed

When you're on the page of the "Country", the argument for the view should be the nid of the current node (select all "Country info" nodes that refer to this "Country")

When you're on the page of a "Country Info", the argument should be the value of the nodereference field
(select all "Country info" nodes that refer to the same "Country" as the current "Country Info")

If you want to use the same View for both cases, you thus have to add some logic to your "argument php code"

Something like (pseudo-code, you'll write the actual stuff, but that's the idea...) :

if ($node->type == 'country") {
  $args = array($node->nid);
}
elseif ($node->type == "country_info' && isset($node->FIELD_COUNTRY_REF['nid'])) {
  $args = array($node->FIELD_COUNTRY_REF['nid']);
}

where FIELD_COUNTRY_REF is the name of your field.

And that is Views support, actually.

#5

j0k3z - March 14, 2007 - 03:29
Status:fixed» active (needs more info)

Thank you for your reply.

I understand that the arguments will be different on this page.

It works just fine on my city info page using this code:
[code]$current_view->args[0]=$node->field_city[0]['value'];[/code]

But this is the code on my country info page that doesnt work:
[code]$current_view->args[0]=$node->field_which_country[0]['view'];[/code]

The only difference between these 2 content types is that the city info page is pulling in the city name from a plain text field on the page where as on the country page it needs to pull the country in from a node reference field. This is where the problem comes in. I am unable to pull info from the node reference field using the code above.

Perhaps it has something to do with the code requesting [view] as aposed to [value] like the city page. But on node reference it dosent allow you to print the "value" like you would with a plain text field.

Is there a solution to this?

#6

j0k3z - March 14, 2007 - 03:39

Perhaps if you have 2 mins of free time you can give it a shot to see what I mean.

Create a new content type called country and add 3 random country names
Create another content type called country info with a node reference field for the country type.

Add a few country info pages that reference a country.

Try to print a view on the page that takes the country from the current page and passes it as an argument to find other info about the same country.

This is the code I use to embed a view into a page:

<?php
global $current_view;
$view_4 = views_get_view('Country_Info');
$current_view->args[0]=$node->field_which_country[0]['view'];
print
t("<br />");
print
'<h2>' . t('More Information About ' . $node-> field_which_country[0]['view']) . '</h2>';
print
views_build_view('embed', $view_4, $current_view->args, true, 10);
?>

#7

daveblack - April 28, 2008 - 13:08

I'm chasing the same thing... anyone have any ideas????

 
 

Drupal is a registered trademark of Dries Buytaert.