Unverified Commit 53bea0b5 authored by Alex Pott's avatar Alex Pott
Browse files

fix: #3545132 Datelist element has nondeterministic timezone

By: luke.leber
By: bkosborne
(cherry picked from commit 6e1cc4a0)
parent 6f4f16ae
Loading
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -24,6 +24,8 @@ class Datelist extends DateElementBase {
   * {@inheritdoc}
   */
  public function getInfo() {
    // Note that since this information is cached, the #date_timezone property
    // is not set here, as this needs to vary potentially by-user.
    return [
      '#input' => TRUE,
      '#element_validate' => [
@@ -38,7 +40,6 @@ public function getInfo() {
      '#date_year_range' => '1900:2050',
      '#date_increment' => 1,
      '#date_date_callbacks' => [],
      '#date_timezone' => date_default_timezone_get(),
    ];
  }

@@ -49,6 +50,8 @@ public function getInfo() {
   * If the date is valid, the date is set in the form.
   */
  public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
    $element += ['#date_timezone' => date_default_timezone_get()];

    $parts = $element['#date_part_order'];
    $increment = $element['#date_increment'];

+17 −0
Original line number Diff line number Diff line
@@ -125,6 +125,23 @@ public function testDatelistElementUntrustedCallbacks() : void {
    \Drupal::formBuilder()->getForm($this, 'datelistDateCallback');
  }

  /**
   * Tests proper timezone handling of the Datelist element.
   */
  public function testTimezoneHandling(): void {
    // Render the form once with the site's timezone.
    $form = \Drupal::formBuilder()->getForm($this);
    $this->render($form);
    $this->assertEquals('Australia/Sydney', $form['datelist_element']['#date_timezone']);

    // Mimic a user with a different timezone than Australia/Sydney.
    date_default_timezone_set('UTC');

    $form = \Drupal::formBuilder()->getForm($this);
    $this->render($form);
    $this->assertEquals('UTC', $form['datelist_element']['#date_timezone']);
  }

  /**
   * {@inheritdoc}
   */