Obfuscate field formatter

Provides several email obfuscation methods that can be used through Field Formatter, text Filter, service container and Twig function.

Context

The initial goal of this module was to provide basic email obfuscation for the core Email field during the Drupal 8 development of the SpamSpan filter module.
The current development now focuses on providing several obfuscation methods (html entities, ROT13 with a CSS reversed text fallback, ...). It will rely on external vendors when available.

The following posts are giving a good overview of the subject :

Installation

Use Composer.

composer require drupal/obfuscate:^1.0@alpha

Configuration

System wide

The system wide configuration is used by the text filter, service container and the Twig extension.

The obfuscation method can be defined via Configuration > Content authoring > Obfuscate (/admin/config/obfuscate).

Text format

Activate the obfuscation for the desired text formats ('Full HTML', ...).Configuration > Content authoring > Text formats and editors, then check Email address obfuscation filter.

Per field

The field configuration inherits from the system wide configuration as a default value and can be overridden per field instance.

View modes

All the view modes (default/full, teaser, search index, ...) that exposes publicly email addresses should define the Field Formatter to Obfuscate.

For some reasons, it should be preferred to leave the Field Formatter of several view modes to Plain text or Email. So, the decision of obfuscating is left to the discretion of the site builder.

Service

Obfuscate can also be used from code via a service.

// Favor dependency injection to get the service
$obfuscateMail = \Drupal::service('obfuscate_mail');
$mail = 'terry.jones@spam.com';
$build = [
  '#markup' => $obfuscateMail->getObfuscatedLink($mail),
];

Service methods

getObfuscatedLink

Returns an obfuscated email link.

// Optional link parameters (html attributes) can be defined.
// If not overriden, provides the default rel="nofollow".
$params = ['class' => 'button'];
$obfuscateMail->getObfuscatedLink($mail, $params);

obfuscateEmail

Returns an obfuscated email link, it is used by the getObfuscatedLink method.

Twig filter

Returns an obfuscated email link. Use case: while using a custom template for email fields or printing User entity email address.

{{ 'terry.jones@spam.com'|obfuscateMail }}

Roadmap

  • Add other methods of obfuscating email addresses.
  • Improve the text Filter.
  • Twig helper {{ mail|obfuscateMail }}.
  • System wide obfuscation method configuration that applies to the Field Formatter, Filter and Twig extension.

Credits to Alexei Tenitski for the obfuscation solution on the first release.

Related modules

SpamSpan filter

Supporting organizations: 
Initial development

Project information

Releases