Broken Twig function

Created on 12 February 2025, 6 months ago

Problem/Motivation

The module declares the obfuscate Twig function.
This function appears to be broken because it returns a Twig filter.

Steps to reproduce

Add this to a Twig template:

{{ obfuscate('test@example.com') }}

It fails with this error:

Twig\ExtensionSet::getFunction(): Return value must be of type ?Twig\TwigFunction, Twig\TwigFilter returned

Proposed resolution

TwigExtension::getFunctions() should probably return an array of TwigFunction objects.

Remaining tasks

User interface changes

API changes

Data model changes

πŸ› Bug report
Status

Active

Version

2.0

Component

Code

Created by

πŸ‡«πŸ‡·France prudloff Lille

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @prudloff
  • Merge request !6Fix Twig function β†’ (Open) created by prudloff
  • πŸ‡«πŸ‡·France prudloff Lille
  • πŸ‡«πŸ‡·France PhilY πŸ‡ͺπŸ‡ΊπŸ‡«πŸ‡· Paris, France

    You should use {{ 'test@example.com'|obfuscateMail }}

  • Status changed to Needs review 3 months ago
  • πŸ‡«πŸ‡·France prudloff Lille

    Yes, the obfuscateMail filter works. But the module also provides an Twig function and it is broken.
    We should either remove the function or make it work.

  • πŸ‡¨πŸ‡­Switzerland weri

    In general, there seem to be a few implementations with the wrong order of the defined function parameters of the interface ObfuscateMailInterface::getObfuscatedLink($email, array $params = [], $text = '')

      /**
       * Returns an obfuscated link from an email address.
       *
       * @param string $email
       *   Email address.
       * @param array $params
       *   Optional parameters to be used by the a tag.
       * @param string $text
       *   Optional text for the a tag innerHtml.
       *
       * @return array
       *   Obfuscated email link render array.
       */
      public function getObfuscatedLink($email, array $params = [], $text = '');
    

    https://www.drupal.org/project/obfuscate/issues/3370295 πŸ› Link Label in Field Formatter is not working Active

  • First commit to issue fork.
  • Merge request !9Resolve #3506101 "Fix twig function" β†’ (Open) created by yannickoo
  • πŸ‡©πŸ‡ͺGermany yannickoo Berlin

    Hey together πŸ‘‹ I have also noticed that the Twig function did not work as expected.

    My goal was to create an obfuscated email link for info@example.com with an extra class attribute for styling. Unfortunately the MR that has been created could not help me since there is no support for attributes even if the module itself could handle that :(

    After checking the code I figured out that we got a small mess here regarding the order of the arguments: Sometimes it was $email, $params, $text and somewhere else $email, $text, $params.

    The MR I have created fixes

    • The broken Twig function
    • Wrong order for arguments
    • Attribute support for Twig function

    Example usages

    Minimal example

    {{ obfuscate('info@example.com') }}
    

    ➑️ <a href="mailto:info@example.com">info@example.com</a>

    With link text

    {{ obfuscate('info@example.com', 'Link text') }}
    

    ➑️ <a href="mailto:info@example.com">Link text</a>

    With link attributes

    {{ obfuscate('info@example.com', 'Link text', {
      class: 'font-bold'
    }) }}
    

    ➑️ <a href="mailto:info@example.com" class="font-bold">Link text</a>

  • πŸ‡¦πŸ‡ΊAustralia nigelcunningham Geelong

    @yannickoo, would you use the function if it was fixed? I was thinking of removing it.

  • πŸ‡«πŸ‡·France PhilY πŸ‡ͺπŸ‡ΊπŸ‡«πŸ‡· Paris, France

    I would use at least use the 'with link text' example and will for sure find some usage for the 'with link attributes' one πŸ˜‰

  • πŸ‡¦πŸ‡ΊAustralia nigelcunningham Geelong

    Thanks for the reply!

  • πŸ‡©πŸ‡ͺGermany yannickoo Berlin

    It is important for me to add classes for styling purposes directly on the link, therefore the flexible function is very useful πŸŽ‰

  • πŸ‡¦πŸ‡ΊAustralia nigelcunningham Geelong

    Thanks for the reply; I'll keep it then :)

Production build 0.71.5 2024