diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/TimestampItem.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/TimestampItem.php index 8c3ffd21192..a5d6f38e2b5 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/TimestampItem.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/TimestampItem.php @@ -2,8 +2,9 @@ namespace Drupal\Core\Field\Plugin\Field\FieldType; -use Drupal\Core\Field\FieldStorageDefinitionInterface; +use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldItemBase; +use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\Core\TypedData\DataDefinition; /** @@ -53,4 +54,14 @@ class TimestampItem extends FieldItemBase { ]; } + /** + * {@inheritdoc} + */ + public static function generateSampleValue(FieldDefinitionInterface $field_definition) { + // Pick a random timestamp in the past year. + $timestamp = \Drupal::time()->getRequestTime() - mt_rand(0, 86400 * 365); + $values['value'] = $timestamp; + return $values; + } + } diff --git a/core/modules/field/tests/src/Kernel/Timestamp/TimestampItemTest.php b/core/modules/field/tests/src/Kernel/Timestamp/TimestampItemTest.php new file mode 100644 index 00000000000..0aaa8d3414d --- /dev/null +++ b/core/modules/field/tests/src/Kernel/Timestamp/TimestampItemTest.php @@ -0,0 +1,91 @@ +fieldStorage = FieldStorageConfig::create([ + 'field_name' => 'field_timestamp', + 'type' => 'timestamp', + 'entity_type' => 'entity_test', + ]); + $this->fieldStorage->save(); + $this->field = FieldConfig::create([ + 'field_storage' => $this->fieldStorage, + 'bundle' => 'entity_test', + ]); + $this->field->save(); + } + + /** + * Tests using entity fields of the datetime field type. + */ + public function testDateTime() { + // Verify entity creation. + $entity = EntityTest::create(); + $value = 1488914208; + $entity->field_timestamp = $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_timestamp instanceof FieldItemListInterface, 'Field implements interface.'); + $this->assertTrue($entity->field_timestamp[0] instanceof FieldItemInterface, 'Field item implements interface.'); + $this->assertEquals($entity->field_timestamp->value, $value); + $this->assertEquals($entity->field_timestamp[0]->value, $value); + + // Verify changing the date value. + $new_value = 1488914000; + $entity->field_timestamp->value = $new_value; + $this->assertEquals($entity->field_timestamp->value, $new_value); + + // Read changed entity and assert changed values. + $this->entityValidateAndSave($entity); + $entity = EntityTest::load($id); + $this->assertEquals($entity->field_timestamp->value, $new_value); + + // Test sample item generation. + $entity = EntityTest::create(); + $entity->field_timestamp->generateSampleItems(); + $this->entityValidateAndSave($entity); + + // Ensure there is sample value a generated for the field. + $this->assertNotNull($entity->field_timestamp->value); + } + +}