You are here

public function SingleDateTimeBase::formatDefaultValue in Single DateTimePicker 8

Creates a date string for use as a default value.

This will take a default value, apply the proper timezone for display in a widget, and set the default time for date-only fields.

Parameters

object $date: The UTC default date.

string $timezone: The timezone to apply.

string $format: Date format to apply.

Return value

string String for use as a default value in a field widget.

2 calls to SingleDateTimeBase::formatDefaultValue()
SingleDateTimeRangeWidget::formElement in modules/single_datetime_range/src/Plugin/Field/FieldWidget/SingleDateTimeRangeWidget.php
Returns the form for a single field widget.
SingleDateTimeWidget::formElement in src/Plugin/Field/FieldWidget/SingleDateTimeWidget.php
Returns the form for a single field widget.

File

src/Plugin/Field/FieldWidget/SingleDateTimeBase.php, line 312

Class

SingleDateTimeBase
Base class for SingleDateTime widget types.

Namespace

Drupal\single_datetime\Plugin\Field\FieldWidget

Code

public function formatDefaultValue($date, $timezone, $format) {

  // The date was created and verified during field_load(), so it is safe to
  // use without further inspection.
  if ($this
    ->getFieldSetting('datetime_type') === DateTimeItem::DATETIME_TYPE_DATE) {

    // A date without time will pick up the current time, use the default
    // time.
    $date
      ->setDefaultDateTime();
  }
  $date
    ->setTimezone(new \DateTimeZone($timezone));

  // Format date.
  return $date
    ->format($format);
}