EntityTestUuidId.php
Namespace
Drupal\entity_test\EntityFile
-
core/
modules/ system/ tests/ modules/ entity_test/ src/ Entity/ EntityTestUuidId.php
View source
<?php
declare (strict_types=1);
namespace Drupal\entity_test\Entity;
use Drupal\Component\Uuid\UuidInterface;
use Drupal\Core\Entity\Attribute\ContentEntityType;
use Drupal\Core\Entity\Routing\DefaultHtmlRouteProvider;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\entity_test\EntityTestAccessControlHandler;
use Drupal\entity_test\EntityTestForm;
/**
* Defines a test entity class with UUIDs as IDs.
*/
class EntityTestUuidId extends EntityTest {
/**
* {@inheritdoc}
*/
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
// Configure a string field to match the UUID field configuration and use it
// for both the ID and the UUID key. The UUID field type cannot be used
// because it would add a unique key to the data table.
$fields[$entity_type->getKey('uuid')] = BaseFieldDefinition::create('string')->setLabel(new TranslatableMarkup('UUID'))
->setSetting('max_length', 128)
->setSetting('is_ascii', TRUE)
->setDefaultValueCallback(static::class . '::generateUuid');
return $fields;
}
/**
* Statically generates a UUID.
*
* @return string
* A newly generated UUID.
*/
public static function generateUuid() : string {
$uuid = \Drupal::service('uuid');
assert($uuid instanceof UuidInterface);
return $uuid->generate();
}
}
Classes
Title | Deprecated | Summary |
---|---|---|
EntityTestUuidId | Defines a test entity class with UUIDs as IDs. |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.