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 / UuidItem.php
1 <?php
2
3 namespace Drupal\Core\Field\Plugin\Field\FieldType;
4
5 use Drupal\Core\Field\FieldStorageDefinitionInterface;
6 use Drupal\Core\Field\FieldDefinitionInterface;
7
8 /**
9  * Defines the 'uuid' entity field type.
10  *
11  * The field uses a newly generated UUID as default value.
12  *
13  * @FieldType(
14  *   id = "uuid",
15  *   label = @Translation("UUID"),
16  *   description = @Translation("An entity field containing a UUID."),
17  *   no_ui = TRUE,
18  *   default_formatter = "string"
19  * )
20  */
21 class UuidItem extends StringItem {
22
23   /**
24    * {@inheritdoc}
25    */
26   public static function defaultStorageSettings() {
27     return [
28       'max_length' => 128,
29       'is_ascii' => TRUE,
30     ] + parent::defaultStorageSettings();
31   }
32
33   /**
34    * {@inheritdoc}
35    */
36   public function applyDefaultValue($notify = TRUE) {
37     // Default to one field item with a generated UUID.
38     $uuid = \Drupal::service('uuid');
39     $this->setValue(['value' => $uuid->generate()], $notify);
40     return $this;
41   }
42
43   /**
44    * {@inheritdoc}
45    */
46   public static function schema(FieldStorageDefinitionInterface $field_definition) {
47     $schema = parent::schema($field_definition);
48     $schema['unique keys']['value'] = ['value'];
49     return $schema;
50   }
51
52   /**
53    * {@inheritdoc}
54    */
55   public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
56     $values['value'] = \Drupal::service('uuid')->generate();
57     return $values;
58   }
59
60 }