--- includes/nodereference_url.rules.inc
+++ includes/nodereference_url.rules.inc
@@ -0,0 +1,242 @@
+<?php
+
+/**
+ * Implementation of hook_rules_action_info().
+ */
+function nodereference_url_rules_action_info() {
+  $info = array();
+  
+  $info['nodereference_url_rules_action_populate_field'] = array(
+    'label' => t('Populate a nodereference URL field'),
+    'arguments' => array(
+      'node' => array(
+        'type' => 'node',
+        'label' => t('Content'),
+      ),
+    ),
+    'eval input' => array('code'),
+    'help' => t('You should make sure that the used field exists in the given content type.'),
+    'module' => 'CCK',
+  );
+  
+  return $info;
+}
+
+/**
+ * Action: populate a nodereference URL field.
+ */
+function nodereference_url_rules_action_populate_field($node, $settings, $element, &$state) {
+  // Get information about the field.
+  $field = content_fields($settings['field_name'], $node->type);
+  $value = _nodereference_url_rules_get_field_value($settings, $state);
+
+  if (!empty($field) && is_array($value)) {
+    $node->$settings['field_name'] = $value;
+    return array('node' => $node);
+  }
+}
+
+/**
+ * Action "populate a nodereference URL" configuration form.
+ */
+function nodereference_url_rules_action_populate_field_form($settings, &$form, &$form_state) {
+  $settings += array('field_name' => '', 'code' => '', 'value' => NULL);
+  
+  if (empty($settings['field_name'])) {
+    $form['settings']['field_name'] = array(
+      '#type' => 'select',
+      '#title' => t('Field'),
+      '#options' => nodereference_url_get_fields(),
+      '#default_value' => $settings['field_name'],
+      '#description' => t('Select the machine-name of the field.'),
+      '#required' => TRUE,
+    );
+    // Hide some form elements in the first step.
+    $form['negate']['#access'] = FALSE;
+    $form['input_help']['#access'] = FALSE;
+    $form['weight']['#access'] = FALSE;
+
+    // Replace the usual submit handlers with a own handler.
+    $form['submit']['#submit'] = array('nodereference_url_rules_action_populate_field_form_step_submit');
+    $form['submit']['#value'] = t('Continue');
+  }
+  else {
+    // Show the fields form here.
+    module_load_include('inc', 'content', 'includes/content.node_form');
+    $field = content_fields($settings['field_name']);
+
+    $form['#node'] = (object) array('type' => '', $settings['field_name'] => $settings['value']);
+    $form['#field_info'][$field['field_name']] = $field;
+    // We can't put it into $form['settings'] as this would break AHAH callbacks
+    $form += (array) content_field_form($form, $form_state, $field);
+    $form[ $settings['field_name'] ]['#weight'] = 4;
+    $form[ $settings['field_name'] ][0]['nid']['#type'] = 'textfield';
+    $form[ $settings['field_name'] ][0]['nid']['#value'] = NULL;
+    $form[ $settings['field_name'] ][0]['nid']['#default_value'] = isset($settings['value'][0]['nid']) ? $settings['value'][0]['nid'] : '';
+    $form[ $settings['field_name'] ][0]['nid']['#description'] = t('When using token, be sure the rendered value will be valid as nodereference.');
+
+    unset($form['#cache']);
+
+      // Advanced: PHP code.
+    $form['advanced_options'] = array(
+      '#type' => 'fieldset',
+      '#title' => t('Advanced: Specify the fields value with PHP code'),
+      '#collapsible' => TRUE,
+      '#collapsed' => empty($settings['code']),
+      '#weight' => 5,
+    );
+
+    $db_info = content_database_info($field);
+    $columns = array_keys($db_info['columns']);
+    foreach ($columns as $key => $column) {
+      $columns[$key] = t("'@column' => value for @column", array('@column' => $column));
+    }
+    $sample = t("return array(\n  0 => array(@columns),\n  // You'll usually want to stop here. Provide more values\n  // if you want your 'default value' to be multi-valued:\n  1 => array(@columns),\n  2 => ...\n);", array('@columns' => implode(', ', $columns)));
+
+    $form['advanced_options']['code'] = array(
+      '#type' => 'textarea',
+      '#title' => t('Code'),
+      '#default_value' => $settings['code'],
+      '#rows' => 6,
+      '#description' => t('Advanced usage only: PHP code that returns the value to set. Should not include &lt;?php ?&gt; delimiters. If this field is filled out, the value returned by this code will override any value specified above. Expected format: <pre>!sample</pre>Using <a href="@link_devel">devel.module\'s</a> \'devel load\' tab on a content page might help you figure out the expected format.', array(
+        '!sample' => $sample,
+        '@link_devel' => 'http://www.drupal.org/project/devel',
+      )),
+    );
+
+    // Add this file to be included when the form is built by rules
+    // as it's needed by CCKs add more button.
+    // See rules_after_build_include_files().
+    $form['#includes'][] = './'. drupal_get_path('module', 'node') .'/node.pages.inc';
+  }
+}
+
+function nodereference_url_rules_action_populate_field_form_step_submit($form, &$form_state) {
+  $form_state['element']['#settings']['field_name'] = $form_state['values']['settings']['field_name'];
+}
+
+/**
+ * Validate the chosen value or php code.
+ */
+function nodereference_url_rules_action_populate_field_validate($form, &$form_state) {
+  if (!isset($form_state['element']['#settings']['field_name'])) {
+    //Just validate the last step.
+    return;
+  }
+
+  if (isset($form_state['values']['code']) && ($php = $form_state['values']['code'])) {
+    if (strpos($php, 'return') === FALSE) {
+      form_set_error('code', t('You have to return the default value in the expected format.'));
+    }
+  }
+  else {
+    $settings = $form_state['element']['#settings'];
+    $tokens = array();
+    
+    // Rules module use '['. $variable_name .':' as prefix for tokens names. Here we only deal with
+    // nodes so we use directly [node: as prefix.
+    foreach (token_get_list('node') as $token) {
+      $tokens = array_merge($tokens, array_map(create_function('$string', 'return "[node:". $string ."]";'), array_keys($token)));
+    }
+    
+    // First exclude validation when using tokens. Indeed, nodereference validation
+    // will fail with non numeric values.
+    // Because a node nid can not be composed by more than 1 token, we only 
+    if (! in_array($form_state['values'][$settings['field_name']][0]['nid'], $tokens)) {
+      // Validate the field.
+      $field = content_fields($settings['field_name']);
+      $field_types = _content_field_types();
+      $function = $field_types[$field['type']]['module'] .'_field';
+      if (function_exists($function)) {
+        $form['#node'] = (object)array('type' => '', $settings['field_name'] => $form_state['values'][$settings['field_name']]);
+        $items = isset($form['#node']->$field['field_name']) ? $form['#node']->$field['field_name'] : array();
+
+        //Make sure AHAH 'add more' button isn't sent to the fields
+        // for processing.
+        unset($items[$field['field_name'] .'_add_more']);
+
+        $function('validate', $form['#node'], $field, $items, $form, NULL);
+        content_field('validate', $form['#node'], $field, $items, $form, NULL);
+      }
+    }
+  }
+}
+
+function nodereference_url_rules_action_populate_field_submit(&$settings, $form, &$form_state) {
+  // Take over field values and filter out private properties added by CCK
+  $settings['value'] = array_filter($form_state['values'][$settings['field_name']], 'is_array');
+
+  foreach ($settings['value'] as $key => $data) {
+    foreach (array_filter(array_keys($data)) as $col) {
+      if ($col[0] == '_') {
+        unset($settings['value'][$key][$col]);
+      }
+    }
+    if ($key && count(array_filter($settings['value'][$key])) == 0) {
+      // For multi-valued fields don't check for any additional empty values.
+      unset($settings['value'][$key]);
+    }
+  }
+
+  $settings['code'] = $form_state['values']['code'];
+
+  if (function_exists('rules_action_custom_php_submit')) {
+    // Support adding variables to the php code, if php module is present.
+    rules_action_custom_php_submit($settings, $form, $form_state);
+  }
+
+  // Add all values to the input evaluator, so that textfields / textares can
+  // make use of it.
+  $names = array('code');
+
+  foreach ($settings['value'] as $key => $data) {
+    foreach (array_filter($data, 'is_string') as $col => $value) {
+      $names[] = "value|$key|$col";
+    }
+  }
+  $form_state['element']['#info']['eval input'] = $names;
+}
+
+
+/**
+ * Label callback: Improve the label of the action.
+ */
+function nodereference_url_rules_action_populate_field_label($settings, $argument_labels) {
+  return t("Populate @node's field '@field'", array('@field' => $settings['field_name']) + $argument_labels);
+}
+
+/**
+ * Returns the nodereference URL fields of a given field type only.
+ * Suitable for using it with #options.
+ */
+function nodereference_url_get_fields($type = NULL) {
+  $fields = array();
+  
+  foreach (content_fields() as $field) {
+    if ($field['widget']['type'] == 'nodereference_url' && (!isset($type) || $field['type'] == $type)) {
+      $fields[$field['field_name']] = $field['field_name'];
+    }
+  }
+  
+  asort($fields);
+  return $fields;
+}
+
+function _nodereference_url_rules_get_field_value($settings, &$state) {
+  if ($settings['code']) {
+    if (function_exists('rules_input_evaluator_php_apply')) {
+      // Support adding variables to the php code, if php module is present.
+      $value = rules_input_evaluator_php_apply($settings['code'], $settings['vars'], $state, FALSE);
+    }
+    else {
+      ob_start();
+      $value = eval($settings['code']);
+      ob_end_clean();
+    }
+  }
+  else {
+    $value = $settings['value'];
+  }
+  return $value;
+}
+


