Skip to content

Instantly share code, notes, and snippets.

@aschanMarcus
Created July 2, 2014 15:01
Show Gist options
  • Save aschanMarcus/fb64a5508ac4c9b6eb4a to your computer and use it in GitHub Desktop.
Save aschanMarcus/fb64a5508ac4c9b6eb4a to your computer and use it in GitHub Desktop.
Create users for every 'fitspirator' node without an entity reference.
/**
* Create user for every fitspirator node without any entity reference.
*/
$query = db_select('node', 'n');
$nids = $query
->fields('n', array('nid'))
->condition('type', 'fitspirator')
->execute()
->fetchCol();
$nodes = node_load_multiple($nids);
$role = user_role_load_by_name('fitspirator');
foreach ($nodes as $node) {
if (empty($node->field_fitspirator)) {
if (!user_load_by_name($node->title)) {
$edit = array(
'name' => $node->title,
'status' => 1,
'language' => $node->language,
'roles' => array($role->rid => $role->name),
);
$user = user_save(NULL, $edit);
if ($user) {
$node->uid = $user->uid;
$node->field_fitspirator[$node->language][0]['target_id'] = $user->uid;
node_save($node);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment