function system_user_timezone

Same name in other branches
  1. 7.x modules/system/system.module \system_user_timezone()

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

@internal This functions exists only to be used by System module's form alters which have been removed.

See also

https://www.drupal.org/node/3113062

File

core/modules/system/system.module, line 827

Code

function system_user_timezone(&$form, FormStateInterface $form_state) {
    $user = \Drupal::currentUser();
    $account = $form_state->getFormObject()
        ->getEntity();
    $form['timezone'] = [
        '#type' => 'details',
        '#title' => t('Locale settings'),
        '#open' => TRUE,
        '#weight' => 6,
    ];
    $form['timezone']['timezone'] = [
        '#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(), TRUE),
        '#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'] = [
            'class' => [
                'timezone-detect',
            ],
        ];
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.