I have a CCK field witch reference to a node type "persons" with categories "customers, collaborators ...."

nodetype persons with some category terms (customers, collaborators ...)
nodetype offers with a reference field to persons
nodetype task with the same reference field to persons

I want the reference field when I am inserting nodes of type offers, show me titles of node type persons with category term customers and when I am inserting nodes of type task, show me only persons with category term collaborators

I tried some php code to default value of the reference field

$sql  = "SELECT n.nid, n.nid, FROM node n LEFT JOIN term_node t ON n.nid = t.nid WHERE n.status = 1 AND n.type = 'persons' AND t.tid = 1 ORDER BY n.changed DESC";

$results = db_result(db_query($sql));
return array(array('nid' => $results));

but i get the error

* Customer : This post can't be referenced.
* The default value php code created Array ( [0] => Array ( [nid] => 7 ) ) which is invalid.

What am I doing wrong?

Thanks to anyone who can help

Comments

eoneillPPH’s picture

Your SQL had an error? Or just a typo here, I hope: "SELECT n.nid, n.nid, FROM node n"... that couldn't work (it selects same field twice, and with comma before the "FROM"?)

Make sure $results is only one value, as should be returned by db_result.

And once you're sure it's only one value, use the format they show in the help area:

  return array(0 => array('nid' => $results)); 

If your node reference field allows multiple values, you'd have to loop through the results with db_fetch_array, building the array to return:

$results = ...
while ($row = db_fetch_array($results)) {
  $myarray[] = array('nid'=>$row['nid']);
}
return $myarray;

(that should do it... haven't tested as such)

It (CCK) will show errors also if there is no value, so test count($myarray) and if it's 0, do:

else return array(0 => array('nid'));

(found that little tidbit here: http://drupal.org/node/233378 )

socialnicheguru’s picture

How do i return default of multiple values

I have a noderefernece which is unlimited
I want to have the default value filled with all the node reference values for that particular field.

I thought I could create the array of values and just return it like return ($array_of_values); but that does not seem to work.

Any ideas?

http://SocialNicheGuru.com
Delivering inSITE(TM), we empower you to deliver the right product and the right message to the right NICHE at the right time across all product, marketing, and sales channels.