updated core and modules
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace Drupal\datetime;
|
||||
|
||||
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
|
||||
use Drupal\Core\Datetime\DrupalDateTime;
|
||||
use Drupal\Core\TypedData\DataDefinitionInterface;
|
||||
use Drupal\Core\TypedData\TypedDataInterface;
|
||||
@@ -40,14 +41,28 @@ class DateTimeComputed extends TypedData {
|
||||
return $this->date;
|
||||
}
|
||||
|
||||
/** @var \Drupal\Core\Field\FieldItemInterface $item */
|
||||
$item = $this->getParent();
|
||||
$value = $item->{($this->definition->getSetting('date source'))};
|
||||
|
||||
$storage_format = $item->getFieldDefinition()->getSetting('datetime_type') == 'date' ? DATETIME_DATE_STORAGE_FORMAT : DATETIME_DATETIME_STORAGE_FORMAT;
|
||||
$datetime_type = $item->getFieldDefinition()->getSetting('datetime_type');
|
||||
$storage_format = $datetime_type === DateTimeItem::DATETIME_TYPE_DATE ? DATETIME_DATE_STORAGE_FORMAT : DATETIME_DATETIME_STORAGE_FORMAT;
|
||||
try {
|
||||
$date = DrupalDateTime::createFromFormat($storage_format, $value, DATETIME_STORAGE_TIMEZONE);
|
||||
if ($date instanceof DrupalDateTime && !$date->hasErrors()) {
|
||||
$this->date = $date;
|
||||
// If the format did not include an explicit time portion, then the
|
||||
// time will be set from the current time instead. For consistency, we
|
||||
// set the time to 12:00:00 UTC for date-only fields. This is used so
|
||||
// that the local date portion is the same, across nearly all time
|
||||
// zones.
|
||||
// @see datetime_date_default_time()
|
||||
// @see http://php.net/manual/en/datetime.createfromformat.php
|
||||
// @todo Update comment and/or code per the chosen solution in
|
||||
// https://www.drupal.org/node/2830094
|
||||
if ($datetime_type === DateTimeItem::DATETIME_TYPE_DATE) {
|
||||
$this->date->setTime(12, 0, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (\Exception $e) {
|
||||
|
||||
@@ -23,16 +23,16 @@ class DateTimeCustomFormatter extends DateTimeFormatterBase {
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function defaultSettings() {
|
||||
return array(
|
||||
return [
|
||||
'date_format' => DATETIME_DATETIME_STORAGE_FORMAT,
|
||||
) + parent::defaultSettings();
|
||||
] + parent::defaultSettings();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function viewElements(FieldItemListInterface $items, $langcode) {
|
||||
$elements = array();
|
||||
$elements = [];
|
||||
|
||||
foreach ($items as $delta => $item) {
|
||||
$output = '';
|
||||
@@ -76,12 +76,12 @@ class DateTimeCustomFormatter extends DateTimeFormatterBase {
|
||||
public function settingsForm(array $form, FormStateInterface $form_state) {
|
||||
$form = parent::settingsForm($form, $form_state);
|
||||
|
||||
$form['date_format'] = array(
|
||||
$form['date_format'] = [
|
||||
'#type' => 'textfield',
|
||||
'#title' => $this->t('Date/time format'),
|
||||
'#description' => $this->t('See <a href="http://php.net/manual/function.date.php" target="_blank">the documentation for PHP date formats</a>.'),
|
||||
'#default_value' => $this->getSetting('date_format'),
|
||||
);
|
||||
];
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
+12
-12
@@ -23,16 +23,16 @@ class DateTimeDefaultFormatter extends DateTimeFormatterBase {
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function defaultSettings() {
|
||||
return array(
|
||||
return [
|
||||
'format_type' => 'medium',
|
||||
) + parent::defaultSettings();
|
||||
] + parent::defaultSettings();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function viewElements(FieldItemListInterface $items, $langcode) {
|
||||
$elements = array();
|
||||
$elements = [];
|
||||
|
||||
foreach ($items as $delta => $item) {
|
||||
$output = '';
|
||||
@@ -56,7 +56,7 @@ class DateTimeDefaultFormatter extends DateTimeFormatterBase {
|
||||
}
|
||||
|
||||
// Display the date using theme datetime.
|
||||
$elements[$delta] = array(
|
||||
$elements[$delta] = [
|
||||
'#cache' => [
|
||||
'contexts' => [
|
||||
'timezone',
|
||||
@@ -65,10 +65,10 @@ class DateTimeDefaultFormatter extends DateTimeFormatterBase {
|
||||
'#theme' => 'time',
|
||||
'#text' => $output,
|
||||
'#html' => FALSE,
|
||||
'#attributes' => array(
|
||||
'#attributes' => [
|
||||
'datetime' => $iso_date,
|
||||
),
|
||||
);
|
||||
],
|
||||
];
|
||||
if (!empty($item->_attributes)) {
|
||||
$elements[$delta]['#attributes'] += $item->_attributes;
|
||||
// Unset field item attributes since they have been included in the
|
||||
@@ -86,7 +86,7 @@ class DateTimeDefaultFormatter extends DateTimeFormatterBase {
|
||||
*/
|
||||
protected function formatDate($date) {
|
||||
$format_type = $this->getSetting('format_type');
|
||||
$timezone = $this->getSetting('timezone_override');
|
||||
$timezone = $this->getSetting('timezone_override') ?: $date->getTimezone()->getName();
|
||||
return $this->dateFormatter->format($date->getTimestamp(), $format_type, '', $timezone != '' ? $timezone : NULL);
|
||||
}
|
||||
|
||||
@@ -100,17 +100,17 @@ class DateTimeDefaultFormatter extends DateTimeFormatterBase {
|
||||
$format_types = $this->dateFormatStorage->loadMultiple();
|
||||
$options = [];
|
||||
foreach ($format_types as $type => $type_info) {
|
||||
$format = $this->dateFormatter->format($time->format('U'), $type);
|
||||
$format = $this->dateFormatter->format($time->getTimestamp(), $type);
|
||||
$options[$type] = $type_info->label() . ' (' . $format . ')';
|
||||
}
|
||||
|
||||
$form['format_type'] = array(
|
||||
$form['format_type'] = [
|
||||
'#type' => 'select',
|
||||
'#title' => t('Date format'),
|
||||
'#description' => t("Choose a format for displaying the date. Be sure to set a format appropriate for the field, i.e. omitting time for a field that only has a date."),
|
||||
'#options' => $options,
|
||||
'#default_value' => $this->getSetting('format_type'),
|
||||
);
|
||||
];
|
||||
|
||||
return $form;
|
||||
}
|
||||
@@ -122,7 +122,7 @@ class DateTimeDefaultFormatter extends DateTimeFormatterBase {
|
||||
$summary = parent::settingsSummary();
|
||||
|
||||
$date = new DrupalDateTime();
|
||||
$summary[] = t('Format: @display', array('@display' => $this->formatDate($date, $this->getFormatSettings())));
|
||||
$summary[] = t('Format: @display', ['@display' => $this->formatDate($date, $this->getFormatSettings())]);
|
||||
|
||||
return $summary;
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ use Drupal\Core\Field\FieldDefinitionInterface;
|
||||
use Drupal\Core\Field\FormatterBase;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
|
||||
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
|
||||
|
||||
@@ -81,9 +82,9 @@ abstract class DateTimeFormatterBase extends FormatterBase implements ContainerF
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function defaultSettings() {
|
||||
return array(
|
||||
return [
|
||||
'timezone_override' => '',
|
||||
) + parent::defaultSettings();
|
||||
] + parent::defaultSettings();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -92,13 +93,13 @@ abstract class DateTimeFormatterBase extends FormatterBase implements ContainerF
|
||||
public function settingsForm(array $form, FormStateInterface $form_state) {
|
||||
$form = parent::settingsForm($form, $form_state);
|
||||
|
||||
$form['timezone_override'] = array(
|
||||
$form['timezone_override'] = [
|
||||
'#type' => 'select',
|
||||
'#title' => $this->t('Time zone override'),
|
||||
'#description' => $this->t('The time zone selected here will always be used'),
|
||||
'#options' => system_time_zones(TRUE),
|
||||
'#default_value' => $this->getSetting('timezone_override'),
|
||||
);
|
||||
];
|
||||
|
||||
return $form;
|
||||
}
|
||||
@@ -110,7 +111,7 @@ abstract class DateTimeFormatterBase extends FormatterBase implements ContainerF
|
||||
$summary = parent::settingsSummary();
|
||||
|
||||
if ($override = $this->getSetting('timezone_override')) {
|
||||
$summary[] = $this->t('Time zone: @timezone', array('@timezone' => $override));
|
||||
$summary[] = $this->t('Time zone: @timezone', ['@timezone' => $override]);
|
||||
}
|
||||
|
||||
return $summary;
|
||||
@@ -140,7 +141,14 @@ abstract class DateTimeFormatterBase extends FormatterBase implements ContainerF
|
||||
* A DrupalDateTime object.
|
||||
*/
|
||||
protected function setTimeZone(DrupalDateTime $date) {
|
||||
$date->setTimeZone(timezone_open(drupal_get_user_timezone()));
|
||||
if ($this->getFieldSetting('datetime_type') === DateTimeItem::DATETIME_TYPE_DATE) {
|
||||
// A date without time has no timezone conversion.
|
||||
$timezone = DATETIME_STORAGE_TIMEZONE;
|
||||
}
|
||||
else {
|
||||
$timezone = drupal_get_user_timezone();
|
||||
}
|
||||
$date->setTimeZone(timezone_open($timezone));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace Drupal\datetime\Plugin\Field\FieldFormatter;
|
||||
|
||||
use Drupal\Core\Field\FieldItemListInterface;
|
||||
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
|
||||
|
||||
/**
|
||||
* Plugin implementation of the 'Plain' formatter for 'datetime' fields.
|
||||
@@ -21,7 +22,7 @@ class DateTimePlainFormatter extends DateTimeFormatterBase {
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function viewElements(FieldItemListInterface $items, $langcode) {
|
||||
$elements = array();
|
||||
$elements = [];
|
||||
|
||||
foreach ($items as $delta => $item) {
|
||||
$output = '';
|
||||
@@ -33,8 +34,6 @@ class DateTimePlainFormatter extends DateTimeFormatterBase {
|
||||
// A date without time will pick up the current time, use the default.
|
||||
datetime_date_default_time($date);
|
||||
}
|
||||
else {
|
||||
}
|
||||
$this->setTimeZone($date);
|
||||
|
||||
$output = $this->formatDate($date);
|
||||
@@ -56,7 +55,7 @@ class DateTimePlainFormatter extends DateTimeFormatterBase {
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function formatDate($date) {
|
||||
$format = $this->getFieldSetting('datetime_type') == 'date' ? DATETIME_DATE_STORAGE_FORMAT : DATETIME_DATETIME_STORAGE_FORMAT;
|
||||
$format = $this->getFieldSetting('datetime_type') == DateTimeItem::DATETIME_TYPE_DATE ? DATETIME_DATE_STORAGE_FORMAT : DATETIME_DATETIME_STORAGE_FORMAT;
|
||||
$timezone = $this->getSetting('timezone_override');
|
||||
return $this->dateFormatter->format($date->getTimestamp(), 'custom', $format, $timezone != '' ? $timezone : NULL);
|
||||
}
|
||||
|
||||
+11
-11
@@ -74,11 +74,11 @@ class DateTimeTimeAgoFormatter extends FormatterBase implements ContainerFactory
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function defaultSettings() {
|
||||
$settings = array(
|
||||
$settings = [
|
||||
'future_format' => '@interval hence',
|
||||
'past_format' => '@interval ago',
|
||||
'granularity' => 2,
|
||||
) + parent::defaultSettings();
|
||||
] + parent::defaultSettings();
|
||||
|
||||
return $settings;
|
||||
}
|
||||
@@ -104,7 +104,7 @@ class DateTimeTimeAgoFormatter extends FormatterBase implements ContainerFactory
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function viewElements(FieldItemListInterface $items, $langcode) {
|
||||
$elements = array();
|
||||
$elements = [];
|
||||
|
||||
foreach ($items as $delta => $item) {
|
||||
$date = $item->date;
|
||||
@@ -128,26 +128,26 @@ class DateTimeTimeAgoFormatter extends FormatterBase implements ContainerFactory
|
||||
public function settingsForm(array $form, FormStateInterface $form_state) {
|
||||
$form = parent::settingsForm($form, $form_state);
|
||||
|
||||
$form['future_format'] = array(
|
||||
$form['future_format'] = [
|
||||
'#type' => 'textfield',
|
||||
'#title' => $this->t('Future format'),
|
||||
'#default_value' => $this->getSetting('future_format'),
|
||||
'#description' => $this->t('Use <em>@interval</em> where you want the formatted interval text to appear.'),
|
||||
);
|
||||
];
|
||||
|
||||
$form['past_format'] = array(
|
||||
$form['past_format'] = [
|
||||
'#type' => 'textfield',
|
||||
'#title' => $this->t('Past format'),
|
||||
'#default_value' => $this->getSetting('past_format'),
|
||||
'#description' => $this->t('Use <em>@interval</em> where you want the formatted interval text to appear.'),
|
||||
);
|
||||
];
|
||||
|
||||
$form['granularity'] = array(
|
||||
$form['granularity'] = [
|
||||
'#type' => 'number',
|
||||
'#title' => $this->t('Granularity'),
|
||||
'#default_value' => $this->getSetting('granularity'),
|
||||
'#description' => $this->t('How many time units should be shown in the formatted output.'),
|
||||
);
|
||||
];
|
||||
|
||||
return $form;
|
||||
}
|
||||
@@ -160,8 +160,8 @@ class DateTimeTimeAgoFormatter extends FormatterBase implements ContainerFactory
|
||||
|
||||
$future_date = new DrupalDateTime('1 year 1 month 1 week 1 day 1 hour 1 minute');
|
||||
$past_date = new DrupalDateTime('-1 year -1 month -1 week -1 day -1 hour -1 minute');
|
||||
$summary[] = t('Future date: %display', array('%display' => $this->formatDate($future_date)));
|
||||
$summary[] = t('Past date: %display', array('%display' => $this->formatDate($past_date)));
|
||||
$summary[] = t('Future date: %display', ['%display' => $this->formatDate($future_date)]);
|
||||
$summary[] = t('Past date: %display', ['%display' => $this->formatDate($past_date)]);
|
||||
|
||||
return $summary;
|
||||
}
|
||||
|
||||
@@ -30,31 +30,31 @@ class DateTimeFieldItemList extends FieldItemList {
|
||||
if (empty($this->getFieldDefinition()->getDefaultValueCallback())) {
|
||||
$default_value = $this->getFieldDefinition()->getDefaultValueLiteral();
|
||||
|
||||
$element = array(
|
||||
'#parents' => array('default_value_input'),
|
||||
'default_date_type' => array(
|
||||
$element = [
|
||||
'#parents' => ['default_value_input'],
|
||||
'default_date_type' => [
|
||||
'#type' => 'select',
|
||||
'#title' => t('Default date'),
|
||||
'#description' => t('Set a default value for this date.'),
|
||||
'#default_value' => isset($default_value[0]['default_date_type']) ? $default_value[0]['default_date_type'] : '',
|
||||
'#options' => array(
|
||||
'#options' => [
|
||||
static::DEFAULT_VALUE_NOW => t('Current date'),
|
||||
static::DEFAULT_VALUE_CUSTOM => t('Relative date'),
|
||||
),
|
||||
],
|
||||
'#empty_value' => '',
|
||||
),
|
||||
'default_date' => array(
|
||||
],
|
||||
'default_date' => [
|
||||
'#type' => 'textfield',
|
||||
'#title' => t('Relative default value'),
|
||||
'#description' => t("Describe a time by reference to the current day, like '+90 days' (90 days from the day the field is created) or '+1 Saturday' (the next Saturday). See <a href=\"http://php.net/manual/function.strtotime.php\">strtotime</a> for more details."),
|
||||
'#default_value' => (isset($default_value[0]['default_date_type']) && $default_value[0]['default_date_type'] == static::DEFAULT_VALUE_CUSTOM) ? $default_value[0]['default_date'] : '',
|
||||
'#states' => array(
|
||||
'visible' => array(
|
||||
':input[id="edit-default-value-input-default-date-type"]' => array('value' => static::DEFAULT_VALUE_CUSTOM),
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
'#states' => [
|
||||
'visible' => [
|
||||
':input[id="edit-default-value-input-default-date-type"]' => ['value' => static::DEFAULT_VALUE_CUSTOM],
|
||||
]
|
||||
]
|
||||
]
|
||||
];
|
||||
|
||||
return $element;
|
||||
}
|
||||
@@ -65,7 +65,7 @@ class DateTimeFieldItemList extends FieldItemList {
|
||||
*/
|
||||
public function defaultValuesFormValidate(array $element, array &$form, FormStateInterface $form_state) {
|
||||
if ($form_state->getValue(['default_value_input', 'default_date_type']) == static::DEFAULT_VALUE_CUSTOM) {
|
||||
$is_strtotime = @strtotime($form_state->getValue(array('default_value_input', 'default_date')));
|
||||
$is_strtotime = @strtotime($form_state->getValue(['default_value_input', 'default_date']));
|
||||
if (!$is_strtotime) {
|
||||
$form_state->setErrorByName('default_value_input][default_date', t('The relative date value entered is invalid.'));
|
||||
}
|
||||
@@ -76,13 +76,13 @@ class DateTimeFieldItemList extends FieldItemList {
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function defaultValuesFormSubmit(array $element, array &$form, FormStateInterface $form_state) {
|
||||
if ($form_state->getValue(array('default_value_input', 'default_date_type'))) {
|
||||
if ($form_state->getValue(array('default_value_input', 'default_date_type')) == static::DEFAULT_VALUE_NOW) {
|
||||
if ($form_state->getValue(['default_value_input', 'default_date_type'])) {
|
||||
if ($form_state->getValue(['default_value_input', 'default_date_type']) == static::DEFAULT_VALUE_NOW) {
|
||||
$form_state->setValueForElement($element['default_date'], static::DEFAULT_VALUE_NOW);
|
||||
}
|
||||
return array($form_state->getValue('default_value_input'));
|
||||
return [$form_state->getValue('default_value_input')];
|
||||
}
|
||||
return array();
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -92,20 +92,28 @@ class DateTimeFieldItemList extends FieldItemList {
|
||||
$default_value = parent::processDefaultValue($default_value, $entity, $definition);
|
||||
|
||||
if (isset($default_value[0]['default_date_type'])) {
|
||||
// A default value should be in the format and timezone used for date
|
||||
// storage.
|
||||
$date = new DrupalDateTime($default_value[0]['default_date'], DATETIME_STORAGE_TIMEZONE);
|
||||
$storage_format = $definition->getSetting('datetime_type') == DateTimeItem::DATETIME_TYPE_DATE ? DATETIME_DATE_STORAGE_FORMAT : DATETIME_DATETIME_STORAGE_FORMAT;
|
||||
$value = $date->format($storage_format);
|
||||
if ($definition->getSetting('datetime_type') === DateTimeItem::DATETIME_TYPE_DATE) {
|
||||
// A default date only value should be in the format used for date
|
||||
// storage but in the user's local timezone.
|
||||
$date = new DrupalDateTime($default_value[0]['default_date'], drupal_get_user_timezone());
|
||||
$format = DATETIME_DATE_STORAGE_FORMAT;
|
||||
}
|
||||
else {
|
||||
// A default date+time value should be in the format and timezone used
|
||||
// for date storage.
|
||||
$date = new DrupalDateTime($default_value[0]['default_date'], DATETIME_STORAGE_TIMEZONE);
|
||||
$format = DATETIME_DATETIME_STORAGE_FORMAT;
|
||||
}
|
||||
$value = $date->format($format);
|
||||
// We only provide a default value for the first item, as do all fields.
|
||||
// Otherwise, there is no way to clear out unwanted values on multiple value
|
||||
// fields.
|
||||
$default_value = array(
|
||||
array(
|
||||
$default_value = [
|
||||
[
|
||||
'value' => $value,
|
||||
'date' => $date,
|
||||
)
|
||||
);
|
||||
]
|
||||
];
|
||||
}
|
||||
return $default_value;
|
||||
}
|
||||
|
||||
@@ -26,9 +26,9 @@ class DateTimeItem extends FieldItemBase {
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function defaultStorageSettings() {
|
||||
return array(
|
||||
return [
|
||||
'datetime_type' => 'datetime',
|
||||
) + parent::defaultStorageSettings();
|
||||
] + parent::defaultStorageSettings();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -63,37 +63,37 @@ class DateTimeItem extends FieldItemBase {
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function schema(FieldStorageDefinitionInterface $field_definition) {
|
||||
return array(
|
||||
'columns' => array(
|
||||
'value' => array(
|
||||
return [
|
||||
'columns' => [
|
||||
'value' => [
|
||||
'description' => 'The date value.',
|
||||
'type' => 'varchar',
|
||||
'length' => 20,
|
||||
),
|
||||
),
|
||||
'indexes' => array(
|
||||
'value' => array('value'),
|
||||
),
|
||||
);
|
||||
],
|
||||
],
|
||||
'indexes' => [
|
||||
'value' => ['value'],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
|
||||
$element = array();
|
||||
$element = [];
|
||||
|
||||
$element['datetime_type'] = array(
|
||||
$element['datetime_type'] = [
|
||||
'#type' => 'select',
|
||||
'#title' => t('Date type'),
|
||||
'#description' => t('Choose the type of date to create.'),
|
||||
'#default_value' => $this->getSetting('datetime_type'),
|
||||
'#options' => array(
|
||||
'#options' => [
|
||||
static::DATETIME_TYPE_DATETIME => t('Date and time'),
|
||||
static::DATETIME_TYPE_DATE => t('Date only'),
|
||||
),
|
||||
],
|
||||
'#disabled' => $has_data,
|
||||
);
|
||||
];
|
||||
|
||||
return $element;
|
||||
}
|
||||
|
||||
@@ -22,11 +22,11 @@ class DateTimeDatelistWidget extends DateTimeWidgetBase {
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function defaultSettings() {
|
||||
return array(
|
||||
return [
|
||||
'increment' => '15',
|
||||
'date_order' => 'YMD',
|
||||
'time_type' => '24',
|
||||
) + parent::defaultSettings();
|
||||
] + parent::defaultSettings();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -35,6 +35,9 @@ class DateTimeDatelistWidget extends DateTimeWidgetBase {
|
||||
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
|
||||
$element = parent::formElement($items, $delta, $element, $form, $form_state);
|
||||
|
||||
// Wrap all of the select elements with a fieldset.
|
||||
$element['#theme_wrappers'][] = 'fieldset';
|
||||
|
||||
$date_order = $this->getSetting('date_order');
|
||||
|
||||
if ($this->getFieldSetting('datetime_type') == 'datetime') {
|
||||
@@ -49,35 +52,35 @@ class DateTimeDatelistWidget extends DateTimeWidgetBase {
|
||||
// Set up the date part order array.
|
||||
switch ($date_order) {
|
||||
case 'YMD':
|
||||
$date_part_order = array('year', 'month', 'day');
|
||||
$date_part_order = ['year', 'month', 'day'];
|
||||
break;
|
||||
|
||||
case 'MDY':
|
||||
$date_part_order = array('month', 'day', 'year');
|
||||
$date_part_order = ['month', 'day', 'year'];
|
||||
break;
|
||||
|
||||
case 'DMY':
|
||||
$date_part_order = array('day', 'month', 'year');
|
||||
$date_part_order = ['day', 'month', 'year'];
|
||||
break;
|
||||
}
|
||||
switch ($time_type) {
|
||||
case '24':
|
||||
$date_part_order = array_merge($date_part_order, array('hour', 'minute'));
|
||||
break;
|
||||
case '24':
|
||||
$date_part_order = array_merge($date_part_order, ['hour', 'minute']);
|
||||
break;
|
||||
|
||||
case '12':
|
||||
$date_part_order = array_merge($date_part_order, array('hour', 'minute', 'ampm'));
|
||||
break;
|
||||
case '12':
|
||||
$date_part_order = array_merge($date_part_order, ['hour', 'minute', 'ampm']);
|
||||
break;
|
||||
|
||||
case 'none':
|
||||
break;
|
||||
case 'none':
|
||||
break;
|
||||
}
|
||||
|
||||
$element['value'] = array(
|
||||
$element['value'] = [
|
||||
'#type' => 'datelist',
|
||||
'#date_increment' => $increment,
|
||||
'#date_part_order' => $date_part_order,
|
||||
) + $element['value'];
|
||||
] + $element['value'];
|
||||
|
||||
return $element;
|
||||
}
|
||||
@@ -85,23 +88,23 @@ class DateTimeDatelistWidget extends DateTimeWidgetBase {
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
function settingsForm(array $form, FormStateInterface $form_state) {
|
||||
public function settingsForm(array $form, FormStateInterface $form_state) {
|
||||
$element = parent::settingsForm($form, $form_state);
|
||||
|
||||
$element['date_order'] = array(
|
||||
$element['date_order'] = [
|
||||
'#type' => 'select',
|
||||
'#title' => t('Date part order'),
|
||||
'#default_value' => $this->getSetting('date_order'),
|
||||
'#options' => array('MDY' => t('Month/Day/Year'), 'DMY' => t('Day/Month/Year'), 'YMD' => t('Year/Month/Day')),
|
||||
);
|
||||
'#options' => ['MDY' => t('Month/Day/Year'), 'DMY' => t('Day/Month/Year'), 'YMD' => t('Year/Month/Day')],
|
||||
];
|
||||
|
||||
if ($this->getFieldSetting('datetime_type') == 'datetime') {
|
||||
$element['time_type'] = array(
|
||||
$element['time_type'] = [
|
||||
'#type' => 'select',
|
||||
'#title' => t('Time type'),
|
||||
'#default_value' => $this->getSetting('time_type'),
|
||||
'#options' => array('24' => t('24 hour time'), '12' => t('12 hour time')),
|
||||
);
|
||||
'#options' => ['24' => t('24 hour time'), '12' => t('12 hour time')],
|
||||
];
|
||||
|
||||
$element['increment'] = [
|
||||
'#type' => 'select',
|
||||
@@ -117,10 +120,10 @@ class DateTimeDatelistWidget extends DateTimeWidgetBase {
|
||||
];
|
||||
}
|
||||
else {
|
||||
$element['time_type'] = array(
|
||||
$element['time_type'] = [
|
||||
'#type' => 'hidden',
|
||||
'#value' => 'none',
|
||||
);
|
||||
];
|
||||
|
||||
$element['increment'] = [
|
||||
'#type' => 'hidden',
|
||||
@@ -135,12 +138,12 @@ class DateTimeDatelistWidget extends DateTimeWidgetBase {
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function settingsSummary() {
|
||||
$summary = array();
|
||||
$summary = [];
|
||||
|
||||
$summary[] = t('Date part order: @order', array('@order' => $this->getSetting('date_order')));
|
||||
$summary[] = t('Date part order: @order', ['@order' => $this->getSetting('date_order')]);
|
||||
if ($this->getFieldSetting('datetime_type') == 'datetime') {
|
||||
$summary[] = t('Time type: @time_type', array('@time_type' => $this->getSetting('time_type')));
|
||||
$summary[] = t('Time increments: @increment', array('@increment' => $this->getSetting('increment')));
|
||||
$summary[] = t('Time type: @time_type', ['@time_type' => $this->getSetting('time_type')]);
|
||||
$summary[] = t('Time increments: @increment', ['@increment' => $this->getSetting('increment')]);
|
||||
}
|
||||
|
||||
return $summary;
|
||||
|
||||
@@ -59,6 +59,16 @@ class DateTimeDefaultWidget extends DateTimeWidgetBase implements ContainerFacto
|
||||
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
|
||||
$element = parent::formElement($items, $delta, $element, $form, $form_state);
|
||||
|
||||
// If the field is date-only, make sure the title is displayed. Otherwise,
|
||||
// wrap everything in a fieldset, and the title will be shown in the legend.
|
||||
if ($this->getFieldSetting('datetime_type') === DateTimeItem::DATETIME_TYPE_DATE) {
|
||||
$element['value']['#title'] = $this->fieldDefinition->getLabel();
|
||||
$element['value']['#description'] = $this->fieldDefinition->getDescription();
|
||||
}
|
||||
else {
|
||||
$element['#theme_wrappers'][] = 'fieldset';
|
||||
}
|
||||
|
||||
// Identify the type of date and time elements to use.
|
||||
switch ($this->getFieldSetting('datetime_type')) {
|
||||
case DateTimeItem::DATETIME_TYPE_DATE:
|
||||
@@ -76,14 +86,14 @@ class DateTimeDefaultWidget extends DateTimeWidgetBase implements ContainerFacto
|
||||
break;
|
||||
}
|
||||
|
||||
$element['value'] += array(
|
||||
$element['value'] += [
|
||||
'#date_date_format' => $date_format,
|
||||
'#date_date_element' => $date_type,
|
||||
'#date_date_callbacks' => array(),
|
||||
'#date_date_callbacks' => [],
|
||||
'#date_time_format' => $time_format,
|
||||
'#date_time_element' => $time_type,
|
||||
'#date_time_callbacks' => array(),
|
||||
);
|
||||
'#date_time_callbacks' => [],
|
||||
];
|
||||
|
||||
return $element;
|
||||
}
|
||||
|
||||
@@ -17,22 +17,19 @@ class DateTimeWidgetBase extends WidgetBase {
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
|
||||
// We are nesting some sub-elements inside the parent, so we need a wrapper.
|
||||
// We also need to add another #title attribute at the top level for ease in
|
||||
// identifying this item in error messages. We do not want to display this
|
||||
// title because the actual title display is handled at a higher level by
|
||||
// the Field module.
|
||||
|
||||
$element['#theme_wrappers'][] = 'datetime_wrapper';
|
||||
$element['#attributes']['class'][] = 'container-inline';
|
||||
|
||||
$element['value'] = array(
|
||||
$element['value'] = [
|
||||
'#type' => 'datetime',
|
||||
'#default_value' => NULL,
|
||||
'#date_increment' => 1,
|
||||
'#date_timezone' => drupal_get_user_timezone(),
|
||||
'#required' => $element['#required'],
|
||||
);
|
||||
];
|
||||
|
||||
if ($this->getFieldSetting('datetime_type') == DateTimeItem::DATETIME_TYPE_DATE) {
|
||||
// A date-only field should have no timezone conversion performed, so
|
||||
// use the same timezone as for storage.
|
||||
$element['value']['#date_timezone'] = DATETIME_STORAGE_TIMEZONE;
|
||||
}
|
||||
|
||||
if ($items[$delta]->date) {
|
||||
$date = $items[$delta]->date;
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\datetime\Plugin\migrate\field\d6;
|
||||
|
||||
use Drupal\migrate\Plugin\MigrationInterface;
|
||||
use Drupal\migrate\MigrateException;
|
||||
use Drupal\migrate_drupal\Plugin\migrate\field\FieldPluginBase;
|
||||
|
||||
/**
|
||||
* @MigrateField(
|
||||
* id = "date",
|
||||
* type_map = {
|
||||
* "date" = "datetime",
|
||||
* "datestamp" = "timestamp",
|
||||
* "datetime" = "datetime",
|
||||
* },
|
||||
* core = {6}
|
||||
* )
|
||||
*/
|
||||
class DateField extends FieldPluginBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getFieldWidgetMap() {
|
||||
return [
|
||||
'date' => 'datetime_default',
|
||||
'datetime' => 'datetime_default',
|
||||
'datestamp' => 'datetime_timestamp',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getFieldFormatterMap() {
|
||||
// See d6_field_formatter_settings.yml and
|
||||
// FieldPluginBase::processFieldFormatter().
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function processFieldValues(MigrationInterface $migration, $field_name, $data) {
|
||||
switch ($data['type']) {
|
||||
case 'date':
|
||||
$from_format = 'Y-m-d\TH:i:s';
|
||||
$to_format = 'Y-m-d\TH:i:s';
|
||||
break;
|
||||
case 'datestamp':
|
||||
$from_format = 'U';
|
||||
$to_format = 'U';
|
||||
break;
|
||||
case 'datetime':
|
||||
$from_format = 'Y-m-d H:i:s';
|
||||
$to_format = 'Y-m-d\TH:i:s';
|
||||
break;
|
||||
default:
|
||||
throw new MigrateException(sprintf('Field %s of type %s is an unknown date field type.', $field_name, var_export($data['type'], TRUE)));
|
||||
}
|
||||
$process = [
|
||||
'value' => [
|
||||
'plugin' => 'format_date',
|
||||
'from_format' => $from_format,
|
||||
'to_format' => $to_format,
|
||||
'source' => 'value',
|
||||
],
|
||||
];
|
||||
|
||||
$process = [
|
||||
'plugin' => 'iterator',
|
||||
'source' => $field_name,
|
||||
'process' => $process,
|
||||
];
|
||||
$migration->mergeProcessOfProperty($field_name, $process);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -58,7 +58,7 @@ class Date extends NumericDate implements ContainerFactoryPluginInterface {
|
||||
* The plugin implementation definition.
|
||||
* @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
|
||||
* The date formatter service.
|
||||
* @param \Symfony\Component\HttpFoundation\RequestStack
|
||||
* @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
|
||||
* The request stack used to determine the current time.
|
||||
*/
|
||||
public function __construct(array $configuration, $plugin_id, $plugin_definition, DateFormatterInterface $date_formatter, RequestStack $request_stack) {
|
||||
|
||||
@@ -0,0 +1,188 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\datetime\Tests;
|
||||
|
||||
@trigger_error('\Drupal\datetime\Tests\DateTestBase is deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0. Use \Drupal\Tests\BrowserTestBase instead. See https://www.drupal.org/node/2780063.', E_USER_DEPRECATED);
|
||||
|
||||
use Drupal\Component\Utility\Unicode;
|
||||
use Drupal\Core\Entity\Entity\EntityFormDisplay;
|
||||
use Drupal\Core\Entity\Entity\EntityViewDisplay;
|
||||
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
|
||||
use Drupal\entity_test\Entity\EntityTest;
|
||||
use Drupal\field\Entity\FieldConfig;
|
||||
use Drupal\field\Entity\FieldStorageConfig;
|
||||
use Drupal\simpletest\WebTestBase;
|
||||
|
||||
/**
|
||||
* Provides a base class for testing Datetime field functionality.
|
||||
*
|
||||
* @deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0.
|
||||
* Use \Drupal\Tests\BrowserTestBase instead.
|
||||
*/
|
||||
abstract class DateTestBase extends WebTestBase {
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = ['node', 'entity_test', 'datetime', 'field_ui'];
|
||||
|
||||
/**
|
||||
* An array of display options to pass to entity_get_display()
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $displayOptions;
|
||||
|
||||
/**
|
||||
* A field storage to use in this test class.
|
||||
*
|
||||
* @var \Drupal\field\Entity\FieldStorageConfig
|
||||
*/
|
||||
protected $fieldStorage;
|
||||
|
||||
/**
|
||||
* The field used in this test class.
|
||||
*
|
||||
* @var \Drupal\field\Entity\FieldConfig
|
||||
*/
|
||||
protected $field;
|
||||
|
||||
/**
|
||||
* The date formatter service.
|
||||
*
|
||||
* @var \Drupal\Core\Datetime\DateFormatterInterface
|
||||
*/
|
||||
protected $dateFormatter;
|
||||
|
||||
/**
|
||||
* An array of timezone extremes to test.
|
||||
*
|
||||
* @var string[]
|
||||
*/
|
||||
protected static $timezones = [
|
||||
// UTC-12, no DST.
|
||||
'Pacific/Kwajalein',
|
||||
// UTC-11, no DST
|
||||
'Pacific/Midway',
|
||||
// UTC-7, no DST.
|
||||
'America/Phoenix',
|
||||
// UTC.
|
||||
'UTC',
|
||||
// UTC+5:30, no DST.
|
||||
'Asia/Kolkata',
|
||||
// UTC+12, no DST
|
||||
'Pacific/Funafuti',
|
||||
// UTC+13, no DST.
|
||||
'Pacific/Tongatapu',
|
||||
];
|
||||
|
||||
/**
|
||||
* Returns the type of field to be tested.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
abstract protected function getTestFieldType();
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$web_user = $this->drupalCreateUser([
|
||||
'access content',
|
||||
'view test entity',
|
||||
'administer entity_test content',
|
||||
'administer entity_test form display',
|
||||
'administer content types',
|
||||
'administer node fields',
|
||||
]);
|
||||
$this->drupalLogin($web_user);
|
||||
|
||||
// Create a field with settings to validate.
|
||||
$this->createField();
|
||||
|
||||
$this->dateFormatter = $this->container->get('date.formatter');
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a date test field.
|
||||
*/
|
||||
protected function createField() {
|
||||
$field_name = Unicode::strtolower($this->randomMachineName());
|
||||
$type = $this->getTestFieldType();
|
||||
$widget_type = $formatter_type = $type . '_default';
|
||||
|
||||
$this->fieldStorage = FieldStorageConfig::create([
|
||||
'field_name' => $field_name,
|
||||
'entity_type' => 'entity_test',
|
||||
'type' => $type,
|
||||
'settings' => ['datetime_type' => DateTimeItem::DATETIME_TYPE_DATE],
|
||||
]);
|
||||
$this->fieldStorage->save();
|
||||
$this->field = FieldConfig::create([
|
||||
'field_storage' => $this->fieldStorage,
|
||||
'bundle' => 'entity_test',
|
||||
'description' => 'Description for ' . $field_name,
|
||||
'required' => TRUE,
|
||||
]);
|
||||
$this->field->save();
|
||||
|
||||
EntityFormDisplay::load('entity_test.entity_test.default')
|
||||
->setComponent($field_name, ['type' => $widget_type])
|
||||
->save();
|
||||
|
||||
$this->displayOptions = [
|
||||
'type' => $formatter_type,
|
||||
'label' => 'hidden',
|
||||
'settings' => ['format_type' => 'medium'] + $this->defaultSettings,
|
||||
];
|
||||
EntityViewDisplay::create([
|
||||
'targetEntityType' => $this->field->getTargetEntityTypeId(),
|
||||
'bundle' => $this->field->getTargetBundle(),
|
||||
'mode' => 'full',
|
||||
'status' => TRUE,
|
||||
])->setComponent($field_name, $this->displayOptions)
|
||||
->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders a entity_test and sets the output in the internal browser.
|
||||
*
|
||||
* @param int $id
|
||||
* The entity_test ID to render.
|
||||
* @param string $view_mode
|
||||
* (optional) The view mode to use for rendering. Defaults to 'full'.
|
||||
* @param bool $reset
|
||||
* (optional) Whether to reset the entity_test controller cache. Defaults to
|
||||
* TRUE to simplify testing.
|
||||
*/
|
||||
protected function renderTestEntity($id, $view_mode = 'full', $reset = TRUE) {
|
||||
if ($reset) {
|
||||
$this->container->get('entity_type.manager')->getStorage('entity_test')->resetCache([$id]);
|
||||
}
|
||||
$entity = EntityTest::load($id);
|
||||
$display = EntityViewDisplay::collectRenderDisplay($entity, $view_mode);
|
||||
$build = $display->build($entity);
|
||||
$output = $this->container->get('renderer')->renderRoot($build);
|
||||
$this->setRawContent($output);
|
||||
$this->verbose($output);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the site timezone to a given timezone.
|
||||
*
|
||||
* @param string $timezone
|
||||
* The timezone identifier to set.
|
||||
*/
|
||||
protected function setSiteTimezone($timezone) {
|
||||
// Set an explicit site timezone, and disallow per-user timezones.
|
||||
$this->config('system.date')
|
||||
->set('timezone.user.configurable', 0)
|
||||
->set('timezone.default', $timezone)
|
||||
->save();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -136,7 +136,8 @@ class FilterDateTimeTest extends DateTimeHandlerTestBase {
|
||||
$view->initHandlers();
|
||||
$view->filter[$field]->operator = 'not between';
|
||||
$view->filter[$field]->value['min'] = '2001-01-01';
|
||||
$view->filter[$field]->value['max'] = '2002-01-01';
|
||||
// Set maximum date to date of node 1 to test range borders.
|
||||
$view->filter[$field]->value['max'] = '2001-10-10T12:12:12';
|
||||
$view->setDisplay('default');
|
||||
$this->executeView($view);
|
||||
$expected_result = [
|
||||
|
||||
Reference in New Issue
Block a user