name
Name Field is a module that provides a multi-component name field for the Field API.
Features
- Select between one to five separate fields to capture a persons title, given (first), middle, family (surname), generational suffix and credentials (post-nominals).
- Name and name list formatters.
- Real / Display name: create an alternative screen name to the users login name. To enable this feature, simply add a name field to the user bundle and enable this feature when configuring the field.
- Select or Text field support for Titles and Generational suffixes.
- Optional autocomplete options for all text fields
- Easier styling with component CSS classes and optional RDFa and microdata markup in formatter output; the RDF UI module can help manage RDF mappings.
- Feeds, Migrate, Views (native and custom filter) and Devel Generate integration.
- Inline component markup formatting options. This can be found in the field formatting options.
- Formatter link options.
- Nickname / preferred field support in the format pattern tokens.
- Alternative field replacement support in the format pattern tokens, for use cases such as a maiden name.
Custom name format strings
These formatters can be as simple as E which will print either the family name, or the given name if the family name is empty.
They can be fairly complicated for specific requirements, such as t+ LG((f-,)+ (x-.)+ (y-.))+ (=\[Uc-\]). This prints the title, the family name with the first letter in uppercase, the initials of the first and middle names in uppercase, followed by the credentials in uppercase, wrapped in square brackets. This could print something like "Mr Doe, J. J. [B.SC., PH.D.]" for a person that has filled out all fields, to just "J." for a person that only filled out their first name.
Post a support request if you need help creating a custom name format string.
Developers
Easily format a name or name list with the name formatter service. The second argument to format() is the name format machine name; formatList() takes the name format and name list format machine names as its second and third arguments (for example default or full). Pass NULL as the optional last argument for the page language, or omit it when using the defaults.
// Machine names: use your configured name_format and name_list_format IDs.
$name_formatter = \Drupal::service('name.formatter');
$formatted = $name_formatter->format($components, 'default');
$list = $name_formatter->formatList($items, 'default', 'default', NULL);
// Gender-aware random sample names (limit and optional field definition).
$name_generator = \Drupal::service('name.generator');
$name_generator->generateSampleNames(100, $field_definition);
Integration
Development ⚙
If you haven't already, install Docker and DDEV
git clone [email protected]:project/name.git
cd name
ddev config --project-type=drupal --docroot=web --php-version=8.4 --corepack-enable --project-name=name
ddev add-on get ddev/ddev-drupal-contrib
ddev start
ddev poser
ddev symlink-project
ddev drush site:install -y
ddev drush pm:install name -y
ddev drush config:export -y
ddev drush user:login