The website has a normal registration form which allows users to register and the likes.
I have created a new custom form in which I am capturing the username/password etc. How do I manually register the user in Drupal system?
Thank You
Check out hook_user and user_save().
http://api.drupal.org/api/function/hook_user
http://api.drupal.org/api/function/user_save/6
Just expanding on Kevin's suggestion of hook_user and user_save here, the code might look something like:
// store user name
$user_name = "user123";
// store user email
$email = "user123#gmail.com";
// set up the user fields, use user_password function for 8 character password
$fields = array(
'name' => $user_name,
'mail' => $email,
'pass' => user_password(8),
'status' => 1,
);
// give new user roles if needed, as shown below
$fields['roles'] = array('new_role');
// pass the fields to user_save() and leave first param empty to create new user
$account = user_save('', $fields);
It's not the answer you want, but you'd save yourself a lot of trouble by using hook_form_alter to change the current registration form:
http://www.lullabot.com/articles/modifying-forms-drupal-5-and-6
Related
I have made a custom module for Drupal 7 that works with user data (has it's own db tables and classes that do some logic with these data). I want to show them on user's profile but I do not want to use drupal fields etc (I dont need another db tables to store the data)..
Simply put, on each user profile I need to instantiate new class from my module (e.g. new UserDataPresenter($user_id)) and on the profile itself I need to "inject" (?) some html/php data e.g.:
<?php $user = new UserDataPresenter($user_id); ?>
e.g. <b>User's apples: <?php echo $user->getUserApples() ?>
etc...
This would be shown at the top of the user profile, something like this:
Is this possible in Drupal 7 (without any external modules)
I just want to present the user relevant data on each user's profile page - that's it! Nothing more.
Thank you for your answer.
You could use the hook hook_user_view_alter()
function MYMODULE_user_view_alter(&$build) {
$build['#post_render'][] = 'MYMODULE_user_post_render';
}
function MYMODULE_user_post_render($arg1, $form) {
$user_id = $form['#account']->uid ;
$user = new UserDataPresenter($user_id);
return "<b>User's apples: ".$user->getUserApples() ;
}
Edit Instead of using #post_render, you could directly add an entry into the $build array :
function MYMODULE_user_view_alter(&$build) {
$uid = $build['#account']->uid;
$user = new UserDataPresenter($uid);
$build['my_html'] = [
'#markup' => '<b>User\'s apples: '.$user->getUserApples().'</b>',
'#weight' => 99,
] ;
}
Then use could #weight to place your markup at the place you want.
I have built a custom registration form using module_form_alter hook. I have also added the required new fields to the database with db_add_field. Now I'm able to add the values to the table in user registration/ user profile edit and the values are also getting stored in the database.. But what I'm not able to do is get the values that are stored in the database in the user profile edit form is displayed. Is there a hook to load the values from database to form on form load? Or is there any other way?
function customUser_schema_alter(&$schema) {
// Add field to existing schema.
$schema['users']['fields']['detail'] = array(
'type' => 'varchar',
'length' => 100,
);
}
function customUser_install() {
$schema = drupal_get_schema('users');
db_add_field('users', 'detail', $schema['fields']['detail']);
}
function customUser_form_alter(&$form, &$form_state, $form_id) {
// check to see if the form is the user registration or user profile form
// if not then return and don’t do anything
if (!($form_id == 'user_register_form' || $form_id == 'user_profile_form')) {
return;
}
$form['account']['detail'] = array(
'#type' => 'textfield',
'#title' => t('Additional Detail'),
);
}
A proper answer needs more details. I can only assume what you did.
You added fields to the {users} table. You didn't update the database schema which made drupal_write_record not be aware of the new fields, that being the reason they are not populated.
You created a new table {my_table} with the fields.
In both cases you need hook_user_insert()
/**
* Implements hook_user_insert().
*/
function mymodule_user_insert(&$edit, $account, $category) {
// Here you add the code to update the entry in {users} table,
// or int your custom table.
// $edit has the values from the form, $account->uid has the
// uid of the newly created user.
}
Note: If my first assumption is true that's not the drupal way to do it. You should have done the 2nd way instead. And even in that case use the hook_schema to create your table in mymodule.install instead of doing db_add_field().
For drupal 7 you could have uses the profile module (core) or profile2 to achieve that.
Based on that code
Try to change to this inside the form alter.
$account = $form['#user'];
$form['account']['detail'] = array(
'#type' => 'textfield',
'#title' => t('Additional Detail'),
'#default_value' => $account->detail,
);
I am using wp_create_user function to register new user in wordpress.
This function is inserting user as a role of subscriber. I want to insert users as a author type role. How can i do this.?
Thanks in advance.
The wp_create_user() function returns the ID of the user it creates. You can then use the WP_User class like:
$new_user_id = wp_create_user($your_args_here);
$u = new WP_User( $new_user_id );
// Remove role
$u->remove_role( 'subscriber' );
// Add role
$u->add_role( 'author' );
Alternatively, you can use wp_update_user() function:
Although I'm not precisely sure how to do that, but I imagine you could work it out by looking in wp-includes/registration.php which is where that function is created.
I have a site where some users will be registered by our staff, and won't have emails associated with them. I would like to keep the email field a required field, so I devised a random email generator.
function generateRandomEmail() {
$email = 'noemail'. rand(0,1000000) . '#noemail.com';
return $email;
}
So, I attached that to the user register form alter, and it worked nicely, effectively generating an email for these users.
However, in the process, all the other fields associated with the main account section (password, username, notify, etc.) disappeared. My question, is there a quick way to populate the rest of the fields that I don't want to alter? I've used drupal_render($form); in a tpl.php, but it didn't work in the form alter.
Here is where I'm altering the form:
function accountselect_user($op, &$edit, &$account, $category) {
if ($op == 'register') {
$fields['account']['mail'] = array(
'#type' => 'textfield',
'#default_value' => generateRandomEmail(),
);
You are currently using hook_user for your manipulation, but that is the wrong place. On $op 'registration', you can return additional fields you want to inject to the registration process, but not alter existing fields. Use hook_form_alter() or hook_form_FORM_ID_alter() for that, e.g.:
function yourModule_form_user_register_alter(&$form, &$form_state) {
$form['account']['mail']['#default_value'] = generateRandomEmail();
}
You probably want to add a check that the request is in fact coming from the staff, since the above code would prepopulate the email field for the normal registration form also!
Also, please do not generate 'random' mail addresses using existing third party domains (like 'nomail.com'). Use the reserved 'example.com', or better yet, one that you own yourself!
I have a module that implements hook_init.
Within that I have a case where I try to call user_save like save
user_save ('');
I figure this should work since I am not setting a uid, so it should create a new one. It also says that the $array should be allowed to be empty.
However, this always returns FALSE. ??
I have also tried setting these values in $array but this doesn't work either:
$foo = array(
'name' => 'the new user',
'mail' => 'the new user mail',
'pass' => user_password(),
'status' => 1,
);
$new_user = user_save ('', $foo);
Look at the documentation:
Parameters
$account The $user object for the user to modify or add. If $user->uid is omitted, a new user will be added.
$array (optional) An array of fields and values to save. For example, array('name' => 'My name'); Setting a field to NULL deletes it from the data column.
So when you call user_save() the first parameter must be a user object. If that user object doesn't have a uid, or is a string like in your case, a new user will be created. Here the 2nd parameter is necessary. It's not required to use the function, but as it holds all the values you want to save, you wont get far trying to create a user with no data at all. What use is a user without password, username etc. So a minimum of data is required as well, so Drupal will know what to save.