5

I want to unset this specific part of the settings form in template php. I found some code example for Drupal 6, but can't figure out how to do it in Drupal 7.

I tried this without result:

if ($form_id == 'user_profile_form') {
  unset($form['language']);
}

(There is a module "user tweaks" but I want it to do it in the preprocess)

2
  • 1
    If the user tweaks module does this, why not just look at the code of that module and copy the relevant part? Jan 9, 2013 at 11:43
  • Because I thought there might be a simple and standard solution.
    – cateye
    Jan 9, 2013 at 14:34

4 Answers 4

14

I created both the mentioned projects and I have maintained them for a while. (I stopped maintaining Drupal 6 and 7 modules.)

The code used in the Disable user language module is not correct: unset() should never be used for form elements, as this would cause problems to modules that try to access the form element.

The correct code is the following one. It would preferable if the code would be executed after other modules execute their own code.

/**
 * Implements hook_form_alter().
 */
function mytheme_form_alter(&$form, $form_state, $form_id) {
  // The language selector is only displayed if there is more than one language.
  if (drupal_multilingual()) {
    if ($form_id == 'user_register_form' || ($form_id == 'user_profile_form' && $form['#user_category'] == 'account')) {
      if (count(element_children($form['locale'])) > 1) {
        $form['locale']['language']['#access'] = FALSE;
      }
      else {
        $form['locale']['#access'] = FALSE;
      }
    }
  }
}

In my module I also used hook_init() to change $user->language to a default value; differently $user->language would have the last value saved.

/**
 * Implements hook_init().
 */
function dul_init() {
  if (!user_access('change own user language')) {
    global $user;

    $user->language = language_default('language');
  }
}

Clearly, the Disable user language module has its own permission, which allows to select which users have the permission to select their own language, but that part is not strictly necessary.

I used hook_init() because the documentation for hook_user_load() has the following warning:

Due to the static cache in user_load_multiple() you should not use this hook to modify the user properties returned by the {users} table itself since this may result in unreliable results when loading from cache.

The code is also changing on the fly a property for the currently logged-in user, without making any changes in the database. hook_user_load() would not serve this purpose, since it is invoked any time a module calls user_load().

8
  • Any idea about the module status of dul? If a co-maintainer would be found, would it be easy to bring this module back in shape?
    – donquixote
    Apr 4, 2016 at 6:29
  • And instead of hook_init(), would it not be better to use hook_user_load() for this?
    – donquixote
    Apr 4, 2016 at 6:32
  • I suggest to use === instead of ==.
    – donquixote
    Apr 4, 2016 at 6:42
  • if you want to maintain it, I can make you maintainer of that module. I don't maintain anymore any of the pre Drupal 8 modules. I used hook_init() because hook_user_load() cannot change any properties Drupal gets from its users table. There is a warning about that in the hook documentation.
    – apaderno
    Apr 4, 2016 at 7:23
  • You don't need === in the code I shown. I would rather use it if the value to check could be (for example) 0, FALSE, or a string, and I need to check it's FALSE.
    – apaderno
    Apr 4, 2016 at 7:25
5

I took a look at the "Disable user language" module.

This part of the code is indeed enough for me:

function template_form_user_profile_form_alter(&$form, $form_state) {
   unset($form['locale']);
}
1

For those who, for whatever reason don't/can't do PHP, a simple way to hide the language settings block in Drupal 7 is to add the following CSS to the user-edit page:

#user-profile-form fieldset#edit-locale
{
    display: none;
}

Having tried several methods of adding CSS to pages, my preferred method is the CSS Injector module (https://www.drupal.org/project/css_injector). I find it easy to use, effective, and a convenient place to maintain all of my injected CSS.

P.S. I realize this is an old post, but I read it looking for an answer, so I figure others will too.

4
  • Hi @Marshall, welcome to Drupal Answers. To improve your answer, could you include how you could add the css file without altering the core theme? Also double-check your code since you're mixing brackets and curly braces. Jan 28, 2017 at 20:28
  • Thanks @WimMostrey. I did the work on too small a screen to notice the typo. I'm fairly new to Drupal, so pretty much everything I know is based on the setup where I work. We use a custom theme, so altering that was fine. For someone who is just using a standard, core theme, I'm not certain how to do this. But I'll work on it. Jan 31, 2017 at 0:46
  • @Pierre.Vriens. I have edited the answer to reflect what I have found after research. Using the CSS Injector, there's no need to explain how to find which CSS files are loaded, since that's not necessary. Feb 7, 2017 at 17:31
  • Much better !!! Feb 7, 2017 at 17:34
0

In drupal 8 we can disable the language setting using the following code:

function digicare_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  if ($form_id === "user_register_form" || $form_id === "user_profile_form"){
    $form['language']['#access'] = FALSE;
  }
}

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.