This is an image field formatter, which inherits settings from the build-in default image field formatter in the Drupal core, acts the same as the built-in one, but if the image does not exist, display an avatar letter generated instead. The letter depends on the account's display name.

In general, it's used to the built-in User Picture field of the user entity. But it's not limited to be used in other image fields in any entity types.

For which letter(s)/char(s) to generate the avatar, If the target entity of the image field is the user entity, then this module calls the getDisplayName() method of the user entity to get the string to display. Otherwise, the label() method is used.

Features

  • Formatter settings inline with the config of the library https://github.com/laravolt/avatar
  • Support non-English
  • Respect the setting of Link to conent which inherits from the ImageFormatter
  • Support inline SVG

Dependencies

https://github.com/laravolt/avatar, install it by composer

composer require laravolt/avatar

Recommendation

If the target entity is the user entity, it's possible to implements hook_user_format_name_alter() to determine which letter(s)/char(s) to display.

Sample pseudo-code:

/**
 * Implements hook_user_format_name_alter().
 */
function MODULE_NAME_user_user_format_name_alter(&$name, AccountInterface $account) {
  $name = MODULE_NAME_get_language_pecific_name($account);
}

Or copy the code of this field formatter as an example to implement your own

Sponsorship

This module is sponsored by:

  • InterGreat.com InterGreat develops products for international students. Visit the website to find out more.
Supporting organizations: 

Project information

Releases