Hey,

I'm using rules to generate a new piece of content and populate some of the fields. It seems that tokens can't be used to populate any field other than text fields as it will not validate. What PHP code do i need to enter in order to populate a node reference field in the form '[nid:1]'?

Thanks any help appreciated

Alx

CommentFileSizeAuthor
#27 retestnoderef.txt4.62 KBdhalgren
#26 rule2.txt4.33 KBjohnthomas00
#22 rule.txt6.79 KBdhalgren

Comments

mitchell’s picture

Status: Active » Fixed

You 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.

AlxM’s picture

Status: Fixed » Active

Thats 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

johnthomas00’s picture

Version: 6.x-1.0-rc2 » 6.x-1.0

First, 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:

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

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.

johnthomas00’s picture

Category: support » task

Wonder if #1 is true. I cannot get it to work.

johnthomas00’s picture

Perhaps 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).

fago’s picture

Category: task » support

You should be able to do that with php input.

johnthomas00’s picture

Cannot 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:

return array(
  0 => array('nid' => $jt_my_nid),
);

The field does not get populated.

Again, any tips are appreciated.

fago’s picture

Which CCK version are you using? Is this the exact input in the code field? Is it saved correctly?

johnthomas00’s picture

cck-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.

 [nid:Array]

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.

rokr’s picture

Subscribing.
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

dhalgren’s picture

Subscribing.

dhalgren’s picture

a solution:
- load node by id
- create new content
- pupoluate a field, php return array(0 => array('nid' => $node->nid));

johnthomas00’s picture

I 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.

array (
  'rules' => 
  array (
    'rules_1' => 
    array (
      '#type' => 'rule',
      '#set' => 'event_node_insert',
      '#label' => 'AutoCreate Notes',
      '#active' => 0,
      '#weight' => '0',
      '#categories' => 
      array (
      ),
      '#status' => 'custom',
      '#conditions' => 
      array (
        0 => 
        array (
          '#weight' => 0,
          '#info' => 
          array (
            'label' => 'Created content is Content B',
            'arguments' => 
            array (
              'node' => 
              array (
                'type' => 'node',
                'label' => 'Content',
              ),
            ),
            'module' => 'Node',
          ),
          '#name' => 'rules_condition_content_is_type',
          '#settings' => 
          array (
            'type' => 
            array (
              'content_b' => 'content_b',
            ),
            '#argument map' => 
            array (
              'node' => 'node',
            ),
          ),
          '#type' => 'condition',
        ),
      ),
      '#actions' => 
      array (
        2 => 
        array (
          '#weight' => -10,
          '#info' => 
          array (
            'label' => 'Load Content B',
            'label callback' => false,
            'arguments' => 
            array (
              'nid' => 
              array (
                'type' => 'number',
                'label' => 'Content ID',
              ),
              'vid' => 
              array (
                'type' => 'number',
                'label' => 'Content Revision ID',
                'description' => 'If you want to load a specific revision, specify it\'s revision id. Else leave it empty to load the current revision.',
                'required' => false,
              ),
            ),
            'new variables' => 
            array (
              'node_loaded_jt' => 
              array (
                'label' => 'JT loaded Content B content',
                'label callback' => false,
                'type' => 'node',
              ),
            ),
            'module' => 'Node',
          ),
          '#name' => 'rules_action_load_node',
          '#settings' => 
          array (
            'nid' => '[node:nid]',
            'vid' => '',
            '#argument map' => 
            array (
              'node_loaded' => 'node_loaded_jt',
            ),
            '#eval input' => 
            array (
              'token_rules_input_evaluator' => 
              array (
                'nid' => 
                array (
                  0 => 'node',
                ),
              ),
            ),
          ),
          '#type' => 'action',
        ),
        0 => 
        array (
          '#type' => 'action',
          '#settings' => 
          array (
            'title' => '[node:title] - Notes',
            'type' => 'task',
            'node_access' => 1,
            '#argument map' => 
            array (
              'author' => 'user',
              'node_added' => 'jt_node_added_notes',
            ),
            '#eval input' => 
            array (
              'token_rules_input_evaluator' => 
              array (
                'title' => 
                array (
                  0 => 'node',
                ),
              ),
            ),
          ),
          '#name' => 'rules_action_add_node',
          '#info' => 
          array (
            'label' => 'Add new content notes',
            'label callback' => false,
            'arguments' => 
            array (
              'author' => 
              array (
                'type' => 'user',
                'label' => 'User, who is set as author',
              ),
              'title' => 
              array (
                'type' => 'string',
                'label' => 'Title',
                'description' => 'The title of the newly created content.',
              ),
            ),
            'new variables' => 
            array (
              'jt_node_added_notes' => 
              array (
                'label' => 'Notes - Auto Generated',
                'label callback' => false,
                'type' => 'node',
                'save' => true,
              ),
            ),
            'module' => 'Node',
          ),
          '#weight' => 0,
        ),
        1 => 
        array (
          '#weight' => 2,
          '#info' => 
          array (
            'label' => 'Populate task relationship field back',
            'label callback' => false,
            'arguments' => 
            array (
              'node' => 
              array (
                'type' => 'node',
                'label' => 'Content',
              ),
            ),
            'eval input' => 
            array (
              0 => 'code',
            ),
            'module' => 'CCK',
          ),
          '#name' => 'content_rules_action_populate_field',
          '#settings' => 
          array (
            'field_name' => 'field_task_asset',
            '#argument map' => 
            array (
              'node' => 'jt_node_added_notes',
            ),
            'value' => 
            array (
              0 => 
              array (
                'nid' => NULL,
              ),
            ),
            'code' => 'return array(
  0 => array(\'nid\' => $node->nid),
);',
          ),
          '#type' => 'action',
        ),
      ),
    ),
  ),
)
johnthomas00’s picture

Component: User interface » Rules Engine
Category: support » bug

Changing to bug report since issue confirmed by others.

fago’s picture

Category: bug » support
Status: Active » Fixed

You 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".

rokr’s picture

Category: support » bug
Status: Fixed » Active

If 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.

johnthomas00’s picture

fago,
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.

johnthomas00’s picture

fago,

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.

malukalu’s picture

subscribing

crea’s picture

Confirming this bug and subscribing.

jastraat’s picture

Subscribing

dhalgren’s picture

StatusFileSize
new6.79 KB

attached 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)

johnthomas00’s picture

dhalgren, 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.

dhalgren’s picture

yes 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)

johnthomas00’s picture

dhalgren,

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.

johnthomas00’s picture

Version: 6.x-1.0 » 6.x-1.1
StatusFileSize
new4.33 KB

dhalgren,

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.

dhalgren’s picture

Assigned: Unassigned » dhalgren
Category: bug » support
StatusFileSize
new4.62 KB

johnthomas

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.

johnthomas00’s picture

Still, 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?

dhalgren’s picture

Assigned: dhalgren » Unassigned
Category: support » bug

after 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).

johnthomas00’s picture

Category: bug » support
Status: Active » Fixed

I 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]));

dhalgren’s picture

yes this is a generalized form of :
return array(0 => array('nid' => $node->nid));

johnthomas00’s picture

dhalgren,

Thank you again! You were very helpful! Best of luck out there!

johnthomas00’s picture

Status: Fixed » Closed (fixed)
curtaindog’s picture

@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.

thekayra’s picture

Chaps2 was kind enough to share the rule creation procedure. Might a little easier to follow.

http://drupal.org/node/223074#comment-1429424

dogboy72’s picture

FWIW, 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.