Unverified Commit 941b5753 authored by Alex Pott's avatar Alex Pott
Browse files

perf: #3587601 Avoid loading date formats in element info

By: catch
By: dcam
By: godotislate
By: alexpott
parent 4081563a
Loading
Loading
Loading
Loading
Loading
+17 −20
Original line number Diff line number Diff line
@@ -23,20 +23,6 @@ class Datetime extends DateElementBase {
   * {@inheritdoc}
   */
  public function getInfo() {
    $date_format = '';
    $time_format = '';
    // Date formats cannot be loaded during install or update.
    if (!defined('MAINTENANCE_MODE')) {
      if ($date_format_entity = DateFormat::load('html_date')) {
        /** @var \Drupal\Core\Datetime\DateFormatInterface $date_format_entity */
        $date_format = $date_format_entity->getPattern();
      }
      if ($time_format_entity = DateFormat::load('html_time')) {
        /** @var \Drupal\Core\Datetime\DateFormatInterface $time_format_entity */
        $time_format = $time_format_entity->getPattern();
      }
    }

    // Note that since this information is cached, the #date_timezone property
    // is not set here, as this needs to vary potentially by-user.
    return [
@@ -54,10 +40,8 @@ public function getInfo() {
      ],
      '#theme' => 'datetime_form',
      '#theme_wrappers' => ['datetime_wrapper'],
      '#date_date_format' => $date_format,
      '#date_date_element' => 'date',
      '#date_date_callbacks' => [],
      '#date_time_format' => $time_format,
      '#date_time_element' => 'time',
      '#date_time_callbacks' => [],
      '#date_year_range' => '1900:2050',
@@ -69,7 +53,14 @@ public function getInfo() {
   * {@inheritdoc}
   */
  public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
    $element += ['#date_timezone' => date_default_timezone_get()];
    $formats = DateFormat::loadMultiple(['html_date', 'html_time']);
    $date_format = $formats['html_date']->getPattern();
    $time_format = $formats['html_time']->getPattern();
    $element += [
      '#date_timezone' => date_default_timezone_get(),
      '#date_date_format' => $date_format,
      '#date_time_format' => $time_format,
    ];

    if ($input !== FALSE) {
      if ($element['#date_date_element'] === 'datetime-local' && !empty($input['date'])) {
@@ -79,8 +70,6 @@ public static function valueCallback(&$element, $input, FormStateInterface $form
        // 'html_datetime' is not a valid format to pass to
        // DrupalDateTime::createFromFormat()
        [$date_input, $time_input] = explode('T', $input['date']);
        $date_format = DateFormat::load('html_date')->getPattern();
        $time_format = DateFormat::load('html_time')->getPattern();
      }
      else {
        $date_input = $element['#date_date_element'] != 'none' && !empty($input['date']) ? $input['date'] : '';
@@ -230,6 +219,14 @@ public static function valueCallback(&$element, $input, FormStateInterface $form
   * @see \Drupal\Core\Datetime\DateFormatterInterface::format()
   */
  public static function processDatetime(&$element, FormStateInterface $form_state, &$complete_form) {
    $formats = DateFormat::loadMultiple(['html_date', 'html_time']);
    $date_format = $formats['html_date']->getPattern();
    $time_format = $formats['html_time']->getPattern();

    $element += [
      '#date_date_format' => $date_format,
      '#date_time_format' => $time_format,
    ];
    $format_settings = [];
    // The value callback has populated the #value array.
    $date = !empty($element['#value']['object']) ? $element['#value']['object'] : NULL;
@@ -246,7 +243,7 @@ public static function processDatetime(&$element, FormStateInterface $form_state
      // a valid format.
      // @see https://www.drupal.org/project/drupal/issues/3505318
      if ($element['#date_date_element'] === 'datetime-local') {
        $date_format = DateFormat::load('html_date')->getPattern() . '\T' . DateFormat::load('html_time')->getPattern();
        $date_format = $formats['html_date']->getPattern() . '\T' . $formats['html_time']->getPattern();
      }
      $date_value = !empty($date) ? $date->format($date_format, $format_settings) : $element['#value']['date'];

+4 −4
Original line number Diff line number Diff line
@@ -126,10 +126,10 @@ protected function doTestNodePageAdministrator(): void {
    }, 'administratorNodePage');

    $expected = [
      'QueryCount' => 263,
      'CacheGetCount' => 253,
      'QueryCount' => 261,
      'CacheGetCount' => 251,
      'CacheGetCountByBin' => [
        'config' => 62,
        'config' => 60,
        'bootstrap' => 16,
        'discovery' => 75,
        'data' => 20,
@@ -139,7 +139,7 @@ protected function doTestNodePageAdministrator(): void {
        'render' => 13,
        'menu' => 22,
      ],
      'CacheSetCount' => 257,
      'CacheSetCount' => 255,
      'CacheDeleteCount' => 0,
      'CacheTagInvalidationCount' => 0,
      'CacheTagLookupQueryCount' => 28,
+3 −3
Original line number Diff line number Diff line
@@ -52,9 +52,9 @@ protected function testFrontPageColdCache(): void {
    $this->assertSession()->pageTextContains('Umami');

    $expected = [
      'QueryCount' => 183,
      'CacheGetCount' => 227,
      'CacheSetCount' => 237,
      'QueryCount' => 181,
      'CacheGetCount' => 225,
      'CacheSetCount' => 235,
      'CacheDeleteCount' => 0,
      'CacheTagLookupQueryCount' => 24,
      'CacheTagInvalidationCount' => 0,
+3 −3
Original line number Diff line number Diff line
@@ -55,9 +55,9 @@ protected function testNodePageColdCache(): void {
    $this->assertSession()->pageTextContains('quiche');

    $expected = [
      'QueryCount' => 199,
      'CacheGetCount' => 218,
      'CacheSetCount' => 222,
      'QueryCount' => 197,
      'CacheGetCount' => 216,
      'CacheSetCount' => 220,
      'CacheDeleteCount' => 0,
      'CacheTagLookupQueryCount' => 23,
      'CacheTagInvalidationCount' => 0,
+10 −0
Original line number Diff line number Diff line
@@ -28,6 +28,16 @@ class DatetimeElementFormTest extends KernelTestBase implements FormInterface, T
   */
  protected static $modules = ['datetime', 'system'];

  /**
   * {@inheritdoc}
   */
  public function setUp(): void {
    parent::setUp();

    $this->installEntitySchema('date_format');
    $this->installConfig(['system']);
  }

  /**
   * {@inheritdoc}
   */