' . t('About') . '';
$output .= '
' . t('The Datetime module provides a Date field that stores dates and times. It also provides the Form API elements datetime and datelist for use in programming modules. See the Field module help and the Field UI module help pages for general information on fields and how to create and manage them. For more information, see the online documentation for the Datetime module.', array('!field' => \Drupal::url('help.page', array('name' => 'field')), '!field_ui' => \Drupal::url('help.page', array('name' => 'field_ui')), '!datetime_do' => 'https://drupal.org/documentation/modules/datetime')) . '
';
$output .= '' . t('Uses') . '
';
$output .= '';
$output .= '- ' . t('Managing and displaying date fields') . '
';
$output .= '- ' . t('The settings and the display of the Date field can be configured separately. See the Field UI help for more information on how to manage fields and their display.', array('!field_ui' => \Drupal::url('help.page', array('name' => 'field_ui')))) . '
';
$output .= '- ' . t('Displaying dates') . '
';
$output .= '- ' . t('Dates can be displayed using the Plain or the Default formatter. The Plain formatter displays the date in the ISO 8601 format. If you choose the Default formatter, you can choose a format from a predefined list that can be managed on the Date and time formats page.', array('!date_format_list'=> \Drupal::url('system.date_format_list'))) . '
';
$output .= '
';
return $output;
}
}
/**
* Validation callback for the datetime widget element.
*
* The date has already been validated by the datetime form type validator and
* transformed to an date object. We just need to convert the date back to a the
* storage timezone and format.
*
* @param array $element
* The form element whose value is being validated.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*/
function datetime_datetime_widget_validate(&$element, FormStateInterface $form_state) {
if (!$form_state->getErrors()) {
$input_exists = FALSE;
$input = NestedArray::getValue($form_state->getValues(), $element['#parents'], $input_exists);
if ($input_exists) {
// The date should have been returned to a date object at this point by
// datetime_validate(), which runs before this.
if (!empty($input['value'])) {
$date = $input['value'];
if ($date instanceOf DrupalDateTime && !$date->hasErrors()) {
// If this is a date-only field, set it to the default time so the
// timezone conversion can be reversed.
if ($element['value']['#date_time_element'] == 'none') {
datetime_date_default_time($date);
}
// Adjust the date for storage.
$date->setTimezone(new \DateTimezone(DATETIME_STORAGE_TIMEZONE));
$value = $date->format($element['value']['#date_storage_format']);
form_set_value($element['value'], $value, $form_state);
}
}
}
}
}
/**
* Validation callback for the datelist widget element.
*
* The date has already been validated by the datetime form type validator and
* transformed to an date object. We just need to convert the date back to a the
* storage timezone and format.
*
* @param array $element
* The form element whose value is being validated.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*/
function datetime_datelist_widget_validate(&$element, FormStateInterface $form_state) {
if (!$form_state->getErrors()) {
$input_exists = FALSE;
$input = NestedArray::getValue($form_state->getValues(), $element['#parents'], $input_exists);
if ($input_exists) {
// The date should have been returned to a date object at this point by
// datetime_validate(), which runs before this.
if (!empty($input['value'])) {
$date = $input['value'];
if ($date instanceOf DrupalDateTime && !$date->hasErrors()) {
// If this is a date-only field, set it to the default time so the
// timezone conversion can be reversed.
if (!in_array('hour', $element['value']['#date_part_order'])) {
datetime_date_default_time($date);
}
// Adjust the date for storage.
$date->setTimezone(new \DateTimezone(DATETIME_STORAGE_TIMEZONE));
$value = $date->format($element['value']['#date_storage_format']);
form_set_value($element['value'], $value, $form_state);
}
}
}
}
}
/**
* Sets a consistent time on a date without time.
*
* The default time for a date without time can be anything, so long as it is
* consistently applied. If we use noon, dates in most timezones will have the
* same value for in both the local timezone and UTC.
*
* @param $date
*
*/
function datetime_date_default_time($date) {
$date->setTime(12, 0, 0);
}
/**
* Implements hook_form_BASE_FORM_ID_alter() for node forms.
*/
function datetime_form_node_form_alter(&$form, FormStateInterface $form_state, $form_id) {
// Alter the 'Authored on' date to use datetime.
$form['created']['#type'] = 'datetime';
$date_format = entity_load('date_format', 'html_date')->getPattern();
$time_format = entity_load('date_format', 'html_time')->getPattern();
$form['created']['#description'] = t('Format: %format. Leave blank to use the time of form submission.', array('%format' => Datetime::formatExample($date_format . ' ' . $time_format)));
unset($form['created']['#maxlength']);
}
/**
* Implements hook_node_prepare_form().
*/
function datetime_node_prepare_form(NodeInterface $node, $operation, FormStateInterface $form_state) {
// Prepare the 'Authored on' date to use datetime.
$node->date = DrupalDateTime::createFromTimestamp($node->getCreatedTime());
}