diff --git a/core/modules/datetime/src/Plugin/Field/FieldType/DateTimeItem.php b/core/modules/datetime/src/Plugin/Field/FieldType/DateTimeItem.php index a7dd1c3d96a1f2eefa3cf225ba8be0e1779b78d5..3bfaa66d2d77b4edbec695bb77ceab881c04931a 100644 --- a/core/modules/datetime/src/Plugin/Field/FieldType/DateTimeItem.php +++ b/core/modules/datetime/src/Plugin/Field/FieldType/DateTimeItem.php @@ -97,6 +97,7 @@ public function storageSettingsForm(array &$form, FormStateInterface $form_state static::DATETIME_TYPE_DATETIME => t('Date and time'), static::DATETIME_TYPE_DATE => t('Date only'), ), + '#disabled' => $has_data, ); return $element; diff --git a/core/modules/datetime/src/Tests/DateTimeFieldTest.php b/core/modules/datetime/src/Tests/DateTimeFieldTest.php index a4067177b09d599993a663a8862f77c5299d0b37..b077425e6a08977678aed5200e920f9b5cb0e185 100644 --- a/core/modules/datetime/src/Tests/DateTimeFieldTest.php +++ b/core/modules/datetime/src/Tests/DateTimeFieldTest.php @@ -9,12 +9,12 @@ use Drupal\Component\Utility\SafeMarkup; use Drupal\Component\Utility\Unicode; -use Drupal\Core\Entity\Entity\EntityViewDisplay; use Drupal\Core\Datetime\DrupalDateTime; +use Drupal\Core\Entity\Entity\EntityViewDisplay; use Drupal\field\Entity\FieldConfig; +use Drupal\field\Entity\FieldStorageConfig; use Drupal\node\Entity\Node; use Drupal\simpletest\WebTestBase; -use Drupal\field\Entity\FieldStorageConfig; /** * Tests Datetime field functionality. @@ -804,6 +804,48 @@ function testInvalidField() { $this->assertText('date is invalid', format_string('Invalid second value %time has been caught.', array('%time' => $time_value))); } + /** + * Tests that 'Date' field storage setting form is disabled if field has data. + */ + public function testDateStorageSettings() { + // Create a test content type. + $this->drupalCreateContentType(['type' => 'date_content']); + + // Create a field storage with settings to validate. + $field_name = Unicode::strtolower($this->randomMachineName()); + $field_storage = FieldStorageConfig::create([ + 'field_name' => $field_name, + 'entity_type' => 'node', + 'type' => 'datetime', + 'settings' => [ + 'datetime_type' => 'date', + ], + ]); + $field_storage->save(); + $field = FieldConfig::create([ + 'field_storage' => $field_storage, + 'field_name' => $field_name, + 'bundle' => 'date_content', + ]); + $field->save(); + + entity_get_form_display('node', 'date_content', 'default') + ->setComponent($field_name, [ + 'type' => 'datetime_default', + ]) + ->save(); + $edit = [ + 'title[0][value]' => $this->randomString(), + 'body[0][value]' => $this->randomString(), + $field_name . '[0][value][date]' => '2016-04-01', + ]; + $this->drupalPostForm('node/add/date_content', $edit, t('Save')); + $this->drupalGet('admin/structure/types/manage/date_content/fields/node.date_content.' . $field_name . '/storage'); + $result = $this->xpath("//*[@id='edit-settings-datetime-type' and contains(@disabled, 'disabled')]"); + $this->assertEqual(count($result), 1, "Changing datetime setting is disabled."); + $this->assertText('There is data for this field in the database. The field settings can no longer be changed.'); + } + /** * Renders a entity_test and sets the output in the internal browser. *