Rules - Copy CCK content to another CCK in the same node - PHP?

Events happening in the community are now at Drupal community events on www.drupal.org.
matteblacke's picture

I have created a new content type with 2 new fields:

field_track
field_track_session

using Rules (or another method) when the user saves a node i want the contents of field_track to copy to field_track_session

field_track_session will be hidden so the user can't manually copy (i want it done automatically on save anyway)

I've not used rules before but have set up the following so far:

ON event After saving new content
IF
Created content is Full Track Content

DO
Populate created content's field 'field_track_session'

What i need is the php to be able to copy the content from field_track to field_track_session.

Both content type fields are just simple text.

Basically, how can i reference a node's field if that node has not been saved yet?

return array(
  0 => array('value' => field_track)
);

Comments

CCK Computed Field

shushu's picture

Maybe the CCK computed field can save you some time.

Annoyingly, at a later date I

matteblacke's picture

Annoyingly, at a later date I want field_track_session to be of different type i.e. a custom CCK e.g. Trackfield

Computed field works great if I didn't need this option.

Someone must know the php code to reference a field within the same node???

something like this:

return array(
  0 => array('value' => $node->field_track[0]['value'])
);

Looks fine

itangalo's picture

Your snippet looks fine. Two ideas for bug hunting:

  1. If it is a new node you might have to save it first – and you might have to do this save in a separate rule to be sure that it has occured.
  2. You might also want to check the variable name. It might not be "$node", and if you're using a node reference field you want to fetch field_track[0]['nid'] instead of field_track[0]['value']. Enable the Devel module and run dpm($node) to have a closer look at the available properties.

Yesterday I wrote a Rules action that copies CCK fields, as an exercise for learning to write actions. It can be found and downloaded as a module at http://groups.drupal.org/node/103004. (It should work just fine, but I still have to say that you use it at your own risk.) It would allow you to do this kind of stuff without entering custom PHP.

Good luck!
//Johan Falk, NodeOne, Sweden

This new rule is almost

matteblacke's picture

This new rule is almost perfect for me. the only problem i have that the field types are slightly different now.

Devel readout:

field_track (Array, 1 element)
0 (Array, 3 elements)
value (String, 103 characters ) 52.856,-1.95,150 52.456,-1.85,150 51.822,-2.23,...
format (NULL)
safe (String, 111 characters )

<

p>52.856,-1.95,150 52.456,-1.85,150 51.822,-2....

field_track_session (Array, 1 element)
0 (Array, 4 elements)
value (NULL)
valuetype (NULL)
trackdatasets (NULL)
attributes (NULL)

is there a way to copy the 'value' of a field to the 'value' of the next field? (and then maybe fill in the rest of the array with default data?)

OK, by commenting out the

matteblacke's picture

OK, by commenting out the below section i am able to get the value from one field to copy to the other... thank you

/
* Validation for "Show a menu name" configuration form
*/
function cck_copy_action_action_copy_field_validate($form, &$form_state) {
  $source = content_fields($form['settings']['source_field']['#value']);
  $target = content_fields($form['settings']['target_field']['#value']);
  /
if ($source['type'] != $target['type']) {
    form_set_error('target_field', t('Source and target fields must be of the same type.'));
  }*/
 
}

as this is going to be a very controlled copy (i.e. i know exactly what is going to be copied) How and where can i set the defaults of the field_track_session?

so:

'value' will be copied across.
'valuetype' needs to be set to: 'latlonalt'
'trackdatasets' needs to be set to: 'latitude,longitude,distance'
'attributes' needs to be set to: 'a:1:{s:8:\"is_climb\";N;}'

Would it be something like the following?

/**
* Action "Copy a CCK field"
*/
function cck_copy_action_action_copy_field($source_node, $target_node, $settings) {
  $target_node->{$settings['target_field']} = $source_node->{$settings['source_field']};
  $target_node->{$settings['target_field']['#valuetype']} = 'latlon';
  $target_node->{$settings['target_field']['#trackdatasets']} = 'latitude,longitude,distance';
  $target_node->{$settings['target_field']['#attributes']} = 'a:1:{s:8:\"is_climb\";N;}';
  return array('target_node' => $target_node);
}

cck copy seems to have broken my other CCKs

efreedom's picture

I recently started noticing that everytime I try to change a piece of content (including content types outside of the one I used cck_copy for), the changes don't save.
So, one by one, I started turning modules on and off and trying to save the content.

It turns out that when I remove cck_copy, I am able to save my content.

Is there another way I can implement the same capability of cck_copy without using the module or can we get the module fixed?!

Bob

Bob Brown

Oops

itangalo's picture

I don't know what's going on with the module, but I've seen similar effects when bad CCK things happen -- if a field goes awry, no CCK field saves.

I would very much like to fix this particular piece of code, and even more to start an official Rules Bonus Pack module, but as things are now I won't have time to do it. :-(
Chances are that when I get time to fix it, I will be focusing entierly on Rules 2 and Drupal 7.

Sorry that I can't give you better news than this. If you're up to it, you are of course exremely welcome to debug the code yourself.

//Johan Falk, Sweden

Another method

itangalo's picture

Hello again

You can also check out this screencast to see how you can set CCK fields: http://nodeone.se/blogg/learn-rules-with-nodeone-part-8-use-php-to-popul...

Cheers,

Rules

Group organizers

Group categories

Categories

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: