UuidItem.php 1.15 KB
Newer Older
1 2
<?php

3
namespace Drupal\Core\Field\Plugin\Field\FieldType;
4

5 6
use Drupal\Core\Field\FieldStorageDefinitionInterface;

7
/**
8
 * Defines the 'uuid' entity field type.
9 10 11
 *
 * The field uses a newly generated UUID as default value.
 *
12 13 14
 * @FieldType(
 *   id = "uuid",
 *   label = @Translation("UUID"),
15
 *   description = @Translation("An entity field containing a UUID."),
16 17
 *   no_ui = TRUE,
 *   default_formatter = "string"
18 19 20 21
 * )
 */
class UuidItem extends StringItem {

22 23 24
  /**
   * {@inheritdoc}
   */
25
  public static function defaultStorageSettings() {
26 27
    return array(
      'max_length' => 128,
28
      'is_ascii' => TRUE,
29
    ) + parent::defaultStorageSettings();
30 31
  }

32 33 34 35 36
  /**
   * {@inheritdoc}
   */
  public function applyDefaultValue($notify = TRUE) {
    // Default to one field item with a generated UUID.
37
    $uuid = \Drupal::service('uuid');
38 39 40
    $this->setValue(array('value' => $uuid->generate()), $notify);
    return $this;
  }
41 42 43 44 45 46 47 48 49 50

  /**
   * {@inheritdoc}
   */
  public static function schema(FieldStorageDefinitionInterface $field_definition) {
    $schema = parent::schema($field_definition);
    $schema['unique keys']['value'] = array('value');
    return $schema;
  }

51
}