0
votes

I have a content type called "Publications" and in that content type I have two fields, "first author" and "authors". I already have created a lot of content with both fields and both of them have a lot of information in them. What I need to do is copy or move everything from the field "first author" to the field "authors" Both fields are node references and I'm using Drupal 7.

Is this possible with a module or maybe from SQL. I don't know what to do, I've been trying to do this from weeks and I'm stuck :( please help!

Thanks so much!

1

1 Answers

1
votes

You should write a module to do this.

You will need a function that does something like this:

<?php 
$type = "publications"; 
$nodes = node_load_multiple(array(), array('type' => $type)); 
foreach($nodes as $node){
    $node->field_authors[LANGUAGE_NONE][0]['nid'] = $node->field_first_author[LANGUAGE_NONE][0]['nid']; 
    node_save($node);
}
?>

For a standalone script, you would need to add the following:

require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

Then the above code.

You would therefore have a php called updateAuthors.php with the following contents:

<?php
    require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
    drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

    $type = "publications"; 
    $nodes = node_load_multiple(array(), array('type' => $type)); 
    foreach($nodes as $node){
        $node->field_authors[LANGUAGE_NONE][0]['nid'] = $node->field_first_author[LANGUAGE_NONE][0]['nid']; 
        node_save($node);
    }
?>

To add the value in first_author as a new node_reference in field_authors, you need to do something like this:

<?php
    require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
    drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

    $type = "publications"; 
    $nodes = node_load_multiple(array(), array('type' => $type)); 
    foreach($nodes as $node){
        $firstAuthor = $node->field_first_author[LANGUAGE_NONE][0]['nid']; 
        if ($firstAuthor === false){
            continue;
        }
        $numberOfValuesInFieldAuthors = count($node->field_authors[LANGUAGE_NONE]);
        $node->field_authors[LANGUAGE_NONE][$numberOfValuesInFieldAuthors]['nid'] = $firstAuthor
        node_save($node);
    }
?>

This will skip any nodes that don't have a first author set, and for nodes that do have one set, will add the value in field_first_author as a new node reference in the field_authors field