drupal/core/modules/datetime/datetime.module

160 lines
6.9 KiB
Plaintext

<?php
/**
* @file
* Field hooks to implement a simple datetime field.
*/
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Template\Attribute;
use Drupal\Core\Datetime\Element\Datetime;
use Drupal\node\NodeInterface;
/**
* Defines the timezone that dates should be stored in.
*/
const DATETIME_STORAGE_TIMEZONE = 'UTC';
/**
* Defines the format that date and time should be stored in.
*/
const DATETIME_DATETIME_STORAGE_FORMAT = 'Y-m-d\TH:i:s';
/**
* Defines the format that dates should be stored in.
*/
const DATETIME_DATE_STORAGE_FORMAT = 'Y-m-d';
/**
* Implements hook_help().
*/
function datetime_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.datetime':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('The Datetime module provides a Date field that stores dates and times. It also provides the Form API elements <em>datetime</em> and <em>datelist</em> for use in programming modules. See the <a href="!field">Field module help</a> and the <a href="!field_ui">Field UI module help</a> pages for general information on fields and how to create and manage them. For more information, see the <a href="!datetime_do">online documentation for the Datetime module</a>.', 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')) . '</p>';
$output .= '<h3>' . t('Uses') . '</h3>';
$output .= '<dl>';
$output .= '<dt>' . t('Managing and displaying date fields') . '</dt>';
$output .= '<dd>' . t('The <em>settings</em> and the <em>display</em> of the Date field can be configured separately. See the <a href="!field_ui">Field UI help</a> for more information on how to manage fields and their display.', array('!field_ui' => \Drupal::url('help.page', array('name' => 'field_ui')))) . '</dd>';
$output .= '<dt>' . t('Displaying dates') . '</dt>';
$output .= '<dd>' . t('Dates can be displayed using the <em>Plain</em> or the <em>Default</em> formatter. The <em>Plain</em> formatter displays the date in the <a href="http://en.wikipedia.org/wiki/ISO_8601">ISO 8601</a> format. If you choose the <em>Default</em> formatter, you can choose a format from a predefined list that can be managed on the <a href="!date_format_list">Date and time formats</a> page.', array('!date_format_list'=> \Drupal::url('system.date_format_list'))) . '</dd>';
$output .= '</dl>';
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_get_errors($form_state)) {
$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_get_errors($form_state)) {
$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());
}