2

I am attempting to override the user registration from at user/register to allow users to select their own language upon login.

Based on this helpful answer, I came up with this override:

function hook_form_user_register_form_alter(&$form, &$form_state, $form_id) {

  $form['locale']['language'] = array(
    '#type' => (count($names) <= 5 ? 'radios' : 'select'),
    '#title' => t('Language'),
    '#default_value' => $user_preferred_language->language,
    '#options' => $names,
    '#description' => $mode ? t("This account's default language for e-mails, and preferred language for site presentation.") : t("This account's default language for e-mails."),
  );

However, it doesn't work because of the following code in locale.module:

  $form['locale'] = array(
    '#type' => 'fieldset',
    '#title' => t('Language settings'),
    '#weight' => 1,
    '#access' => ($form['#user_category'] == 'account' || ($form['#user_category'] == 'register' && user_access('administer users'))),
  );

Simply deleting && user_access('administer users') from locale.module and clearing the cache makes it work, but if I do that I'm hacking core.

How can I remove the bit about && user_access('administer users') without hacking core?

5

You should be able to just replace the #access property for the element added by the locale module in your form alter:

function MYMODULE_form_user_register_form_alter(&$form, &$form_state, $form_id) {
  // Use exactly the same access logic as the original, 
  // without checking for the 'administer users' permission
  $form['locale']['#access'] = ($form['#user_category'] == 'account' || ($form['#user_category'] == 'register'));
}

That's essentially the same as taking the && user_access('administer users') part out of the core file.

All you need to make sure of is that your module hooks are run after those of the locale module (so the element actually exists when you try to alter it), which you can see how to do here.

  • Thanks! In this case, I didn't need to set the weight, but it's very helpful to know I may need to do that in the future. – Patrick Kenny Mar 31 '12 at 15:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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