UuidItem.php

Same filename in other branches
  1. 9 core/lib/Drupal/Core/Field/Plugin/Field/FieldType/UuidItem.php
  2. 8.9.x core/lib/Drupal/Core/Field/Plugin/Field/FieldType/UuidItem.php
  3. 11.x core/lib/Drupal/Core/Field/Plugin/Field/FieldType/UuidItem.php

Namespace

Drupal\Core\Field\Plugin\Field\FieldType

File

core/lib/Drupal/Core/Field/Plugin/Field/FieldType/UuidItem.php

View source
<?php

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

use Drupal\Core\Field\Attribute\FieldType;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;

/**
 * Defines the 'uuid' entity field type.
 *
 * The field uses a newly generated UUID as default value.
 */
class UuidItem extends StringItem {
    
    /**
     * {@inheritdoc}
     */
    public static function defaultStorageSettings() {
        return [
            'max_length' => 128,
            'is_ascii' => TRUE,
        ] + parent::defaultStorageSettings();
    }
    
    /**
     * {@inheritdoc}
     */
    public function applyDefaultValue($notify = TRUE) {
        // Default to one field item with a generated UUID.
        $uuid = \Drupal::service('uuid');
        $this->setValue([
            'value' => $uuid->generate(),
        ], $notify);
        return $this;
    }
    
    /**
     * {@inheritdoc}
     */
    public static function schema(FieldStorageDefinitionInterface $field_definition) {
        $schema = parent::schema($field_definition);
        $schema['unique keys']['value'] = [
            'value',
        ];
        return $schema;
    }
    
    /**
     * {@inheritdoc}
     */
    public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
        $values['value'] = \Drupal::service('uuid')->generate();
        return $values;
    }

}

Classes

Title Deprecated Summary
UuidItem Defines the 'uuid' entity field type.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.