Commit 935fd2c4 authored by alexpott's avatar alexpott

Issue #2510348 by justAChris, NickWilde, Calystod, mpdonadio: Datetime select...

Issue #2510348 by justAChris, NickWilde, Calystod, mpdonadio: Datetime select list widget has options for time interval on date only mode
parent 0ba2b184
......@@ -40,8 +40,15 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
$element = parent::formElement($items, $delta, $element, $form, $form_state);
$date_order = $this->getSetting('date_order');
$time_type = $this->getSetting('time_type');
$increment = $this->getSetting('increment');
if ($this->getFieldSetting('datetime_type') == 'datetime') {
$time_type = $this->getSetting('time_type');
$increment = $this->getSetting('increment');
}
else {
$time_type = '';
$increment = '';
}
// Set up the date part order array.
switch ($date_order) {
......@@ -99,25 +106,31 @@ function settingsForm(array $form, FormStateInterface $form_state) {
'#default_value' => $this->getSetting('time_type'),
'#options' => array('24' => t('24 hour time'), '12' => t('12 hour time')),
);
$element['increment'] = [
'#type' => 'select',
'#title' => t('Time increments'),
'#default_value' => $this->getSetting('increment'),
'#options' => [
1 => t('1 minute'),
5 => t('5 minute'),
10 => t('10 minute'),
15 => t('15 minute'),
30 => t('30 minute'),
],
];
}
else {
$element['time_type'] = array(
'#type' => 'hidden',
'#value' => 'none',
);
}
$element['increment'] = array(
'#type' => 'select',
'#title' => t('Time increments'),
'#default_value' => $this->getSetting('increment'),
'#options' => array(
1 => t('1 minute'),
5 => t('5 minute'),
10 => t('10 minute'),
15 => t('15 minute'),
30 => t('30 minute')),
);
$element['increment'] = [
'#type' => 'hidden',
'#value' => $this->getSetting('increment'),
];
}
return $element;
}
......@@ -131,8 +144,8 @@ public function settingsSummary() {
$summary[] = t('Date part order: !order', array('!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 increments: !increment', array('!increment' => $this->getSetting('increment')));
return $summary;
}
......
......@@ -69,6 +69,7 @@ protected function setUp() {
'access content',
'view test entity',
'administer entity_test content',
'administer entity_test form display',
'administer content types',
'administer node fields',
));
......@@ -383,6 +384,38 @@ function testDatetimeField() {
*/
function testDatelistWidget() {
$field_name = $this->fieldStorage->getName();
// Ensure field is set to a date only field.
$this->fieldStorage->setSetting('datetime_type', 'date');
$this->fieldStorage->save();
// Change the widget to a datelist widget.
entity_get_form_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'default')
->setComponent($field_name, array(
'type' => 'datetime_datelist',
'settings' => array(
'date_order' => 'YMD',
),
))
->save();
\Drupal::entityManager()->clearCachedFieldDefinitions();
// Display creation form.
$this->drupalGet('entity_test/add');
// 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.');
$this->assertNoFieldByXPath("//*[@id=\"edit-$field_name-0-value-minute\"]", NULL, 'Minute element not found on Date Only.');
// Go to the form display page to assert that increment option does not appear on Date Only
$fieldEditUrl = 'entity_test/structure/entity_test/form-display';
$this->drupalGet($fieldEditUrl);
// Click on the widget settings button to open the widget settings form.
$this->drupalPostAjaxForm(NULL, array(), $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.');
// Change the field to a datetime field.
$this->fieldStorage->setSetting('datetime_type', 'datetime');
$this->fieldStorage->save();
......@@ -400,6 +433,14 @@ function testDatelistWidget() {
->save();
\Drupal::entityManager()->clearCachedFieldDefinitions();
// Go to the form display page to assert that increment option does appear on Date Time
$fieldEditUrl = 'entity_test/structure/entity_test/form-display';
$this->drupalGet($fieldEditUrl);
// Click on the widget settings button to open the widget settings form.
$this->drupalPostAjaxForm(NULL, array(), $field_name . "_settings_edit");
$this->assertFieldByXPath($xpathIncr, NULL, 'Increment element found for Date and time.');
// Display creation form.
$this->drupalGet('entity_test/add');
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment