Closed (fixed)
Project:
Rules
Version:
6.x-1.1
Component:
Rules Engine
Priority:
Normal
Category:
Support request
Assigned:
Unassigned
Reporter:
Created:
26 May 2009 at 15:58 UTC
Updated:
20 Mar 2011 at 15:52 UTC
Jump to comment: Most recent file
Comments
Comment #1
mitchell commentedYou don't need php code to accomplish this. Put a 'Load a node' action before the 'create a node' action. This will allow you to use its values in your 'populate a field' action.
Comment #2
AlxM commentedThats not quite what i meant. So i have content type A and when a user flags a node of this type my Rule creates a node of content type B using some of the fields from A. I have a node reference field in B which i want to use to reference A.
However using a token in a node reference field returns an error because the token is not a valid node reference (this happens to all non-text fields for me). I believe this is the same problem as this: drupal.org/node/473146 in which it states that you can use a php return array to input values like this.
Since my first post i have found a work around which is to just use a text field and populate it with the nid and treat it as a reference. Although there are other fields, such as date fields that could do with having the correct format (other than just a text field) used.
Thanks all the same
Comment #3
johnthomas00 commentedFirst, thanks for the great module.
I can confirm this / would like help too. I am not a programmer, so some of the following may appear daft. I risked appearing daft in order to be most helpful about getting this fixed.
Results Desired:
After creation of node type Y
Create a node of type X and modify the cck Node reference on the New X to be the New Y
Challenges / Methods Tried:
Only relevant actions shown, other actions work fine.
Method Test 1. Add action
Populate a field
Arguments configuration (content: select node type Y)
Field: (select Node reference field in content type X)
continue
Arguments configuration (content: select node type Y)
Node Reference Field [node:title]
save
Result: RefFieldName: found no valid post with that title.
Side Note: This would seem to be the ideal/simple solution, but it does not seem to work.
Method Test 2. Add action
Populate a field
Arguments configuration (content: select node type Y)
Field: (select Node reference field in content type X)
continue
Arguments configuration (content: select node type Y)
Node Reference Field leave blank
php code:
Side Note 1: if, earlier in the rule, you create an action to create a string variable (where string $newvariable is [node:title]) and use $newvariable instead of $nid, the result is the same.
Side Note 2: if you save then edit this rule, [nid:Array], shows up in the reference field(s) and that must be deleted/removed to save the rule again, otherwise you get a "found no valid post with that title" similar to above.
Side Note 3: The rules provided example php code for a node reference seems to use nid, whereas when I create a reference by hand, Drupal seems to require a title.
save
Result: Node X is created, but reference field is empty. I tried a few different versions of Method 2.
I am happy to experiment.
Comment #4
johnthomas00 commentedWonder if #1 is true. I cannot get it to work.
Comment #5
johnthomas00 commentedPerhaps there could be a option to turn off validation of a field in a rule action. The rule works fine if I fill in a specific node, but I want the node to be a variable. Since a variable node will not validate, you could turn off validation in that instance (or perhaps validate as a node or proper variable).
Comment #6
fagoYou should be able to do that with php input.
Comment #7
johnthomas00 commentedCannot get php input to work. Any tips appreciated.
One example of my attemps was to create a rule to create a new number variable ($jt_my_nid) with number [node:nid].
Then I used the following php code to populate the reference field:
The field does not get populated.
Again, any tips are appreciated.
Comment #8
fagoWhich CCK version are you using? Is this the exact input in the code field? Is it saved correctly?
Comment #9
johnthomas00 commentedcck-6.x-2.4.tar.gz
rules-6.x-1.0.tar.gz
drupal-6.13.tar.gz
The field is a multi-unlimited field (i.e. I can reference as many other nodes as I want), in case that matters.
Yes, the above is the exact (copy and paste) input in the code field.
The action saves correctly (I think) because when I reopen the code is still exactly the same.
The only odd behavior is when I reopen the action the following code shows up in the first two node reference fields and a third empty field is blank.
I have to delete the above code in order to save the action again.
I have built other rules to populate other fields and that works great.
Comment #10
rokrSubscribing.
I'm also looking for a working PHP snippet which populates a nodereference field of (an existing) node A with the node id of a created node B.
Any hints welcome.
cheers, Ronald
Comment #11
dhalgren commentedSubscribing.
Comment #12
dhalgren commenteda solution:
- load node by id
- create new content
- pupoluate a field, php return array(0 => array('nid' => $node->nid));
Comment #13
johnthomas00 commentedI tried #12, but that did not work for me.
I tried a node reference when only one reference was allowed, but that did not work either.
I tried a number of other things too.
Following is a code export of my latest attemp, in case that allows anyone to make suggestions.
Comment #14
johnthomas00 commentedChanging to bug report since issue confirmed by others.
Comment #15
fagoYou can't set the node reference for a node that hasn't an ID yet. That's the problem which I'd call "by design".
Comment #16
rokrIf you save the new node before filling the field you actually have that ID. That works for a cck link field for example. The problem is: you can't fill a nodereference field with any ID via token or php.
Comment #17
johnthomas00 commentedfago,
Thank you for considering this bug.
I modified the rule shown above in 13 to have a "save node" action just before the "populate a field" action. This rule still does not work and the saving made no difference. I am happy to produce another export if you think that would be helpful.
Comment #18
johnthomas00 commentedfago,
I have tried everything on this. Any other suggestions?
The bottom line, populate a field with a node reference does not work. I have tried everything.
Comment #19
malukalu commentedsubscribing
Comment #20
crea commentedConfirming this bug and subscribing.
Comment #21
jastraat commentedSubscribing
Comment #22
dhalgren commentedattached my rule.
essentially:
ON event A node has been flagged, under "oggetti posseduti" ("owned objects")
IF
Flagged content is Gallery Image (you can remove this condition)
DO
Load content by id (load flagged node)
Add new content (add a review type node)
Populate new content of type review's field 'field_node_immage_ref' (populate nodereference field with the NID of the loaded node)
Populate new content of type review's field 'field_nuovo' (you can remove this action)
Comment #23
johnthomas00 commenteddhalgren, thank you for your help.
I am not sure if you were able to populate a nodereference field or not. I suspect not, but hope to hear otherwise.
I was unable to populate a nodereference field with your help. First, I tried creating a rule using your outline above, but that did not help. Second, I imported your rule.txt, excepting I changed the node names and changed the event from flagging a node to creating a certain type of node. Neither worked. In both cases, as in the past, the node reference field is not populated.
Please tell me you have been able to populate a nodereference.
Comment #24
dhalgren commentedyes i populate the nodereference field.
i think that the problem may be the creation of a new node (instead of flagging), cause i' dont know how drupal save the new node (and _when_)
if you import my rule as is, and generate the flag and the adequate content type, you can experiment if the problem is the creation of a new node.
if you export your rule, i can try a review.
(pardon my english)
Comment #25
johnthomas00 commenteddhalgren,
I imported your rule after only changing the flag name, the new content type name, and the reference field name. With the gui, I deleted the condition and the population of the other field. The the result is the same, the reference field does NOT populate.
I will test with a clean install to see if I can isolate the issue. Please stand by.
Comment #26
johnthomas00 commenteddhalgren,
The bottom line, is the population does not work, on a clean site either. I wonder what is different on our sites. I am using CentOS 5.3 with php upgraded to 5.2.10.and mysql upgraded to 5.1.38, both from Remi's repository. I am using clean urls and a multisite installation.
To summarize my latest test, I created a new site and only loaded the required modules. I created a node type page2. I added a node reference field to type page referring to page2. I create a flag. I create a rule. The rule executes, the new type is created, but the field is not populated.
Attached is the rule from this new clean site. Any suggestions are appreciated.
Comment #27
dhalgren commentedjohnthomas
i've imported your rule and don't work.
i've created a new rule, very similar, _and work._
this rule load the flagged page, create a new content (of a different type) and populate the new content's field with the nid of the page.
try this rule: you must first create a content type "Primo" and add a nodereference field "page2" with the select list widget.
change issue's category 'cause this is more a support request than a bug.
Comment #28
johnthomas00 commentedStill, no success here. My gut tells me this is a bug somewhere (since I and several others cannot get it to work) but I am not entirely convinced it is rules since you can get it to work.
Thank you for your help. I will work to isolate the issue.
I did exactly as you suggested, with no success. I then exported your rule and did a diff against the one you posted to be sure the rule is the same:
diff retestnoderef.txt my-retestnoderef.txt
4c4
< 'rules_19' =>
---
> 'rules_2' =>
I also tried disabling my php eaccelerator, to no avail.
Following is the debug from the rule execution:
* 0 ms "A node has been flagged, under "Flag this"" has been invoked.
* 1.267 ms Executing the rule "tetest" on rule set "A node has been flagged, under "Flag this""
* 1.631 ms Loaded variable "flagged content"
* 30.592 ms Action execution: "Load content by id---"
* 30.865 ms Successfully added the new variable "content with id [node:nid]"
* 31.129 ms Action execution: "Add new content--"
* 35.828 ms Successfully added the new variable "new content2"
* 36.077 ms Action execution: "Populate new content2's field 'field_field2'"
* 36.413 ms Saved variable new content2 of type node.
* 43.099 ms Evaluation of "A node has been flagged, under "Flag this"" has been finished.
I will post a tar.gz of an export of my test database and send the link to you using your private contact page.
Can somebody else test dhalgren suggestions to see if I am a bozo?
Comment #29
dhalgren commentedafter many test on johnthomas00's site, my rule don't work. this is very strange and i don't understand why. maybe is really a bug.
change status to bug and unassign (php knowledge required).
Comment #30
johnthomas00 commentedI got it to work based on the unsuspecting help of curtaindog with his solution here:
http://drupal.org/node/483602
Use the following php code to populate a node reference:
return array(0 => array('nid' => [the_token_name_of_the_loaded_nid:nid]));Comment #31
dhalgren commentedyes this is a generalized form of :
return array(0 => array('nid' => $node->nid));
Comment #32
johnthomas00 commenteddhalgren,
Thank you again! You were very helpful! Best of luck out there!
Comment #33
johnthomas00 commentedComment #34
curtaindog commented@johnthomas00 - Cool, glad to be of help in resolving this.
Another cheeky trick I've employed when using php which the validator refuses to accept is to check the current url. If my rule determines that the url is part of the rule admin I just return something I know will validate. That way I get to use whatever values I want.
Comment #35
thekayra commentedChaps2 was kind enough to share the rule creation procedure. Might a little easier to follow.
http://drupal.org/node/223074#comment-1429424
Comment #36
dogboy72 commentedFWIW, I was able to use #30/31 only if I made my node reference not required. In my case, the field is required, but because I am creating the content through rules, I can make sure the field is filled. I use Field Permissions to keep users who need to edit the content from changing this field.