You are here

function system_user_timezone in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/system.module \system_user_timezone()

Add the time zone field to the user edit and register forms.

2 calls to system_user_timezone()
system_form_user_form_alter in core/modules/system/system.module
Implements hook_form_FORM_ID_alter().
system_form_user_register_form_alter in core/modules/system/system.module
Implements hook_form_FORM_ID_alter().

File

core/modules/system/system.module, line 767
Configuration system that lets administrators modify the workings of the site.

Code

function system_user_timezone(&$form, FormStateInterface $form_state) {
  $user = \Drupal::currentUser();
  $account = $form_state
    ->getFormObject()
    ->getEntity();
  $form['timezone'] = array(
    '#type' => 'details',
    '#title' => t('Locale settings'),
    '#open' => TRUE,
    '#weight' => 6,
  );
  $form['timezone']['timezone'] = array(
    '#type' => 'select',
    '#title' => t('Time zone'),
    '#default_value' => $account
      ->getTimezone() ? $account
      ->getTimezone() : \Drupal::config('system.date')
      ->get('timezone.default'),
    '#options' => system_time_zones($account
      ->id() != $user
      ->id()),
    '#description' => t('Select the desired local time and time zone. Dates and times throughout this site will be displayed using this time zone.'),
  );
  $user_input = $form_state
    ->getUserInput();
  if (!$account
    ->getTimezone() && $account
    ->id() == $user
    ->id() && empty($user_input['timezone'])) {
    $form['timezone']['#attached']['library'][] = 'core/drupal.timezone';
    $form['timezone']['timezone']['#attributes'] = array(
      'class' => array(
        'timezone-detect',
      ),
    );
  }
}