Drupal is a registered trademark of Dries Buytaert
drupal 11.3.7 Update released for Drupal core (11.3.7)! drupal 11.2.11 Update released for Drupal core (11.2.11)! drupal 10.6.7 Update released for Drupal core (10.6.7)! drupal 10.5.9 Update released for Drupal core (10.5.9)! cms 2.1.1 Update released for Drupal core (2.1.1)! drupal 11.3.6 Update released for Drupal core (11.3.6)! drupal 10.6.6 Update released for Drupal core (10.6.6)! cms 2.1.0 Update released for Drupal core (2.1.0)! bootstrap 8.x-3.40 Minor update available for theme bootstrap (8.x-3.40). menu_link_attributes 8.x-1.7 Minor update available for module menu_link_attributes (8.x-1.7). eca 3.1.1 Minor update available for module eca (3.1.1). layout_paragraphs 2.1.3 Minor update available for module layout_paragraphs (2.1.3). ai 1.3.3 Minor update available for module ai (1.3.3). ai 1.2.14 Minor update available for module ai (1.2.14). node_revision_delete 2.0.3 Minor update available for module node_revision_delete (2.0.3). moderated_content_bulk_publish 2.0.52 Minor update available for module moderated_content_bulk_publish (2.0.52). klaro 3.0.10 Minor update available for module klaro (3.0.10). klaro 3.0.9 Minor update available for module klaro (3.0.9). layout_paragraphs 2.1.2 Minor update available for module layout_paragraphs (2.1.2). geofield_map 11.1.8 Minor update available for module geofield_map (11.1.8).

obfuscate

333 sites Security covered
View on drupal.org

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 = '[email protected]';
$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.

{{ '[email protected]'|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

Activity

Total releases
1
First release
Apr 2025
Latest release
1 year ago
Release cadence
Stability
100% stable

Releases

Version Type Release date
2.0.1 Stable Apr 2, 2025