system_user_timezone(&$form, FormStateInterface $form_state)
Add the time zone field to the user edit and register forms.
File
- core/modules/system/system.module, line 800
 - 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'));
  }
}
Please login to continue.