UuidItem.php 1.21 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 22
 *   no_ui = TRUE,
 *   default_formatter = "string"
23 24 25 26
 * )
 */
class UuidItem extends StringItem {

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

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

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

55
}