function DateFormatter::dateFormat

Same name in other branches
  1. 9 core/lib/Drupal/Core/Datetime/DateFormatter.php \Drupal\Core\Datetime\DateFormatter::dateFormat()
  2. 8.9.x core/lib/Drupal/Core/Datetime/DateFormatter.php \Drupal\Core\Datetime\DateFormatter::dateFormat()
  3. 10 core/lib/Drupal/Core/Datetime/DateFormatter.php \Drupal\Core\Datetime\DateFormatter::dateFormat()

Loads the given format pattern for the given langcode.

Parameters

string $type: The machine name of the date format type which is one of:

  • One of the built-in date format types: 'short', 'medium', 'long', 'html_datetime', 'html_date', 'html_time', 'html_yearless_date', 'html_week', 'html_month', 'html_year'.
  • The name of a date format type defined by a date format config entity.
  • The machine name of an administrator-defined date format type.
  • 'custom' for a custom date format type.

string $langcode: The langcode of the language to use.

Return value

\Drupal\Core\Datetime\DateFormatInterface|null The configuration entity for the date format in the given language for non-custom formats, NULL otherwise.

1 call to DateFormatter::dateFormat()
DateFormatter::format in core/lib/Drupal/Core/Datetime/DateFormatter.php
Formats a date, using a date type or a custom date format string.

File

core/lib/Drupal/Core/Datetime/DateFormatter.php, line 333

Class

DateFormatter
Provides a service to handle various date related functionality.

Namespace

Drupal\Core\Datetime

Code

protected function dateFormat($type, $langcode) {
    if (!isset($this->dateFormats[$type][$langcode])) {
        $original_language = $this->languageManager
            ->getConfigOverrideLanguage();
        $this->languageManager
            ->setConfigOverrideLanguage(new Language([
            'id' => $langcode,
        ]));
        $this->dateFormats[$type][$langcode] = $this->dateFormatStorage
            ->load($type);
        $this->languageManager
            ->setConfigOverrideLanguage($original_language);
    }
    return $this->dateFormats[$type][$langcode];
}

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