Name Generator Service¶
Service identity¶
- Service ID:
name.generator - Interface:
Drupal\name\Service\GeneratorInterface - Class:
Drupal\name\Service\GeneratorService
Use this service to generate realistic sample name values for tests, demos, and seed data.
Quick use¶
Common methods¶
generateSampleNames($limit = 3, ?FieldDefinitionInterface $field_definition = NULL)¶
Returns $limit generated name arrays. If you pass a field definition, results
respect that field's enabled components and length settings.
loadSampleValues($limit = 3, ?FieldDefinitionInterface $field_definition = NULL, $random = FALSE)¶
Returns example name arrays from name.generate.examples configuration (and
field-specific overrides when a field definition is provided).
$limit— maximum number of example records to return.$field_definition— when set, filters examples to enabled components.$random— whenTRUE, shuffles examples before slicing.
$generator = \Drupal::service('name.generator');
$examples = $generator->loadSampleValues(10, $field_definition, TRUE);
Injection example¶
use Drupal\name\Service\GeneratorInterface;
final class DemoNameSeeder {
public function __construct(
private readonly GeneratorInterface $generator,
) {}
public function buildNames(int $count): array {
return $this->generator->generateSampleNames($count);
}
}