You are here

function date_default_value in Date 8

Same name and namespace in other branches
  1. 5 date.module \date_default_value()
  2. 7.3 date_elements.inc \date_default_value()
  3. 7.2 date_elements.inc \date_default_value()

The callback for setting a default value for an empty date field.

3 string references to 'date_default_value'
DateFieldWidgetBase::defaultValueFunction in date_field/lib/Drupal/date_field/Plugin/field/widget/DateFieldWidgetBase.php
Return the callback used to set a date default value.
DateTextWidget::__construct in lib/Drupal/date/Plugin/field/widget/DateTextWidget.php
Constructs a DateWidget object.
DateWidgetBase::defaultValueFunction in lib/Drupal/date/Plugin/field/widget/DateWidgetBase.php
Return the callback used to set a date default value.

File

./date.module, line 545
Defines date/time field types.

Code

function date_default_value($entity_type, $entity, $field, $instance, $langcode) {
  $item = array();
  $db_format = date_type_format($field['type']);
  $date = date_default_value_part($item, $field, $instance, $langcode, 'value');
  $item[0]['value'] = $date instanceof DrupalDateTime ? date_format($date, $db_format) : '';
  if (!empty($field['settings']['todate'])) {
    $date = date_default_value_part($item, $field, $instance, $langcode, 'value2');
    $item[0]['value2'] = $date instanceof DrupalDateTime ? date_format($date, $db_format) : '';
  }

  // Make sure the default value has the same construct as a loaded field value
  // to avoid errors if the default value is used on a hidden element.
  $item[0]['timezone'] = date_get_timezone($field['settings']['tz_handling']);
  $item[0]['timezone_db'] = date_get_timezone_db($field['settings']['tz_handling']);
  $item[0]['date_type'] = $field['type'];
  if (!isset($item[0]['value2'])) {
    $item[0]['value2'] = $item[0]['value'];
  }
  return $item;
}