UuidItem.php 1.17 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\Core\Entity\Plugin\Field\FieldType\UuidItem.
6 7
 */

8
namespace Drupal\Core\Field\Plugin\Field\FieldType;
9

10 11
use Drupal\Core\Field\FieldStorageDefinitionInterface;

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

26 27 28
  /**
   * {@inheritdoc}
   */
29
  public static function defaultStorageSettings() {
30 31
    return array(
      'max_length' => 128,
32
    ) + parent::defaultStorageSettings();
33 34
  }

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

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

54
}