Created
July 2, 2014 15:01
-
-
Save aschanMarcus/fb64a5508ac4c9b6eb4a to your computer and use it in GitHub Desktop.
Create users for every 'fitspirator' node without an entity reference.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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