Update core 8.3.0
This commit is contained in:
parent
da7a7918f8
commit
cd7a898e66
6144 changed files with 132297 additions and 87747 deletions
|
@ -47,10 +47,8 @@ field.formatter.settings.datetime_custom:
|
|||
label: 'Datetime custom display format settings'
|
||||
mapping:
|
||||
date_format:
|
||||
type: string
|
||||
type: date_format
|
||||
label: 'Date/time format'
|
||||
translatable: true
|
||||
translation context: 'PHP date format'
|
||||
|
||||
field.formatter.settings.datetime_time_ago:
|
||||
type: mapping
|
||||
|
|
|
@ -30,13 +30,13 @@ function datetime_help($route_name, RouteMatchInterface $route_match) {
|
|||
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::moduleHandler()->moduleExists('field_ui')) ? \Drupal::url('help.page', array('name' => 'field_ui')) : '#', ':datetime_do' => 'https://www.drupal.org/documentation/modules/datetime')) . '</p>';
|
||||
$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>.', [':field' => \Drupal::url('help.page', ['name' => 'field']), ':field_ui' => (\Drupal::moduleHandler()->moduleExists('field_ui')) ? \Drupal::url('help.page', ['name' => 'field_ui']) : '#', ':datetime_do' => 'https://www.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::moduleHandler()->moduleExists('field_ui')) ? \Drupal::url('help.page', array('name' => 'field_ui')) : '#')) . '</dd>';
|
||||
$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.', [':field_ui' => (\Drupal::moduleHandler()->moduleExists('field_ui')) ? \Drupal::url('help.page', ['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('entity.date_format.collection'))) . '</dd>';
|
||||
$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.', [':date_format_list' => \Drupal::url('entity.date_format.collection')]) . '</dd>';
|
||||
$output .= '</dl>';
|
||||
return $output;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -82,9 +82,9 @@ abstract class DateTimeFormatterBase extends FormatterBase implements ContainerF
|
|||
* {@inheritdoc}
|
||||
*/
|
||||
public static function defaultSettings() {
|
||||
return array(
|
||||
return [
|
||||
'timezone_override' => '',
|
||||
) + parent::defaultSettings();
|
||||
] + parent::defaultSettings();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -93,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;
|
||||
}
|
||||
|
@ -111,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;
|
||||
|
|
|
@ -22,7 +22,7 @@ class DateTimePlainFormatter extends DateTimeFormatterBase {
|
|||
* {@inheritdoc}
|
||||
*/
|
||||
public function viewElements(FieldItemListInterface $items, $langcode) {
|
||||
$elements = array();
|
||||
$elements = [];
|
||||
|
||||
foreach ($items as $delta => $item) {
|
||||
$output = '';
|
||||
|
|
|
@ -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 [];
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -108,12 +108,12 @@ class DateTimeFieldItemList extends FieldItemList {
|
|||
// 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'));
|
||||
$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'));
|
||||
$date_part_order = array_merge($date_part_order, ['hour', 'minute', 'ampm']);
|
||||
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,13 @@ 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
|
||||
|
|
|
@ -5,7 +5,7 @@ namespace Drupal\datetime\Tests;
|
|||
use Drupal\Component\Utility\Unicode;
|
||||
use Drupal\Core\Entity\Entity\EntityFormDisplay;
|
||||
use Drupal\Core\Entity\Entity\EntityViewDisplay;
|
||||
use Drupal\datetime_range\Plugin\Field\FieldType\DateRangeItem;
|
||||
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
|
||||
use Drupal\entity_test\Entity\EntityTest;
|
||||
use Drupal\field\Entity\FieldConfig;
|
||||
use Drupal\field\Entity\FieldStorageConfig;
|
||||
|
@ -114,12 +114,13 @@ abstract class DateTestBase extends WebTestBase {
|
|||
'field_name' => $field_name,
|
||||
'entity_type' => 'entity_test',
|
||||
'type' => $type,
|
||||
'settings' => ['datetime_type' => DateRangeItem::DATETIME_TYPE_DATE],
|
||||
'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();
|
||||
|
|
|
@ -35,7 +35,7 @@ class DateTimeFieldTest extends DateTestBase {
|
|||
/**
|
||||
* Tests date field functionality.
|
||||
*/
|
||||
function testDateField() {
|
||||
public function testDateField() {
|
||||
$field_name = $this->fieldStorage->getName();
|
||||
|
||||
// Loop through defined timezones to test that date-only fields work at the
|
||||
|
@ -47,8 +47,10 @@ class DateTimeFieldTest extends DateTestBase {
|
|||
// Display creation form.
|
||||
$this->drupalGet('entity_test/add');
|
||||
$this->assertFieldByName("{$field_name}[0][value][date]", '', 'Date element found.');
|
||||
$this->assertFieldByXPath('//*[@id="edit-' . $field_name . '-wrapper"]/h4[contains(@class, "js-form-required")]', TRUE, 'Required markup found');
|
||||
$this->assertFieldByXPath('//*[@id="edit-' . $field_name . '-wrapper"]//label[contains(@class,"js-form-required")]', TRUE, 'Required markup found');
|
||||
$this->assertNoFieldByName("{$field_name}[0][value][time]", '', 'Time element not found.');
|
||||
$this->assertFieldByXPath('//input[@aria-describedby="edit-' . $field_name . '-0-value--description"]', NULL, 'ARIA described-by found');
|
||||
$this->assertFieldByXPath('//div[@id="edit-' . $field_name . '-0-value--description"]', NULL, 'ARIA description found');
|
||||
|
||||
// Build up a date in the UTC timezone. Note that using this will also
|
||||
// mimic the user in a different timezone simply entering '2012-12-31' via
|
||||
|
@ -60,13 +62,13 @@ class DateTimeFieldTest extends DateTestBase {
|
|||
$date_format = DateFormat::load('html_date')->getPattern();
|
||||
$time_format = DateFormat::load('html_time')->getPattern();
|
||||
|
||||
$edit = array(
|
||||
$edit = [
|
||||
"{$field_name}[0][value][date]" => $date->format($date_format),
|
||||
);
|
||||
];
|
||||
$this->drupalPostForm(NULL, $edit, t('Save'));
|
||||
preg_match('|entity_test/manage/(\d+)|', $this->url, $match);
|
||||
$id = $match[1];
|
||||
$this->assertText(t('entity_test @id has been created.', array('@id' => $id)));
|
||||
$this->assertText(t('entity_test @id has been created.', ['@id' => $id]));
|
||||
$this->assertRaw($date->format($date_format));
|
||||
$this->assertNoRaw($date->format($time_format));
|
||||
|
||||
|
@ -84,15 +86,15 @@ class DateTimeFieldTest extends DateTestBase {
|
|||
$this->assertEqual('2012-12-31', $entity->{$field_name}->value);
|
||||
|
||||
// Reset display options since these get changed below.
|
||||
$this->displayOptions = array(
|
||||
$this->displayOptions = [
|
||||
'type' => 'datetime_default',
|
||||
'label' => 'hidden',
|
||||
'settings' => array('format_type' => 'medium') + $this->defaultSettings,
|
||||
);
|
||||
'settings' => ['format_type' => 'medium'] + $this->defaultSettings,
|
||||
];
|
||||
// Verify that the date is output according to the formatter settings.
|
||||
$options = array(
|
||||
'format_type' => array('short', 'medium', 'long'),
|
||||
);
|
||||
$options = [
|
||||
'format_type' => ['short', 'medium', 'long'],
|
||||
];
|
||||
// Formats that display a time component for date-only fields will display
|
||||
// the default time, so that is applied before calculating the expected
|
||||
// value.
|
||||
|
@ -100,7 +102,7 @@ class DateTimeFieldTest extends DateTestBase {
|
|||
foreach ($options as $setting => $values) {
|
||||
foreach ($values as $new_value) {
|
||||
// Update the entity display settings.
|
||||
$this->displayOptions['settings'] = array($setting => $new_value) + $this->defaultSettings;
|
||||
$this->displayOptions['settings'] = [$setting => $new_value] + $this->defaultSettings;
|
||||
entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')
|
||||
->setComponent($field_name, $this->displayOptions)
|
||||
->save();
|
||||
|
@ -113,7 +115,7 @@ class DateTimeFieldTest extends DateTestBase {
|
|||
$expected = format_date($date->getTimestamp(), $new_value, '', DATETIME_STORAGE_TIMEZONE);
|
||||
$expected_iso = format_date($date->getTimestamp(), 'custom', 'Y-m-d\TH:i:s\Z', DATETIME_STORAGE_TIMEZONE);
|
||||
$this->renderTestEntity($id);
|
||||
$this->assertFieldByXPath('//time[@datetime="' . $expected_iso . '"]', $expected, SafeMarkup::format('Formatted date field using %value format displayed as %expected with %expected_iso attribute.', array('%value' => $new_value, '%expected' => $expected, '%expected_iso' => $expected_iso)));
|
||||
$this->assertFieldByXPath('//time[@datetime="' . $expected_iso . '"]', $expected, SafeMarkup::format('Formatted date field using %value format displayed as %expected with %expected_iso attribute.', ['%value' => $new_value, '%expected' => $expected, '%expected_iso' => $expected_iso]));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -127,17 +129,17 @@ class DateTimeFieldTest extends DateTestBase {
|
|||
->save();
|
||||
$expected = $date->format(DATETIME_DATE_STORAGE_FORMAT);
|
||||
$this->renderTestEntity($id);
|
||||
$this->assertText($expected, SafeMarkup::format('Formatted date field using plain format displayed as %expected.', array('%expected' => $expected)));
|
||||
$this->assertText($expected, SafeMarkup::format('Formatted date field using plain format displayed as %expected.', ['%expected' => $expected]));
|
||||
|
||||
// Verify that the 'datetime_custom' formatter works.
|
||||
$this->displayOptions['type'] = 'datetime_custom';
|
||||
$this->displayOptions['settings'] = array('date_format' => 'm/d/Y') + $this->defaultSettings;
|
||||
$this->displayOptions['settings'] = ['date_format' => 'm/d/Y'] + $this->defaultSettings;
|
||||
entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')
|
||||
->setComponent($field_name, $this->displayOptions)
|
||||
->save();
|
||||
$expected = $date->format($this->displayOptions['settings']['date_format']);
|
||||
$this->renderTestEntity($id);
|
||||
$this->assertText($expected, SafeMarkup::format('Formatted date field using datetime_custom format displayed as %expected.', array('%expected' => $expected)));
|
||||
$this->assertText($expected, SafeMarkup::format('Formatted date field using datetime_custom format displayed as %expected.', ['%expected' => $expected]));
|
||||
|
||||
// Verify that the 'datetime_time_ago' formatter works for intervals in the
|
||||
// past. First update the test entity so that the date difference always
|
||||
|
@ -152,11 +154,11 @@ class DateTimeFieldTest extends DateTestBase {
|
|||
$entity->save();
|
||||
|
||||
$this->displayOptions['type'] = 'datetime_time_ago';
|
||||
$this->displayOptions['settings'] = array(
|
||||
$this->displayOptions['settings'] = [
|
||||
'future_format' => '@interval in the future',
|
||||
'past_format' => '@interval in the past',
|
||||
'granularity' => 3,
|
||||
);
|
||||
];
|
||||
entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')
|
||||
->setComponent($field_name, $this->displayOptions)
|
||||
->save();
|
||||
|
@ -164,7 +166,7 @@ class DateTimeFieldTest extends DateTestBase {
|
|||
'@interval' => $this->dateFormatter->formatTimeDiffSince($timestamp, ['granularity' => $this->displayOptions['settings']['granularity']])
|
||||
]);
|
||||
$this->renderTestEntity($id);
|
||||
$this->assertText($expected, SafeMarkup::format('Formatted date field using datetime_time_ago format displayed as %expected.', array('%expected' => $expected)));
|
||||
$this->assertText($expected, SafeMarkup::format('Formatted date field using datetime_time_ago format displayed as %expected.', ['%expected' => $expected]));
|
||||
|
||||
// Verify that the 'datetime_time_ago' formatter works for intervals in the
|
||||
// future. First update the test entity so that the date difference always
|
||||
|
@ -185,14 +187,14 @@ class DateTimeFieldTest extends DateTestBase {
|
|||
'@interval' => $this->dateFormatter->formatTimeDiffUntil($timestamp, ['granularity' => $this->displayOptions['settings']['granularity']])
|
||||
]);
|
||||
$this->renderTestEntity($id);
|
||||
$this->assertText($expected, SafeMarkup::format('Formatted date field using datetime_time_ago format displayed as %expected.', array('%expected' => $expected)));
|
||||
$this->assertText($expected, SafeMarkup::format('Formatted date field using datetime_time_ago format displayed as %expected.', ['%expected' => $expected]));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests date and time field.
|
||||
*/
|
||||
function testDatetimeField() {
|
||||
public function testDatetimeField() {
|
||||
$field_name = $this->fieldStorage->getName();
|
||||
// Change the field to a datetime field.
|
||||
$this->fieldStorage->setSetting('datetime_type', 'datetime');
|
||||
|
@ -202,6 +204,9 @@ class DateTimeFieldTest extends DateTestBase {
|
|||
$this->drupalGet('entity_test/add');
|
||||
$this->assertFieldByName("{$field_name}[0][value][date]", '', 'Date element found.');
|
||||
$this->assertFieldByName("{$field_name}[0][value][time]", '', 'Time element found.');
|
||||
$this->assertFieldByXPath('//fieldset[@id="edit-' . $field_name . '-0"]/legend//text()', $field_name, 'Fieldset and label found');
|
||||
$this->assertFieldByXPath('//fieldset[@aria-describedby="edit-' . $field_name . '-0--description"]', NULL, 'ARIA described-by found');
|
||||
$this->assertFieldByXPath('//div[@id="edit-' . $field_name . '-0--description"]', NULL, 'ARIA description found');
|
||||
|
||||
// Build up a date in the UTC timezone.
|
||||
$value = '2012-12-31 00:00:00';
|
||||
|
@ -214,25 +219,25 @@ class DateTimeFieldTest extends DateTestBase {
|
|||
$date_format = DateFormat::load('html_date')->getPattern();
|
||||
$time_format = DateFormat::load('html_time')->getPattern();
|
||||
|
||||
$edit = array(
|
||||
$edit = [
|
||||
"{$field_name}[0][value][date]" => $date->format($date_format),
|
||||
"{$field_name}[0][value][time]" => $date->format($time_format),
|
||||
);
|
||||
];
|
||||
$this->drupalPostForm(NULL, $edit, t('Save'));
|
||||
preg_match('|entity_test/manage/(\d+)|', $this->url, $match);
|
||||
$id = $match[1];
|
||||
$this->assertText(t('entity_test @id has been created.', array('@id' => $id)));
|
||||
$this->assertText(t('entity_test @id has been created.', ['@id' => $id]));
|
||||
$this->assertRaw($date->format($date_format));
|
||||
$this->assertRaw($date->format($time_format));
|
||||
|
||||
// Verify that the date is output according to the formatter settings.
|
||||
$options = array(
|
||||
'format_type' => array('short', 'medium', 'long'),
|
||||
);
|
||||
$options = [
|
||||
'format_type' => ['short', 'medium', 'long'],
|
||||
];
|
||||
foreach ($options as $setting => $values) {
|
||||
foreach ($values as $new_value) {
|
||||
// Update the entity display settings.
|
||||
$this->displayOptions['settings'] = array($setting => $new_value) + $this->defaultSettings;
|
||||
$this->displayOptions['settings'] = [$setting => $new_value] + $this->defaultSettings;
|
||||
entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')
|
||||
->setComponent($field_name, $this->displayOptions)
|
||||
->save();
|
||||
|
@ -244,7 +249,7 @@ class DateTimeFieldTest extends DateTestBase {
|
|||
$expected = format_date($date->getTimestamp(), $new_value);
|
||||
$expected_iso = format_date($date->getTimestamp(), 'custom', 'Y-m-d\TH:i:s\Z', 'UTC');
|
||||
$this->renderTestEntity($id);
|
||||
$this->assertFieldByXPath('//time[@datetime="' . $expected_iso . '"]', $expected, SafeMarkup::format('Formatted date field using %value format displayed as %expected with %expected_iso attribute.', array('%value' => $new_value, '%expected' => $expected, '%expected_iso' => $expected_iso)));
|
||||
$this->assertFieldByXPath('//time[@datetime="' . $expected_iso . '"]', $expected, SafeMarkup::format('Formatted date field using %value format displayed as %expected with %expected_iso attribute.', ['%value' => $new_value, '%expected' => $expected, '%expected_iso' => $expected_iso]));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -258,27 +263,27 @@ class DateTimeFieldTest extends DateTestBase {
|
|||
->save();
|
||||
$expected = $date->format(DATETIME_DATETIME_STORAGE_FORMAT);
|
||||
$this->renderTestEntity($id);
|
||||
$this->assertText($expected, SafeMarkup::format('Formatted date field using plain format displayed as %expected.', array('%expected' => $expected)));
|
||||
$this->assertText($expected, SafeMarkup::format('Formatted date field using plain format displayed as %expected.', ['%expected' => $expected]));
|
||||
|
||||
// Verify that the 'datetime_custom' formatter works.
|
||||
$this->displayOptions['type'] = 'datetime_custom';
|
||||
$this->displayOptions['settings'] = array('date_format' => 'm/d/Y g:i:s A') + $this->defaultSettings;
|
||||
$this->displayOptions['settings'] = ['date_format' => 'm/d/Y g:i:s A'] + $this->defaultSettings;
|
||||
entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')
|
||||
->setComponent($field_name, $this->displayOptions)
|
||||
->save();
|
||||
$expected = $date->format($this->displayOptions['settings']['date_format']);
|
||||
$this->renderTestEntity($id);
|
||||
$this->assertText($expected, SafeMarkup::format('Formatted date field using datetime_custom format displayed as %expected.', array('%expected' => $expected)));
|
||||
$this->assertText($expected, SafeMarkup::format('Formatted date field using datetime_custom format displayed as %expected.', ['%expected' => $expected]));
|
||||
|
||||
// Verify that the 'timezone_override' setting works.
|
||||
$this->displayOptions['type'] = 'datetime_custom';
|
||||
$this->displayOptions['settings'] = array('date_format' => 'm/d/Y g:i:s A', 'timezone_override' => 'America/New_York') + $this->defaultSettings;
|
||||
$this->displayOptions['settings'] = ['date_format' => 'm/d/Y g:i:s A', 'timezone_override' => 'America/New_York'] + $this->defaultSettings;
|
||||
entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')
|
||||
->setComponent($field_name, $this->displayOptions)
|
||||
->save();
|
||||
$expected = $date->format($this->displayOptions['settings']['date_format'], array('timezone' => 'America/New_York'));
|
||||
$expected = $date->format($this->displayOptions['settings']['date_format'], ['timezone' => 'America/New_York']);
|
||||
$this->renderTestEntity($id);
|
||||
$this->assertText($expected, SafeMarkup::format('Formatted date field using datetime_custom format displayed as %expected.', array('%expected' => $expected)));
|
||||
$this->assertText($expected, SafeMarkup::format('Formatted date field using datetime_custom format displayed as %expected.', ['%expected' => $expected]));
|
||||
|
||||
// Verify that the 'datetime_time_ago' formatter works for intervals in the
|
||||
// past. First update the test entity so that the date difference always
|
||||
|
@ -293,11 +298,11 @@ class DateTimeFieldTest extends DateTestBase {
|
|||
$entity->save();
|
||||
|
||||
$this->displayOptions['type'] = 'datetime_time_ago';
|
||||
$this->displayOptions['settings'] = array(
|
||||
$this->displayOptions['settings'] = [
|
||||
'future_format' => '@interval from now',
|
||||
'past_format' => '@interval earlier',
|
||||
'granularity' => 3,
|
||||
);
|
||||
];
|
||||
entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')
|
||||
->setComponent($field_name, $this->displayOptions)
|
||||
->save();
|
||||
|
@ -305,7 +310,7 @@ class DateTimeFieldTest extends DateTestBase {
|
|||
'@interval' => $this->dateFormatter->formatTimeDiffSince($timestamp, ['granularity' => $this->displayOptions['settings']['granularity']])
|
||||
]);
|
||||
$this->renderTestEntity($id);
|
||||
$this->assertText($expected, SafeMarkup::format('Formatted date field using datetime_time_ago format displayed as %expected.', array('%expected' => $expected)));
|
||||
$this->assertText($expected, SafeMarkup::format('Formatted date field using datetime_time_ago format displayed as %expected.', ['%expected' => $expected]));
|
||||
|
||||
// Verify that the 'datetime_time_ago' formatter works for intervals in the
|
||||
// future. First update the test entity so that the date difference always
|
||||
|
@ -326,13 +331,13 @@ class DateTimeFieldTest extends DateTestBase {
|
|||
'@interval' => $this->dateFormatter->formatTimeDiffUntil($timestamp, ['granularity' => $this->displayOptions['settings']['granularity']])
|
||||
]);
|
||||
$this->renderTestEntity($id);
|
||||
$this->assertText($expected, SafeMarkup::format('Formatted date field using datetime_time_ago format displayed as %expected.', array('%expected' => $expected)));
|
||||
$this->assertText($expected, SafeMarkup::format('Formatted date field using datetime_time_ago format displayed as %expected.', ['%expected' => $expected]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests Date List Widget functionality.
|
||||
*/
|
||||
function testDatelistWidget() {
|
||||
public function testDatelistWidget() {
|
||||
$field_name = $this->fieldStorage->getName();
|
||||
|
||||
// Ensure field is set to a date only field.
|
||||
|
@ -341,17 +346,20 @@ class DateTimeFieldTest extends DateTestBase {
|
|||
|
||||
// Change the widget to a datelist widget.
|
||||
entity_get_form_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'default')
|
||||
->setComponent($field_name, array(
|
||||
->setComponent($field_name, [
|
||||
'type' => 'datetime_datelist',
|
||||
'settings' => array(
|
||||
'settings' => [
|
||||
'date_order' => 'YMD',
|
||||
),
|
||||
))
|
||||
],
|
||||
])
|
||||
->save();
|
||||
\Drupal::entityManager()->clearCachedFieldDefinitions();
|
||||
|
||||
// Display creation form.
|
||||
$this->drupalGet('entity_test/add');
|
||||
$this->assertFieldByXPath('//fieldset[@id="edit-' . $field_name . '-0"]/legend//text()', $field_name, 'Fieldset and label found');
|
||||
$this->assertFieldByXPath('//fieldset[@aria-describedby="edit-' . $field_name . '-0--description"]', NULL, 'ARIA described-by found');
|
||||
$this->assertFieldByXPath('//div[@id="edit-' . $field_name . '-0--description"]', NULL, 'ARIA description found');
|
||||
|
||||
// Assert that Hour and Minute Elements do not appear on Date Only
|
||||
$this->assertNoFieldByXPath("//*[@id=\"edit-$field_name-0-value-hour\"]", NULL, 'Hour element not found on Date Only.');
|
||||
|
@ -362,7 +370,7 @@ class DateTimeFieldTest extends DateTestBase {
|
|||
$this->drupalGet($fieldEditUrl);
|
||||
|
||||
// Click on the widget settings button to open the widget settings form.
|
||||
$this->drupalPostAjaxForm(NULL, array(), $field_name . "_settings_edit");
|
||||
$this->drupalPostAjaxForm(NULL, [], $field_name . "_settings_edit");
|
||||
$xpathIncr = "//select[starts-with(@id, \"edit-fields-$field_name-settings-edit-form-settings-increment\")]";
|
||||
$this->assertNoFieldByXPath($xpathIncr, NULL, 'Increment element not found for Date Only.');
|
||||
|
||||
|
@ -372,14 +380,14 @@ class DateTimeFieldTest extends DateTestBase {
|
|||
|
||||
// Change the widget to a datelist widget.
|
||||
entity_get_form_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'default')
|
||||
->setComponent($field_name, array(
|
||||
->setComponent($field_name, [
|
||||
'type' => 'datetime_datelist',
|
||||
'settings' => array(
|
||||
'settings' => [
|
||||
'increment' => 1,
|
||||
'date_order' => 'YMD',
|
||||
'time_type' => '12',
|
||||
),
|
||||
))
|
||||
],
|
||||
])
|
||||
->save();
|
||||
\Drupal::entityManager()->clearCachedFieldDefinitions();
|
||||
|
||||
|
@ -388,7 +396,7 @@ class DateTimeFieldTest extends DateTestBase {
|
|||
$this->drupalGet($fieldEditUrl);
|
||||
|
||||
// Click on the widget settings button to open the widget settings form.
|
||||
$this->drupalPostAjaxForm(NULL, array(), $field_name . "_settings_edit");
|
||||
$this->drupalPostAjaxForm(NULL, [], $field_name . "_settings_edit");
|
||||
$this->assertFieldByXPath($xpathIncr, NULL, 'Increment element found for Date and time.');
|
||||
|
||||
// Display creation form.
|
||||
|
@ -415,9 +423,9 @@ class DateTimeFieldTest extends DateTestBase {
|
|||
$this->assertOptionByText("edit-$field_name-0-value-ampm", t('AM/PM'));
|
||||
|
||||
// Submit a valid date and ensure it is accepted.
|
||||
$date_value = array('year' => 2012, 'month' => 12, 'day' => 31, 'hour' => 5, 'minute' => 15);
|
||||
$date_value = ['year' => 2012, 'month' => 12, 'day' => 31, 'hour' => 5, 'minute' => 15];
|
||||
|
||||
$edit = array();
|
||||
$edit = [];
|
||||
// Add the ampm indicator since we are testing 12 hour time.
|
||||
$date_value['ampm'] = 'am';
|
||||
foreach ($date_value as $part => $value) {
|
||||
|
@ -427,7 +435,7 @@ class DateTimeFieldTest extends DateTestBase {
|
|||
$this->drupalPostForm(NULL, $edit, t('Save'));
|
||||
preg_match('|entity_test/manage/(\d+)|', $this->url, $match);
|
||||
$id = $match[1];
|
||||
$this->assertText(t('entity_test @id has been created.', array('@id' => $id)));
|
||||
$this->assertText(t('entity_test @id has been created.', ['@id' => $id]));
|
||||
|
||||
$this->assertOptionSelected("edit-$field_name-0-value-year", '2012', 'Correct year selected.');
|
||||
$this->assertOptionSelected("edit-$field_name-0-value-month", '12', 'Correct month selected.');
|
||||
|
@ -438,14 +446,14 @@ class DateTimeFieldTest extends DateTestBase {
|
|||
|
||||
// Test the widget using increment other than 1 and 24 hour mode.
|
||||
entity_get_form_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'default')
|
||||
->setComponent($field_name, array(
|
||||
->setComponent($field_name, [
|
||||
'type' => 'datetime_datelist',
|
||||
'settings' => array(
|
||||
'settings' => [
|
||||
'increment' => 15,
|
||||
'date_order' => 'YMD',
|
||||
'time_type' => '24',
|
||||
),
|
||||
))
|
||||
],
|
||||
])
|
||||
->save();
|
||||
\Drupal::entityManager()->clearCachedFieldDefinitions();
|
||||
|
||||
|
@ -458,9 +466,9 @@ class DateTimeFieldTest extends DateTestBase {
|
|||
$this->assertNoFieldByXPath("//*[@id=\"edit-$field_name-0-value-ampm\"]", NULL, 'AMPM element not found.');
|
||||
|
||||
// Submit a valid date and ensure it is accepted.
|
||||
$date_value = array('year' => 2012, 'month' => 12, 'day' => 31, 'hour' => 17, 'minute' => 15);
|
||||
$date_value = ['year' => 2012, 'month' => 12, 'day' => 31, 'hour' => 17, 'minute' => 15];
|
||||
|
||||
$edit = array();
|
||||
$edit = [];
|
||||
foreach ($date_value as $part => $value) {
|
||||
$edit["{$field_name}[0][value][$part]"] = $value;
|
||||
}
|
||||
|
@ -468,7 +476,7 @@ class DateTimeFieldTest extends DateTestBase {
|
|||
$this->drupalPostForm(NULL, $edit, t('Save'));
|
||||
preg_match('|entity_test/manage/(\d+)|', $this->url, $match);
|
||||
$id = $match[1];
|
||||
$this->assertText(t('entity_test @id has been created.', array('@id' => $id)));
|
||||
$this->assertText(t('entity_test @id has been created.', ['@id' => $id]));
|
||||
|
||||
$this->assertOptionSelected("edit-$field_name-0-value-year", '2012', 'Correct year selected.');
|
||||
$this->assertOptionSelected("edit-$field_name-0-value-month", '12', 'Correct month selected.');
|
||||
|
@ -478,14 +486,14 @@ class DateTimeFieldTest extends DateTestBase {
|
|||
|
||||
// Test the widget for partial completion of fields.
|
||||
entity_get_form_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'default')
|
||||
->setComponent($field_name, array(
|
||||
->setComponent($field_name, [
|
||||
'type' => 'datetime_datelist',
|
||||
'settings' => array(
|
||||
'settings' => [
|
||||
'increment' => 1,
|
||||
'date_order' => 'YMD',
|
||||
'time_type' => '24',
|
||||
),
|
||||
))
|
||||
],
|
||||
])
|
||||
->save();
|
||||
\Drupal::entityManager()->clearCachedFieldDefinitions();
|
||||
|
||||
|
@ -497,7 +505,7 @@ class DateTimeFieldTest extends DateTestBase {
|
|||
$this->drupalGet('entity_test/add');
|
||||
|
||||
// Submit a partial date and ensure and error message is provided.
|
||||
$edit = array();
|
||||
$edit = [];
|
||||
foreach ($date_value as $part => $value) {
|
||||
$edit["{$field_name}[0][value][$part]"] = $value;
|
||||
}
|
||||
|
@ -512,8 +520,8 @@ class DateTimeFieldTest extends DateTestBase {
|
|||
// Test the widget for complete input with zeros as part of selections.
|
||||
$this->drupalGet('entity_test/add');
|
||||
|
||||
$date_value = array('year' => 2012, 'month' => '12', 'day' => '31', 'hour' => '0', 'minute' => '0');
|
||||
$edit = array();
|
||||
$date_value = ['year' => 2012, 'month' => '12', 'day' => '31', 'hour' => '0', 'minute' => '0'];
|
||||
$edit = [];
|
||||
foreach ($date_value as $part => $value) {
|
||||
$edit["{$field_name}[0][value][$part]"] = $value;
|
||||
}
|
||||
|
@ -522,13 +530,13 @@ class DateTimeFieldTest extends DateTestBase {
|
|||
$this->assertResponse(200);
|
||||
preg_match('|entity_test/manage/(\d+)|', $this->url, $match);
|
||||
$id = $match[1];
|
||||
$this->assertText(t('entity_test @id has been created.', array('@id' => $id)));
|
||||
$this->assertText(t('entity_test @id has been created.', ['@id' => $id]));
|
||||
|
||||
// Test the widget to ensure zeros are not deselected on validation.
|
||||
$this->drupalGet('entity_test/add');
|
||||
|
||||
$date_value = array('year' => 2012, 'month' => '12', 'day' => '31', 'hour' => '', 'minute' => '0');
|
||||
$edit = array();
|
||||
$date_value = ['year' => 2012, 'month' => '12', 'day' => '31', 'hour' => '', 'minute' => '0'];
|
||||
$edit = [];
|
||||
foreach ($date_value as $part => $value) {
|
||||
$edit["{$field_name}[0][value][$part]"] = $value;
|
||||
}
|
||||
|
@ -574,18 +582,18 @@ class DateTimeFieldTest extends DateTestBase {
|
|||
/**
|
||||
* Test default value functionality.
|
||||
*/
|
||||
function testDefaultValue() {
|
||||
public function testDefaultValue() {
|
||||
// Create a test content type.
|
||||
$this->drupalCreateContentType(array('type' => 'date_content'));
|
||||
$this->drupalCreateContentType(['type' => 'date_content']);
|
||||
|
||||
// Create a field storage with settings to validate.
|
||||
$field_name = Unicode::strtolower($this->randomMachineName());
|
||||
$field_storage = FieldStorageConfig::create(array(
|
||||
$field_storage = FieldStorageConfig::create([
|
||||
'field_name' => $field_name,
|
||||
'entity_type' => 'node',
|
||||
'type' => 'datetime',
|
||||
'settings' => array('datetime_type' => 'date'),
|
||||
));
|
||||
'settings' => ['datetime_type' => 'date'],
|
||||
]);
|
||||
$field_storage->save();
|
||||
|
||||
$field = FieldConfig::create([
|
||||
|
@ -601,9 +609,9 @@ class DateTimeFieldTest extends DateTestBase {
|
|||
$this->setSiteTimezone($timezone);
|
||||
|
||||
// Set now as default_value.
|
||||
$field_edit = array(
|
||||
$field_edit = [
|
||||
'default_value_input[default_date_type]' => 'now',
|
||||
);
|
||||
];
|
||||
$this->drupalPostForm('admin/structure/types/manage/date_content/fields/node.date_content.' . $field_name, $field_edit, t('Save settings'));
|
||||
|
||||
// Check that default value is selected in default value form.
|
||||
|
@ -614,10 +622,10 @@ class DateTimeFieldTest extends DateTestBase {
|
|||
// Check if default_date has been stored successfully.
|
||||
$config_entity = $this->config('field.field.node.date_content.' . $field_name)
|
||||
->get();
|
||||
$this->assertEqual($config_entity['default_value'][0], array(
|
||||
$this->assertEqual($config_entity['default_value'][0], [
|
||||
'default_date_type' => 'now',
|
||||
'default_date' => 'now',
|
||||
), 'Default value has been stored successfully');
|
||||
], 'Default value has been stored successfully');
|
||||
|
||||
// Clear field cache in order to avoid stale cache values.
|
||||
\Drupal::entityManager()->clearCachedFieldDefinitions();
|
||||
|
@ -629,19 +637,19 @@ class DateTimeFieldTest extends DateTestBase {
|
|||
->offsetGet(0)->value, $expected_date->format(DATETIME_DATE_STORAGE_FORMAT));
|
||||
|
||||
// Set an invalid relative default_value to test validation.
|
||||
$field_edit = array(
|
||||
$field_edit = [
|
||||
'default_value_input[default_date_type]' => 'relative',
|
||||
'default_value_input[default_date]' => 'invalid date',
|
||||
);
|
||||
];
|
||||
$this->drupalPostForm('admin/structure/types/manage/date_content/fields/node.date_content.' . $field_name, $field_edit, t('Save settings'));
|
||||
|
||||
$this->assertText('The relative date value entered is invalid.');
|
||||
|
||||
// Set a relative default_value.
|
||||
$field_edit = array(
|
||||
$field_edit = [
|
||||
'default_value_input[default_date_type]' => 'relative',
|
||||
'default_value_input[default_date]' => '+90 days',
|
||||
);
|
||||
];
|
||||
$this->drupalPostForm('admin/structure/types/manage/date_content/fields/node.date_content.' . $field_name, $field_edit, t('Save settings'));
|
||||
|
||||
// Check that default value is selected in default value form.
|
||||
|
@ -652,10 +660,10 @@ class DateTimeFieldTest extends DateTestBase {
|
|||
// Check if default_date has been stored successfully.
|
||||
$config_entity = $this->config('field.field.node.date_content.' . $field_name)
|
||||
->get();
|
||||
$this->assertEqual($config_entity['default_value'][0], array(
|
||||
$this->assertEqual($config_entity['default_value'][0], [
|
||||
'default_date_type' => 'relative',
|
||||
'default_date' => '+90 days',
|
||||
), 'Default value has been stored successfully');
|
||||
], 'Default value has been stored successfully');
|
||||
|
||||
// Clear field cache in order to avoid stale cache values.
|
||||
\Drupal::entityManager()->clearCachedFieldDefinitions();
|
||||
|
@ -668,9 +676,9 @@ class DateTimeFieldTest extends DateTestBase {
|
|||
->offsetGet(0)->value, $expected_date->format(DATETIME_DATE_STORAGE_FORMAT));
|
||||
|
||||
// Remove default value.
|
||||
$field_edit = array(
|
||||
$field_edit = [
|
||||
'default_value_input[default_date_type]' => '',
|
||||
);
|
||||
];
|
||||
$this->drupalPostForm('admin/structure/types/manage/date_content/fields/node.date_content.' . $field_name, $field_edit, t('Save settings'));
|
||||
|
||||
// Check that default value is selected in default value form.
|
||||
|
@ -696,7 +704,7 @@ class DateTimeFieldTest extends DateTestBase {
|
|||
/**
|
||||
* Test that invalid values are caught and marked as invalid.
|
||||
*/
|
||||
function testInvalidField() {
|
||||
public function testInvalidField() {
|
||||
// Change the field to a datetime field.
|
||||
$this->fieldStorage->setSetting('datetime_type', 'datetime');
|
||||
$this->fieldStorage->save();
|
||||
|
@ -709,72 +717,72 @@ class DateTimeFieldTest extends DateTestBase {
|
|||
|
||||
// Submit invalid dates and ensure they is not accepted.
|
||||
$date_value = '';
|
||||
$edit = array(
|
||||
$edit = [
|
||||
"{$field_name}[0][value][date]" => $date_value,
|
||||
"{$field_name}[0][value][time]" => '12:00:00',
|
||||
);
|
||||
];
|
||||
$this->drupalPostForm(NULL, $edit, t('Save'));
|
||||
$this->assertText('date is invalid', 'Empty date value has been caught.');
|
||||
|
||||
$date_value = 'aaaa-12-01';
|
||||
$edit = array(
|
||||
$edit = [
|
||||
"{$field_name}[0][value][date]" => $date_value,
|
||||
"{$field_name}[0][value][time]" => '00:00:00',
|
||||
);
|
||||
];
|
||||
$this->drupalPostForm(NULL, $edit, t('Save'));
|
||||
$this->assertText('date is invalid', format_string('Invalid year value %date has been caught.', array('%date' => $date_value)));
|
||||
$this->assertText('date is invalid', format_string('Invalid year value %date has been caught.', ['%date' => $date_value]));
|
||||
|
||||
$date_value = '2012-75-01';
|
||||
$edit = array(
|
||||
$edit = [
|
||||
"{$field_name}[0][value][date]" => $date_value,
|
||||
"{$field_name}[0][value][time]" => '00:00:00',
|
||||
);
|
||||
];
|
||||
$this->drupalPostForm(NULL, $edit, t('Save'));
|
||||
$this->assertText('date is invalid', format_string('Invalid month value %date has been caught.', array('%date' => $date_value)));
|
||||
$this->assertText('date is invalid', format_string('Invalid month value %date has been caught.', ['%date' => $date_value]));
|
||||
|
||||
$date_value = '2012-12-99';
|
||||
$edit = array(
|
||||
$edit = [
|
||||
"{$field_name}[0][value][date]" => $date_value,
|
||||
"{$field_name}[0][value][time]" => '00:00:00',
|
||||
);
|
||||
];
|
||||
$this->drupalPostForm(NULL, $edit, t('Save'));
|
||||
$this->assertText('date is invalid', format_string('Invalid day value %date has been caught.', array('%date' => $date_value)));
|
||||
$this->assertText('date is invalid', format_string('Invalid day value %date has been caught.', ['%date' => $date_value]));
|
||||
|
||||
$date_value = '2012-12-01';
|
||||
$time_value = '';
|
||||
$edit = array(
|
||||
$edit = [
|
||||
"{$field_name}[0][value][date]" => $date_value,
|
||||
"{$field_name}[0][value][time]" => $time_value,
|
||||
);
|
||||
];
|
||||
$this->drupalPostForm(NULL, $edit, t('Save'));
|
||||
$this->assertText('date is invalid', 'Empty time value has been caught.');
|
||||
|
||||
$date_value = '2012-12-01';
|
||||
$time_value = '49:00:00';
|
||||
$edit = array(
|
||||
$edit = [
|
||||
"{$field_name}[0][value][date]" => $date_value,
|
||||
"{$field_name}[0][value][time]" => $time_value,
|
||||
);
|
||||
];
|
||||
$this->drupalPostForm(NULL, $edit, t('Save'));
|
||||
$this->assertText('date is invalid', format_string('Invalid hour value %time has been caught.', array('%time' => $time_value)));
|
||||
$this->assertText('date is invalid', format_string('Invalid hour value %time has been caught.', ['%time' => $time_value]));
|
||||
|
||||
$date_value = '2012-12-01';
|
||||
$time_value = '12:99:00';
|
||||
$edit = array(
|
||||
$edit = [
|
||||
"{$field_name}[0][value][date]" => $date_value,
|
||||
"{$field_name}[0][value][time]" => $time_value,
|
||||
);
|
||||
];
|
||||
$this->drupalPostForm(NULL, $edit, t('Save'));
|
||||
$this->assertText('date is invalid', format_string('Invalid minute value %time has been caught.', array('%time' => $time_value)));
|
||||
$this->assertText('date is invalid', format_string('Invalid minute value %time has been caught.', ['%time' => $time_value]));
|
||||
|
||||
$date_value = '2012-12-01';
|
||||
$time_value = '12:15:99';
|
||||
$edit = array(
|
||||
$edit = [
|
||||
"{$field_name}[0][value][date]" => $date_value,
|
||||
"{$field_name}[0][value][time]" => $time_value,
|
||||
);
|
||||
];
|
||||
$this->drupalPostForm(NULL, $edit, t('Save'));
|
||||
$this->assertText('date is invalid', format_string('Invalid second value %time has been caught.', array('%time' => $time_value)));
|
||||
$this->assertText('date is invalid', format_string('Invalid second value %time has been caught.', ['%time' => $time_value]));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -4,6 +4,7 @@ namespace Drupal\Tests\datetime\Kernel;
|
|||
|
||||
use Drupal\Core\Field\FieldItemListInterface;
|
||||
use Drupal\Core\Field\FieldItemInterface;
|
||||
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
|
||||
use Drupal\entity_test\Entity\EntityTest;
|
||||
use Drupal\field\Entity\FieldConfig;
|
||||
use Drupal\Tests\field\Kernel\FieldKernelTestBase;
|
||||
|
@ -16,44 +17,61 @@ use Drupal\field\Entity\FieldStorageConfig;
|
|||
*/
|
||||
class DateTimeItemTest extends FieldKernelTestBase {
|
||||
|
||||
/**
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('datetime');
|
||||
public static $modules = ['datetime'];
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
// Create a field with settings to validate.
|
||||
$field_storage = FieldStorageConfig::create(array(
|
||||
$this->fieldStorage = FieldStorageConfig::create([
|
||||
'field_name' => 'field_datetime',
|
||||
'type' => 'datetime',
|
||||
'entity_type' => 'entity_test',
|
||||
'settings' => array('datetime_type' => 'date'),
|
||||
));
|
||||
$field_storage->save();
|
||||
$field = FieldConfig::create([
|
||||
'field_storage' => $field_storage,
|
||||
'bundle' => 'entity_test',
|
||||
'settings' => array(
|
||||
'default_value' => 'blank',
|
||||
),
|
||||
'settings' => ['datetime_type' => DateTimeItem::DATETIME_TYPE_DATETIME],
|
||||
]);
|
||||
$field->save();
|
||||
$this->fieldStorage->save();
|
||||
$this->field = FieldConfig::create([
|
||||
'field_storage' => $this->fieldStorage,
|
||||
'bundle' => 'entity_test',
|
||||
'settings' => [
|
||||
'default_value' => 'blank',
|
||||
],
|
||||
]);
|
||||
$this->field->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests using entity fields of the date field type.
|
||||
* Tests using entity fields of the datetime field type.
|
||||
*/
|
||||
public function testDateTimeItem() {
|
||||
public function testDateTime() {
|
||||
$this->fieldStorage->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATETIME);
|
||||
$this->fieldStorage->save();
|
||||
|
||||
// Verify entity creation.
|
||||
$entity = EntityTest::create();
|
||||
$value = '2014-01-01T20:00:00Z';
|
||||
$value = '2014-01-01T20:00:00';
|
||||
$entity->field_datetime = $value;
|
||||
$entity->name->value = $this->randomMachineName();
|
||||
$entity->save();
|
||||
$this->entityValidateAndSave($entity);
|
||||
|
||||
// Verify entity has been created properly.
|
||||
$id = $entity->id();
|
||||
|
@ -64,12 +82,50 @@ class DateTimeItemTest extends FieldKernelTestBase {
|
|||
$this->assertEqual($entity->field_datetime[0]->value, $value);
|
||||
|
||||
// Verify changing the date value.
|
||||
$new_value = $this->randomMachineName();
|
||||
$new_value = '2016-11-04T00:21:00';
|
||||
$entity->field_datetime->value = $new_value;
|
||||
$this->assertEqual($entity->field_datetime->value, $new_value);
|
||||
|
||||
// Read changed entity and assert changed values.
|
||||
$entity->save();
|
||||
$this->entityValidateAndSave($entity);
|
||||
$entity = EntityTest::load($id);
|
||||
$this->assertEqual($entity->field_datetime->value, $new_value);
|
||||
|
||||
// Test the generateSampleValue() method.
|
||||
$entity = EntityTest::create();
|
||||
$entity->field_datetime->generateSampleItems();
|
||||
$this->entityValidateAndSave($entity);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests using entity fields of the date field type.
|
||||
*/
|
||||
public function testDateOnly() {
|
||||
$this->fieldStorage->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATE);
|
||||
$this->fieldStorage->save();
|
||||
|
||||
// Verify entity creation.
|
||||
$entity = EntityTest::create();
|
||||
$value = '2014-01-01';
|
||||
$entity->field_datetime = $value;
|
||||
$entity->name->value = $this->randomMachineName();
|
||||
$this->entityValidateAndSave($entity);
|
||||
|
||||
// Verify entity has been created properly.
|
||||
$id = $entity->id();
|
||||
$entity = EntityTest::load($id);
|
||||
$this->assertTrue($entity->field_datetime instanceof FieldItemListInterface, 'Field implements interface.');
|
||||
$this->assertTrue($entity->field_datetime[0] instanceof FieldItemInterface, 'Field item implements interface.');
|
||||
$this->assertEqual($entity->field_datetime->value, $value);
|
||||
$this->assertEqual($entity->field_datetime[0]->value, $value);
|
||||
|
||||
// Verify changing the date value.
|
||||
$new_value = '2016-11-04';
|
||||
$entity->field_datetime->value = $new_value;
|
||||
$this->assertEqual($entity->field_datetime->value, $new_value);
|
||||
|
||||
// Read changed entity and assert changed values.
|
||||
$this->entityValidateAndSave($entity);
|
||||
$entity = EntityTest::load($id);
|
||||
$this->assertEqual($entity->field_datetime->value, $new_value);
|
||||
|
||||
|
@ -83,11 +139,15 @@ class DateTimeItemTest extends FieldKernelTestBase {
|
|||
* Tests DateTimeItem::setValue().
|
||||
*/
|
||||
public function testSetValue() {
|
||||
// Test a date+time field.
|
||||
$this->fieldStorage->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATETIME);
|
||||
$this->fieldStorage->save();
|
||||
|
||||
// Test DateTimeItem::setValue() using string.
|
||||
$entity = EntityTest::create();
|
||||
$value = '2014-01-01T20:00:00Z';
|
||||
$value = '2014-01-01T20:00:00';
|
||||
$entity->get('field_datetime')->set(0, $value);
|
||||
$entity->save();
|
||||
$this->entityValidateAndSave($entity);
|
||||
// Load the entity and ensure the field was saved correctly.
|
||||
$id = $entity->id();
|
||||
$entity = EntityTest::load($id);
|
||||
|
@ -95,9 +155,33 @@ class DateTimeItemTest extends FieldKernelTestBase {
|
|||
|
||||
// Test DateTimeItem::setValue() using property array.
|
||||
$entity = EntityTest::create();
|
||||
$value = '2014-01-01T20:00:00Z';
|
||||
$value = '2014-01-01T20:00:00';
|
||||
$entity->set('field_datetime', $value);
|
||||
$entity->save();
|
||||
$this->entityValidateAndSave($entity);
|
||||
// Load the entity and ensure the field was saved correctly.
|
||||
$id = $entity->id();
|
||||
$entity = EntityTest::load($id);
|
||||
$this->assertEqual($entity->field_datetime[0]->value, $value, 'DateTimeItem::setValue() works with array value.');
|
||||
|
||||
// Test a date-only field.
|
||||
$this->fieldStorage->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATE);
|
||||
$this->fieldStorage->save();
|
||||
|
||||
// Test DateTimeItem::setValue() using string.
|
||||
$entity = EntityTest::create();
|
||||
$value = '2014-01-01';
|
||||
$entity->get('field_datetime')->set(0, $value);
|
||||
$this->entityValidateAndSave($entity);
|
||||
// Load the entity and ensure the field was saved correctly.
|
||||
$id = $entity->id();
|
||||
$entity = EntityTest::load($id);
|
||||
$this->assertEqual($entity->field_datetime[0]->value, $value, 'DateTimeItem::setValue() works with string value.');
|
||||
|
||||
// Test DateTimeItem::setValue() using property array.
|
||||
$entity = EntityTest::create();
|
||||
$value = '2014-01-01';
|
||||
$entity->set('field_datetime', $value);
|
||||
$this->entityValidateAndSave($entity);
|
||||
// Load the entity and ensure the field was saved correctly.
|
||||
$id = $entity->id();
|
||||
$entity = EntityTest::load($id);
|
||||
|
@ -108,12 +192,29 @@ class DateTimeItemTest extends FieldKernelTestBase {
|
|||
* Tests setting the value of the DateTimeItem directly.
|
||||
*/
|
||||
public function testSetValueProperty() {
|
||||
// Test Date::setValue().
|
||||
// Test Date::setValue() with a date+time field.
|
||||
// Test a date+time field.
|
||||
$this->fieldStorage->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATETIME);
|
||||
$this->fieldStorage->save();
|
||||
$entity = EntityTest::create();
|
||||
$value = '2014-01-01T20:00:00Z';
|
||||
$value = '2014-01-01T20:00:00';
|
||||
|
||||
$entity->set('field_datetime', $value);
|
||||
$entity->save();
|
||||
$this->entityValidateAndSave($entity);
|
||||
// Load the entity and ensure the field was saved correctly.
|
||||
$id = $entity->id();
|
||||
$entity = EntityTest::load($id);
|
||||
$this->assertEqual($entity->field_datetime[0]->value, $value, '"Value" property can be set directly.');
|
||||
|
||||
// Test Date::setValue() with a date-only field.
|
||||
// Test a date+time field.
|
||||
$this->fieldStorage->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATE);
|
||||
$this->fieldStorage->save();
|
||||
$entity = EntityTest::create();
|
||||
$value = '2014-01-01';
|
||||
|
||||
$entity->set('field_datetime', $value);
|
||||
$this->entityValidateAndSave($entity);
|
||||
// Load the entity and ensure the field was saved correctly.
|
||||
$id = $entity->id();
|
||||
$entity = EntityTest::load($id);
|
||||
|
|
Reference in a new issue