BLT Validation Fails on Twig Functions / Variables

One of my personal favorite BLT features is all of the automated code validation it does. I’ve talked extensively about code quality on the blog in the past. But! One of the most common problems with this particular feature comes about if you’re doing extensive work with Twig (and especially custom Twig functions or filters). Which, you know, you might be doing if you’re doing front end Drupal 8 or Drupal 9 development.

Let’s talk about how to fix this issue when it crops up.

What Is the Issue?

This issue will crop up while BLT is running its validate:twig command. It will look something like this…

> validate:twig
Validating twig syntax for all custom modules and themes...
Iterating over fileset files.twig...

  ERROR  in docroot/themes/custom/<your theme>/templates/<folder>/your-template.html.twig (line 2)
      1      {# Generated on Wed, 24 Feb 2021 07:18:55 GMT #}
  >> Unknown "processtoken" function. 
      3      <div class="coh-container swiper-slide" > {% set hideData %}{{ processtoken("[your_token]", {"paragraph": paragraph}, _context, true) }}{% endset %} {% if hideData is null or hideData|trim != '' %}
      4        <div class="coh-container" >

The relevant thing to note is the “Unknown "processtoken" function” line (which actually cropped up in the middle of a fairly long output in my terminal, I’ve trimmed the output here for ease of reading and to anonymize the material).

BLT is seeing this processtoken function, which is a valid function in the theme because it was defined elsewhere. But, the validation script is blowing up because it doesn’t know what it is (because it’s not part of drupal/core’s Twig definitions).

How do you Fix it?

This is super easy to fix. Open your blt/blt.yml file and add the following:

validate:
  twig:
    functions:

Then, under functions, just add a list of all the functions that you find. For me, after finding this processtoken issue I actually found another one as well (so it took me a couple of runs). You may also run into some issues with custom twig filters, in which case you can do this:

validate:
  twig:
    filters:

And then, just like with the functions, provide a list of the custom filters.

Each function / filter should be indented and on its own line with a dash in front of it (e.g. - view). Just follow YAML syntax.

See https://docs.acquia.com/blt/extending-blt/#modifying-tests for more information and other examples.

In Conclusion

Sometimes when you’re doing code quality scans you’ll get a failure and you scratch your head because the failure “isn’t really a failure.” This is a prime example! Usually when code quality tests fail, you should fix the code quality issue. You should not change the test to “work the way you want it to.” But every once in a while, your code will actually be “fine” and the test will be wrong and the right answer IS to update the test. Thankfully BLT provides a mechanism to do just this!

Related Content