Skip to content
Snippets Groups Projects
Select Git revision
  • 02cd2f439fbc7ed28a5424ebb67dc8fef1b1d176
  • 11.x default protected
  • 11.2.x protected
  • 10.5.x protected
  • 10.6.x protected
  • 11.1.x protected
  • 10.4.x protected
  • 11.0.x protected
  • 10.3.x protected
  • 7.x protected
  • 10.2.x protected
  • 10.1.x protected
  • 9.5.x protected
  • 10.0.x protected
  • 9.4.x protected
  • 9.3.x protected
  • 9.2.x protected
  • 9.1.x protected
  • 8.9.x protected
  • 9.0.x protected
  • 8.8.x protected
  • 10.5.1 protected
  • 11.2.2 protected
  • 11.2.1 protected
  • 11.2.0 protected
  • 10.5.0 protected
  • 11.2.0-rc2 protected
  • 10.5.0-rc1 protected
  • 11.2.0-rc1 protected
  • 10.4.8 protected
  • 11.1.8 protected
  • 10.5.0-beta1 protected
  • 11.2.0-beta1 protected
  • 11.2.0-alpha1 protected
  • 10.4.7 protected
  • 11.1.7 protected
  • 10.4.6 protected
  • 11.1.6 protected
  • 10.3.14 protected
  • 10.4.5 protected
  • 11.0.13 protected
41 results

TimestampItem.php

  • Lauri Eskola's avatar
    Issue #2802541 by dagmar, mpdonadio: Timestamp field type doesn't provide sample values
    Lauri Timmanee authored
    02cd2f43
    History
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    TimestampItem.php 1.59 KiB
    <?php
    
    namespace Drupal\Core\Field\Plugin\Field\FieldType;
    
    use Drupal\Core\Field\FieldDefinitionInterface;
    use Drupal\Core\Field\FieldItemBase;
    use Drupal\Core\Field\FieldStorageDefinitionInterface;
    use Drupal\Core\TypedData\DataDefinition;
    
    /**
     * Defines the 'timestamp' entity field type.
     *
     * @FieldType(
     *   id = "timestamp",
     *   label = @Translation("Timestamp"),
     *   description = @Translation("An entity field containing a UNIX timestamp value."),
     *   default_widget = "datetime_timestamp",
     *   default_formatter = "timestamp",
     *   constraints = {
     *     "ComplexData" = {
     *       "value" = {
     *         "Range" = {
     *           "min" = "-2147483648",
     *           "max" = "2147483648",
     *         }
     *       }
     *     }
     *   }
     * )
     * )
     */
    class TimestampItem extends FieldItemBase {
    
      /**
       * {@inheritdoc}
       */
      public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
        $properties['value'] = DataDefinition::create('timestamp')
          ->setLabel(t('Timestamp value'))
          ->setRequired(TRUE);
        return $properties;
      }
    
      /**
       * {@inheritdoc}
       */
      public static function schema(FieldStorageDefinitionInterface $field_definition) {
        return [
          'columns' => [
            'value' => [
              'type' => 'int',
            ],
          ],
        ];
      }
    
      /**
       * {@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;
      }
    
    }