Pattern Rendering Creates Excessive Twig String Template Cache Files

Created on 14 December 2022, over 1 year ago
Updated 16 January 2023, over 1 year ago

Problem/Motivation

When rendering a pattern, Patternkit uses a pattern's Twig template content to render the markup and this requires compiling the Twig files producing cache files in the site's file cache directory (typically located at sites/default/files/php/twig/). The way template content is loaded and cached in the database rather than loading directly from the file produces every pattern's primary template output as a string template compilation file rather than directly mapped to the pattern's twig file. This implementation for rendering is used for every pattern using the Twig plugin and may be seen here:

      $template = $pattern->getTemplate();
      if (empty($template)) {
        return [];
      }
      $pattern->config ??= [];
      $output = [
        '#type' => 'inline_template',
        '#template' => $template,
        '#context' => $pattern->config,
      ];

Additionally, the current implementation of Token processing is using a PatternFieldProcessor plugin implementation to recursively iterate through all string properties and replace token values into the string. Unfortunately, this implementation attempts to support Twig filters on token values in the string and in doing so renders every string value as a compiled Twig cache file for every field value it encounters. This means a new string template Twig cache file is created for every unique string value in any field of any pattern instance.

<!--break-->

Steps to reproduce

Confirmation of this behavior requires default Twig caching behaviors to be enabled and access to review file creation within the site's files directory.

  1. Install and enable the Patternkit, Patternkit Example, Patternkit Devel, and Devel modules
  2. Log in as an administrative user
  3. Navigate to /devel/pattern-test
  4. Clear all caches (drush cr) or delete all compiled Twig files within sites/default/files/php/twig
  5. Select "@patternkit/atoms/example/src/example (Example)" from the "Pattern to render" selection field
  6. Enter the following configuration in the JSON configuration text field:
    {
      "text": "text value",
      "formatted_text": "<strong>My formatted text</strong>"
      }
  7. Click the "Execute" button
  8. Expect the page to reload and render the pattern successfully
  9. Observe the presence of 3 "*___string_template__*" directories in the sites/default/files/php/twig folder
  10. Change the value for "text" in the configuration value
  11. "Click the "Execute" button
  12. Observe the presence of a 4th string template directory

Proposed resolution

Since the support for Twig filters on token values in patterns is undocumented and likely sees very limited use, I propose removing support for this functionality and simplifying the token processor to only replace token values into the string without the twig rendering process. This will remove the inline template rendering for dynamic string values (a violation of best practice), and should resolve the creation of new twig cache files for every new string value encountered.

Remaining tasks

  • Code review and testing
  • Change record creation

User interface changes

None

API changes

  • Token values in Pattern string values may no longer use twig filters

Data model changes

None

πŸ› Bug report
Status

Fixed

Version

9.1

Component

Module Core

Created by

πŸ‡ΊπŸ‡ΈUnited States slucero Arkansas

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.69.0 2024