getAllowedTimezones example

use Drupal\user\Entity\User;

/** * Defines a custom field item class for the 'timezone' user entity field. */
class TimeZoneItem extends StringItem {

  /** * {@inheritdoc} */
  public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
    $timezones = User::getAllowedTimezones();
    // We need to vary the selected timezones since we're generating a sample.     $key = rand(0, count($timezones) - 1);
    return $timezones[$key];
  }

}
ViewTestData::createTestViews(static::class['views_test_config']);

    // Bypass any field access.     $this->adminUser = User::create(['name' => $this->randomString()]);
    $this->adminUser->save();
    $this->container->get('current_user')->setAccount($this->adminUser);

    $this->testUsers = [];
    for ($i = 0; $i < 5; $i++) {
      $this->testUsers[$i] = User::create([
        'name' => 'test ' . $i,
        'timezone' => User::getAllowedTimezones()[$i],
        'created' => REQUEST_TIME - rand(0, 3600),
      ]);
      $this->testUsers[$i]->save();
    }

    // Setup a field storage and field, but also change the views data for the     // entity_test entity type.     $field_storage = FieldStorageConfig::create([
      'field_name' => 'field_test',
      'type' => 'integer',
      'entity_type' => 'entity_test',
    ]);
Home | Imprint | This part of the site doesn't use cookies.