I thought I made a note on this, because while my solution could be not elegant, it is a solution that somebody might use, or somebody else might correct in which case it will benefit me. Please let me know if there is a more elegant, effective way of doing this, or if there is a module that does the trick.
The problem:
- How to pass an argument to a CCK input form and tell node reference that this parameter is the node that it wants to pick.
Use:
- My site where I want users to be able to create child nodes of a different content type based on a particular node. For example, passages in a book, and I want users to make interpretations of each passage, several interpretations will have a singular passage as parent.
The information available:
- A lot, including this custom module from open concepts (http://openconcept.ca/blog/ethan/creating_custom_cck_widgets) that does the trick, and which I copied my function from.
The solution:
- I will send the node number by parameter from the referring node, and catch it in the CCK node with the node reference field.
- Take the number from the URL and tell node reference that that number is the referring node.
How:
- Create a CCK nodereference field in the content type which I called interpretation.
- Paste the following code in the "Default value" section "PHP code" field:
$url = request_uri();
$arguments = explode('/', $url);
$nid = array_pop($arguments);
return array(0 => array('nid' => $nid ));
How it works:
When you call the add node function, you add the number of the referring node after the trailing slash "/", the little function breaks it, and selects the node to reference based on that number. Your call URL might look something like:
http://www.example.com/node/add/interpretation/195
Note:
- You will get an error when submitting your field inside the content type... this is because the nodereference module tries to evaluate your PHP expression on the fly. To get over it, take your URL and add "/1" or any number so that when it is tested, it knows it works.
Comments
error
I have got the following errors:
* fieldrefertoparent : This post can't be referenced.
* The default value php code created Array ( [0] => Array ( [nid] => field_fieldrefertoparent ) ) which is invalid.
Any ideas why?
What I did:
Step1) Create child content type
Step2) Add CCK nodereference field to it
Step3) I name it fieldrefertoparent
Step4) Under default value I pasted your code above
Step5) I attempted to save and got the above error.
Please assist. Thanks!
The PHP code which you are
The PHP code which you are using takes the URL on the address field of your browser and breaks it at every "/", then it takes whatever is after the last "/", and that is your value.
Add a "/##" with ## being a node that actually exists when you create your fieldrefertoparent. This will get you over the hurdle of creating/editing your content type. For some reason, it wants to evaluate it right there, and if your URL doesn't have a number at the end, it will give you the error.
The other side is that when you want to create a new node fieldrefertoparent you have to add the /## with the ## being the node that is referring... I haven't done that part yet, but it will involve a link from the referring node that has the node # at the end.
http://www.faunapolis.org/
I tried. It still doesn't
I tried. It still doesn't work.
My URL was initially like this:
http://localhost/drupal/?q=admin/content/types/child/fields/fieldreferto...
I added /3 to it to become like this:
http://localhost/drupal/?q=admin/content/types/child/fields/fieldreferto...
I still received the same error
* fieldrefertoparent : This post can't be referenced.
* The default value php code created Array ( [0] => Array ( [nid] => field_frefer ) ) which is invalid.
I am using Drupal 5.5, CCK 1.6. Hope you can help me with this. Thanks!
Take a look at your error expression
Take a look at your error expression; it is inserting the "field_frefer" text instead of the number 3. ILook at the 2 functions on the code, and you will see what they do. I am also learning PHP as I go, this is a nice little code to understand. Check your code, try it a few times until it works if your code is fine.
http://www.faunapolis.org/
Sorry about asking. I tried
Sorry about asking. I tried again but I can't seem to get it to work.
What do you think my mistake is? What should I do?
...
You want to add the "/3" to the node creation form. Like this:
q=node/add/child/3
The purpose of the PHP snippet is to set the initial value for the nodereference field on that form. That is, when you bring up the node creation form, the snippet reads the "3" from the URL and puts it in the nodereference field.
Thanks!
Was struggling with this, adding a q=3 at the end of the url for editing the field and it allowed me to save a php code default value. After I was able to save once, I didn't need to add q=3 anymore.
Thanks again!
...
That's a nice solution!
You can wrap the 'return' in a check for numeriuc argument. That is, change:
to:
This might be exactly what I
This might be exactly what I need! Thank you to faunapolis for sharing and all the others for enhancing and clarifying.
I have two additional needs that I'm wondering if anyone knows how to accomplish as part of this solution.
1. I'd like to make it so that the nodereference field auto-populates (as described above) but then not allow the user to modify it (grayed out or protected). In other words, the field is simply to establish a relationship between the referrer and the add/node and should not be changed by the user.
2. I'd like to establish the link from the referring node with a CCK Button field but haven't seemed to find the right code to make it work.
Also, just want to confirm that this works with Clean URLs and URL Aliasing all the same.
Any input or ideas would be greatly appreciated! Thanks!
See this post for complete answers to 1 and 2
http://drupal.org/node/158172#comment-1026155
$url =
nice trick works!
nice trick
works!
+1. Excellent solution.
+1. Excellent solution. Thanks
www.albatrossdigital.com
Prepopulate module
Found this module:
http://drupal.org/project/prepopulate
I would have thought that this would be an ideal way to prepopulate the node reference field - I have not used this module myself yet, but plan to
thanks to WorldFallz in the following post: http://drupal.org/node/414220#comment-1433264