Rename multiple nodes based on the value of a field

Public

Use case: client had a major product renaming coming up, and all nodes had to be renamed at once. We added an "Upcoming Name" field and a "Previously Known As" field to each product node, and admins populated the "Upcoming" field with values. On the agreed-upon date, we simply ran the following PHP script via Drush (to bootstrap the database).

</> CopyGet raw version
php
  1. <?php
  2.  
  3. $query = "SELECT * FROM node
  4. LEFT JOIN field_data_field_upcoming_name ON (node.nid = field_data_field_upcoming_name.entity_id)
  5. WHERE field_upcoming_name_value IS NOT NULL;";
  6. $result = db_query($query);
  7. foreach ($result as $row) {
  8. $node = node_load($row->nid);
  9. if ($node->title != $node->field_upcoming_name['und'][0]['value']) {
  10. print "on node/".$node->nid." we are changing ".($node->title)." to ".$node->field_upcoming_name['und'][0]['value']."\n";
  11. $node->field_previously_known_as['und'][0]['value'] = $node->title;
  12. $node->title = $node->field_upcoming_name['und'][0]['value'];
  13. node_save($node);
  14. }
  15. }
  16. ?>