Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / lib / Drupal / Core / Field / Plugin / Field / FieldType / TimestampItem.php
1 <?php
2
3 namespace Drupal\Core\Field\Plugin\Field\FieldType;
4
5 use Drupal\Core\Field\FieldDefinitionInterface;
6 use Drupal\Core\Field\FieldItemBase;
7 use Drupal\Core\Field\FieldStorageDefinitionInterface;
8 use Drupal\Core\TypedData\DataDefinition;
9
10 /**
11  * Defines the 'timestamp' entity field type.
12  *
13  * @FieldType(
14  *   id = "timestamp",
15  *   label = @Translation("Timestamp"),
16  *   description = @Translation("An entity field containing a UNIX timestamp value."),
17  *   default_widget = "datetime_timestamp",
18  *   default_formatter = "timestamp",
19  *   constraints = {
20  *     "ComplexData" = {
21  *       "value" = {
22  *         "Range" = {
23  *           "min" = "-2147483648",
24  *           "max" = "2147483648",
25  *         }
26  *       }
27  *     }
28  *   }
29  * )
30  */
31 class TimestampItem extends FieldItemBase {
32
33   /**
34    * {@inheritdoc}
35    */
36   public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
37     $properties['value'] = DataDefinition::create('timestamp')
38       ->setLabel(t('Timestamp value'))
39       ->setRequired(TRUE);
40     return $properties;
41   }
42
43   /**
44    * {@inheritdoc}
45    */
46   public static function schema(FieldStorageDefinitionInterface $field_definition) {
47     return [
48       'columns' => [
49         'value' => [
50           'type' => 'int',
51         ],
52       ],
53     ];
54   }
55
56   /**
57    * {@inheritdoc}
58    */
59   public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
60     // Pick a random timestamp in the past year.
61     $timestamp = \Drupal::time()->getRequestTime() - mt_rand(0, 86400 * 365);
62     $values['value'] = $timestamp;
63     return $values;
64   }
65
66 }