You are here

public function FormWidgetBase::createDefaultDateTime in Typed Data API enhancements 8

Create a default DrupalDateTime object.

This is used in the DateTimeWidget and DateTimeRangeWidget forms.

Parameters

string $date: (optional) A formatted date string as stored by the widgets. If no value is given an empty date with time 12:00 noon is created.

Return value

object A DrupalDateTime object with the required date and time values.

2 calls to FormWidgetBase::createDefaultDateTime()
DatetimeRangeWidget::form in src/Plugin/TypedDataFormWidget/DatetimeRangeWidget.php
Creates the widget's form elements for editing the given data.
DatetimeWidget::form in src/Plugin/TypedDataFormWidget/DatetimeWidget.php
Creates the widget's form elements for editing the given data.

File

src/Widget/FormWidgetBase.php, line 89

Class

FormWidgetBase
Base class for 'form widget' plugin implementations.

Namespace

Drupal\typed_data\Widget

Code

public function createDefaultDateTime($date) {
  if (!empty($date)) {
    $default = new DrupalDateTime($date);
  }
  else {

    // The DrupalDateTime object is created first with no parameters so that
    // it has the current users timezone. Then setDate with year 0 has the
    // effect that the widget date remains empty but allows a default time to
    // be set using setTime(). This is done in setDefaultDateTime().
    $default = new DrupalDateTime();
    $default
      ->setDate(0, 1, 1);
    $default
      ->setDefaultDateTime();
  }
  return $default;
}