a5d6f38e2b57d8e0c42ea79dca39972363472756
[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  */
32 class TimestampItem extends FieldItemBase {
33
34   /**
35    * {@inheritdoc}
36    */
37   public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
38     $properties['value'] = DataDefinition::create('timestamp')
39       ->setLabel(t('Timestamp value'))
40       ->setRequired(TRUE);
41     return $properties;
42   }
43
44   /**
45    * {@inheritdoc}
46    */
47   public static function schema(FieldStorageDefinitionInterface $field_definition) {
48     return [
49       'columns' => [
50         'value' => [
51           'type' => 'int',
52         ],
53       ],
54     ];
55   }
56
57   /**
58    * {@inheritdoc}
59    */
60   public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
61     // Pick a random timestamp in the past year.
62     $timestamp = \Drupal::time()->getRequestTime() - mt_rand(0, 86400 * 365);
63     $values['value'] = $timestamp;
64     return $values;
65   }
66
67 }