You are here

protected function SmartDateDefaultFormatter::formatDate in Smart Date 8

Creates a formatted date value as a string.

Parameters

object $date: A date object.

Return value

string A formatted date string using the chosen format.

Overrides DateTimeDefaultFormatter::formatDate

File

src/Plugin/Field/FieldFormatter/SmartDateDefaultFormatter.php, line 41

Class

SmartDateDefaultFormatter
Plugin implementation of the 'Default' formatter for 'smartdate' fields.

Namespace

Drupal\smart_date\Plugin\Field\FieldFormatter

Code

protected function formatDate($date) {

  // Try to load the format from the field settings. If that doesn't work, use
  // the default format, which cannot be deleted.
  $format = static::loadSmartDateFormat($this
    ->getSetting('format')) ?: static::loadSmartDateFormat('default');

  // If a timezone override is set, get its machine name.
  $timezone = $this
    ->getSetting('timezone_override') ?: $date
    ->getTimezone()
    ->getName();

  // This (formatDate()) function only formats one date, so we pass the same
  // date to both the start and end dates of SmartDateTrait::formatSmartDate()
  // which will only display one date.
  return static::formatSmartDate($date
    ->getTimestamp(), $date
    ->getTimestamp(), $format
    ->getOptions(), $timezone, 'string');
}