5

How can I modify the Drupal settings found on /admin/config/people/accounts (and other configuration pages) using a custom module?

For example, how do I rename the Anonymous user and change 'Who can register accounts' setting from 'administrator' to 'visitor' programmatically with a custom module?

1 Answer 1

9

Most of these configuration settings tend to be controlled by a variable, so Drupal uses variable_get() and variable_set() to retrieve and modify these values.

To find out the variable name and value you can have a look at the source page. For example:

<label for="edit-user-register">Who can register accounts? </label>
<div id="edit-user-register" class="form-radios">
  <input type="radio" id="edit-user-register-0" name="user_register" value="0" class="form-radio">  
  <label class="option" for="edit-user-register-0">Administrators only </label>

  <input type="radio" id="edit-user-register-1" name="user_register" value="1" class="form-radio">
  <label class="option" for="edit-user-register-1">Visitors </label>

  <input type="radio" id="edit-user-register-2" name="user_register" value="2" checked="checked" class="form-radio">
  <label class="option" for="edit-user-register-2">Visitors, but administrator approval is required </label>
</div>

Here you can see that in order to change the "Who can register accounts?" option, you need to change the value of the user_register variable. You can set it to 1 for visitors, so putting this code in your module will do the trick:

variable_set('user_register', 1);

Of course, this is if you're too lazy to open the source code :) . The other way is to find out which callback generates this form, and inspect user.module:

function user_menu() {
  //snip...
  $items['admin/config/people/accounts'] = array(
    'title' => 'Account settings',
    'description' => 'Configure default behavior of users, including registration requirements, e-mails, fields, and user pictures.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('user_admin_settings'),
    'access arguments' => array('administer users'),
    'file' => 'user.admin.inc',
    'weight' => -10,
  );

Then find the callback function user_admin_settings() in user.admin.inc:

$form['registration_cancellation']['user_register'] = array(
  '#type' => 'radios', 
  '#title' => t('Who can register accounts?'), 
  '#default_value' => variable_get('user_register', USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL), 
  '#options' => array(
    USER_REGISTER_ADMINISTRATORS_ONLY => t('Administrators only'), 
    USER_REGISTER_VISITORS => t('Visitors'), 
    USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL => t('Visitors, but administrator approval is required'),
  ),
);

So there you have your variable and in fact there's some constant to set up the values: USER_REGISTER_VISITORS.

3
  • variable_set('user_register', 1); nailed it. And thank you for the detailed notes on the callbacks. This will make finding other settings a snap. :)
    – Citricguy
    Commented Nov 3, 2011 at 1:58
  • Yep quite a lengthy explanation but I think it's quite useful, glad it helped you ;)
    – tostinni
    Commented Nov 3, 2011 at 2:07
  • 1
    "drush vset user_register [0,1,2]" comes in handy, too
    – Keith
    Commented Dec 3, 2012 at 16:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.