2

I have created a field called people_field. I used this in another content type as an existing field. The problem is now it's screwing up a view that uses the field in a relationship.

So what I want to do is keep the content but make move all the data from one filed into a new field, if that makes sense?

I can't figure a way to do this without losing all my data.

5

Let's consider that your content type is article and existing field is field_old and new field is field_new. Then you can use the script at once to copy all the data from old field to new field.

$article_nodes = node_load_multiple(array(), array('type' => 'article'));
foreach ($article_nodes as $article_node) {
  $article_node->field_new = $article_node->field_old;
  field_attach_update('node', $article_node);
}

Am not sure, if this is what you are looking for.

  • Awesome thanks, stupid question but, where do I put this? I assume it just needs to get triggered once, do I need to create a module for this? – karl Apr 7 '16 at 14:11
  • Just put in a separate PHP file and run it using drush scr filename.php or put inside a custom function in a custom module and run the function using drush php-eval "function_name" – Suresh R Apr 7 '16 at 14:31
  • Fantastic code! Thank you! Saved me hours and hours. There is one mistake on line 3. Instead of $media_node->field_old; this should be $article_node->field_old; – Ben May 11 '18 at 10:51
4

If you have a content type A and their have two field "field_old" and "field_new", then you can transfer data like this code :

$node_load_multiple = node_load_multiple(array(), array('type' => 'A'));
foreach ($node_load_multiple as $node) {
 $node->field_new = $node->field_old;
 unset($node->field_old);
 field_attach_update('node', $node);
}

This code will transfer one field value to another for a perticular content type and your another content type have now different field with it's own data.

Note: Create a menu item and in callback of that menu item put this code and run your menu.

  • Awesome thanks, stupid question but, where do I put this? I assume it just needs to get triggered once, do I need to create a module for this? – karl Apr 7 '16 at 13:01
  • Just create a menu item and in callback of that menu item put this code and run your menu. – Manish Kumar Apr 11 '16 at 12:08
1

Step 1: Create a Rules Component

Have a look at this example of a Rules Component (in Rules export format):

{ "rules_copy_a_field_value_to_another_field" : {
"LABEL" : "Copy a field value to another field",
"PLUGIN" : "rule",
"OWNER" : "rules",
"REQUIRES" : [ "rules" ],
"USES VARIABLES" : { "node" : { "label" : "Node", "type" : "node" } },
"IF" : [
  { "entity_has_field" : { "entity" : [ "node" ], "field" : "field_people_field_old" } },
  { "entity_has_field" : { "entity" : [ "node" ], "field" : "field_people_field_new" } }
],
"DO" : [
  { "data_set" : {
      "data" : [ "node:field-people-field-new" ],
      "value" : "[node:field-people-field-old]"
    }
  }
]

} }

Some more details to further explain the above above example:

  • This Rules Component will be used in "Step 2" below.
  • In my case I have a field with machine name field_people_field_old, and I want all its content to be copied to field_people_field_new.
  • I also added the Rules Conditions Entity has field to make that those fields are available for further processing in this Rules Component.

I've not added a Rules condition here about selected content types, that's possibly an extra condition you might want to add also. But if the selection list field is only used for the content types you're interested in, then the Entity has field conditions should be sufficient already.

If you want, just grab (copy) the exported rule above, change all occurrences of the machine names of those fields to match the machine names of your fields. After doing so you can simply import this Rules Component in your own environment.

After you imported this Rules Component, navigate to admin/config/workflow/rules/components in your own site. Then use the hyperlink "execute" to qa-test it in your own site. After you use that link, use the button that says Switch to direct input mode. Then you'll be able to enter some Node identifier and hit the Execute button. Depending on the node id you selected (i.e. if it has that field, and if the Rules Condition is satisfied), your node will be updated accordingly.

After you're confident your Rules Component works perfect for all variations of your nodes, move on to step 2 below.

Step 2: Let VBO to the bulk of the work

This step is pretty easy ... Just create an appropriate view of all your nodes to be processed, and transform it into a Views Bulk Operations view. The clue to what you want VBO to do as an operation is ... you guessed it: Execute the Rules Component from Step 1.

If you're not familiar with how to do so, have a look at the video about Using Rules components with VBO".

Of course there are multiple variations of the above approach, like processing by component type, narrowing down your Views output to only "a" specific value, etc. But that should be straight forward, and up to your own imagination.

PS: I'm assuming this question is about D7.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.