I had setup the content profile for the user of my site. And the user can upload their video in the video node with a CCK nodereference field reference to the content profile. Then the node might be edited by the Editor role.

My question is
1) How to assign node id of user's content profile automatically into the CCK nodereference field when create a node???

2) How to keep the nodereference field unchanged when Editor edit the node?

I can setup and assign a views to selected the content profile from the current log-in user and put it into the nodereference field under the Advanced - Nodes that can be referenced (View) section when node added. This solve the first question.

However, the above views will change the nodereference field with Editor's node id since the Editor is the current log-in user. So I stuck at the second question with the same views.

Was the view the correct direction to solve the above question? Or put a php code in the php code field at Default value section in the nodereference field?? Or the other solution?

Will anybody shed some light for me? Thanks in advance.

Comments

happydrupal’s picture

After some try and error, I think the following code will return the correct value.
Just put the following code into "php code" field in "Default value" of the nodereference field.
With these code, the nid of author's content profile will be assigned in the nodereference field automatically when author is creating the node. And this nodereference field will keep unchanged when the editor is editing the node.

global $user; 
if ($node->uid) $current_node_uid=$node->uid; 
else $current_node_uid=$user->uid; 
$profile_nid=$node->your_nodereference_field_id[0]['nid'];
if ($profile_nid)    
   return array(       
      0 => array('nid' => $profile_nid),
   ); 
else {
   content_profile_load('your_content_type_id', $current_node_uid);
   return array(       
      0 => array('nid' => $node->nid),    
   ); 
};
blueblade’s picture

Hi,

I want to try out your code but instead of user profile, I want to assign a different content type. Which line should be replaced in that case? Thank you for your help =)

BB

happydrupal’s picture

As far as I know, to change "content type" is very different than just assign the default value of a field in a form.

Maybe you should check Nodetype first.

socialnicheguru’s picture

I am a little confused.

I have a scenario where I have a content profile.
I allow people to create several other stories. I want to have them all connect back via nodereference.

If they create stories at node/add I want them to automatically be referenced back to the content profile.
If I were to look at the content profile, I would like to see the entire list of items. So all the stories that Jane wrote should be linked back to her profile.

I created nodereference, story_ref, in my content profile.
I created nodereference, profile_ref, in the story.

I tried putting your code in profile_ref in default. Was that right? I guess I just need more instruction.

socialnicheguru’s picture

I found I had to make a slight change:


global $user;
if ($node->uid) $current_node_uid=$node->uid;
else $current_node_uid=$user->uid;
$profile_nid=$node->your_nodereference_field_id[0]['nid'];
if ($profile_nid)
return array(
0 => array('nid' => $profile_nid),
);
else {
$NEW_NODE= content_profile_load('your_content_type_id', $current_node_uid);
return array(
0 => array('nid' => $NEW_NODE->nid),
);
};

socialnicheguru’s picture

How can I modify so that I can return multiple items as default to the nodereference or can default only be one value?
The nodereference is set to unlimited btw.

Thanks.