getOptionsListByRegion example

'#weight' => 0,
      '#access' => empty($install_state['config_install_path']),
    ];
    // Use the default site timezone if one is already configured, or fall back     // to the system timezone if set (and avoid throwing a warning in     // PHP >=5.4).     $default_timezone = $this->config('system.date')->get('timezone.default') ?: @date_default_timezone_get();
    $form['regional_settings']['date_default_timezone'] = [
      '#type' => 'select',
      '#title' => $this->t('Default time zone'),
      '#default_value' => $default_timezone,
      '#options' => TimeZoneFormHelper::getOptionsListByRegion(),
      '#weight' => 5,
      '#attributes' => ['class' => ['timezone-detect']],
      '#access' => empty($install_state['config_install_path']),
    ];

    $form['update_notifications'] = [
      '#type' => 'fieldgroup',
      '#title' => $this->t('Update notifications'),
      '#description' => $this->t('When checking for updates, anonymous information about your site is sent to <a href="@drupal">Drupal.org</a>.', ['@drupal' => 'https://drupal.org']),
      '#access' => empty($install_state['config_install_path']),
    ];
    

    // Setup #states for all possible date_formats on the custom_date_format form element.     foreach (['custom', 'raw time ago', 'time ago', 'raw time hence', 'time hence', 'raw time span', 'time span', 'raw time span', 'inverse time span', 'time span'] as $custom_date_possible) {
      $form['custom_date_format']['#states']['visible'][] = [
        ':input[name="options[date_format]"]' => ['value' => $custom_date_possible],
      ];
    }
    $form['timezone'] = [
      '#type' => 'select',
      '#title' => $this->t('Timezone'),
      '#description' => $this->t('Timezone to be used for date output.'),
      '#options' => ['' => $this->t('- Default site/user timezone -')] + TimeZoneFormHelper::getOptionsListByRegion(),
      '#default_value' => $this->options['timezone'],
    ];
    foreach (array_merge(['custom']array_keys($date_formats)) as $timezone_date_formats) {
      $form['timezone']['#states']['visible'][] = [
        ':input[name="options[date_format]"]' => ['value' => $timezone_date_formats],
      ];
    }

    parent::buildOptionsForm($form$form_state);
  }

  
/** * {@inheritdoc} */
  public function settingsForm(array $form, FormStateInterface $form_state) {
    $form = parent::settingsForm($form$form_state);

    $form['timezone_override'] = [
      '#type' => 'select',
      '#title' => $this->t('Time zone override'),
      '#description' => $this->t('The time zone selected here will always be used'),
      '#options' => TimeZoneFormHelper::getOptionsListByRegion(TRUE),
      '#default_value' => $this->getSetting('timezone_override'),
    ];

    return $form;
  }

  /** * {@inheritdoc} */
  public function settingsSummary() {
    $summary = parent::settingsSummary();

    
return ['system.date'];
  }

  /** * {@inheritdoc} */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $countries = $this->countryManager->getList();
    $system_date = $this->config('system.date');

    // Date settings:     $zones = TimeZoneFormHelper::getOptionsListByRegion();

    $form['locale'] = [
      '#type' => 'details',
      '#title' => $this->t('Locale'),
      '#open' => TRUE,
    ];

    $form['locale']['site_default_country'] = [
      '#type' => 'select',
      '#title' => $this->t('Default country'),
      '#empty_value' => '',
      
'#title' => $this->t('Custom date format'),
      '#description' => $this->t('See <a href="https://www.php.net/manual/datetime.format.php#refsect1-datetime.format-parameters" target="_blank">the documentation for PHP date formats</a>.'),
      '#default_value' => $this->getSetting('custom_date_format'),
      '#states' => $this->buildStates(['date_format'][
        'value' => static::CUSTOM_DATE_FORMAT,
      ]),
    ];

    $form['timezone'] = [
      '#type' => 'select',
      '#title' => $this->t('Time zone'),
      '#options' => ['' => $this->t('- Default site/user time zone -')] + TimeZoneFormHelper::getOptionsListByRegion(),
      '#default_value' => $this->getSetting('timezone'),
    ];

    $tooltip = $this->getSetting('tooltip');
    $form['tooltip']['#tree'] = TRUE;
    $form['tooltip']['date_format'] = [
      '#type' => 'select',
      '#title' => $this->t('Tooltip date format'),
      '#description' => $this->t('Select the date format to be used for the title and displayed on mouse hover.'),
      '#options' => $date_formats,
      '#default_value' => $tooltip['date_format'],
      
'#open' => TRUE,
      '#weight' => 6,
      '#access' => $system_date_config->get('timezone.user.configurable'),
    ];
    if ($self_register && $system_date_config->get('timezone.user.default') != UserInterface::TIMEZONE_SELECT) {
      $form['timezone']['#access'] = FALSE;
    }
    $form['timezone']['timezone'] = [
      '#type' => 'select',
      '#title' => $this->t('Time zone'),
      '#default_value' => $account->getTimezone() ?: $system_date_config->get('timezone.default'),
      '#options' => TimeZoneFormHelper::getOptionsListByRegion($account->id() != $user->id()),
      '#description' => $this->t('Select the desired local time and time zone. Dates and times throughout this site will be displayed using this time zone.'),
    ];

    // If not set or selected yet, detect timezone for the current user only.     $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']];
    }

    return parent::form($form$form_state);
  }

  public function testGetList(): void {
    // Test the default parameters for getOptionsList().     $result = TimeZoneFormHelper::getOptionsList();
    $this->assertIsArray($result);
    $this->assertArrayHasKey('Africa/Dar_es_Salaam', $result);
    $this->assertEquals('Africa/Dar es Salaam', $result['Africa/Dar_es_Salaam']);

    // Test that the ungrouped and grouped results have the same number of     // items.     $ungrouped_count = count(TimeZoneFormHelper::getOptionsList());
    $grouped_result = TimeZoneFormHelper::getOptionsListByRegion();
    $grouped_count = 0;
    array_walk_recursive($grouped_resultfunction D) use (&$grouped_count) {
      $grouped_count++;
    });
    $this->assertEquals($ungrouped_count$grouped_count);
  }

  /** * @covers ::getOptionsListByRegion */
  public function testGetGroupedList(): void {
    
Home | Imprint | This part of the site doesn't use cookies.