Providing a default user in an installation profile in Drupal 7
Found some great clues in a test
http://api.drupal.org/api/drupal/modules--simpletest--drupal_web_test_case.php/7/source
Putting that to work in our installation profile's .install file, shown beneath a modification of standard.install's role creation to create an additional 'content manager' role and to name the 'administrator' role more appropriately, as 'developer':
// Create a role for developers, with all available permissions assigned. $devel_role = new stdClass(); $devel_role->name = 'developer'; $devel_role->weight = 3; user_role_save($devel_role); user_role_grant_permissions($devel_role->rid, array_keys(module_invoke_all('permission'))); // Set this as the administrator role. variable_set('user_admin_role', $devel_role->rid); // Assign user 1 the "developer" role. db_insert('users_roles') ->fields(array('uid' => 1, 'rid' => $devel_role->rid)) ->execute(); // Create a role for site content administrators, or managers. $admin_role = new stdClass(); $admin_role->name = 'content manager'; $admin_role->weight = 2; user_role_save($admin_role); // Additional permissions beyond what the authenticated user receives. user_role_grant_permissions($admin_role->rid, array( // redundant - 'access content overview', 'administer nodes', 'administer users', )); // Create a user for content administration assigned to the "content manager" role. $edit = array(); $edit['name'] = 'contentadmin'; $edit['mail'] = 'contentadmin@example.com'; $edit['roles'] = array($admin_role->rid => $admin_role->rid); $edit['pass'] = 'dream1939'; // Publication year of Finnegans Wake. $edit['status'] = 1; $account = user_save(drupal_anonymous_user(), $edit);
(Note: In our custom installation profile for migrating a site to Drupal, we used the content administrator's name and e-mail address. Not sure it would ever make sense to create a dummy user in an installation profile for distribution, maybe for testing, but more likely this would only make sense in an installation profile wizard that could gather information from the person installing.)
Then when it comes to creating a node attributed to this user, a couple tricks:
<?php
node_object_prepare($node);
// We have to set the uid here, because node_object_prepare() is a discrace.
$node->uid = $uid;
node_save($node);
?>
Anything else set in node_object_prepare() put to a custom setting will have to be likewise set after node_object_prepare() runs.
(If you're counting, one tip was to use node_object_prepare, and the other was how to work around the sad fact that it uses the global $user for setting the user ID.)
And the code to create a user was building the $edit array and then using user_save(drupal_anonymous_user(), $edit);
.
Comments
Post new comment