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.

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

  • Automated tests to reproduce the issue
  • Solution implementation
  • 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

Issue fork patternkit-3327185

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

slucero created an issue. See original summary.

slucero’s picture

Assigned: slucero » Unassigned
Issue summary: View changes
Status: Active » Needs review
Parent issue: » #3327256: Beta 7 Release Plan

I've uploaded an MR to address this issue by simplifying the token replacement process in the token field processor plugin. This significantly simplifies the code involved, and includes related updates to the tests to work as expected.

Next steps:

  • Code review
  • Testing

slucero’s picture

Now that tests are passing I'm uploading a patch of the changes for easy reference and inclusion in composer files.

{
  ...
  "extra": {
    "patches": {
      "drupal/patternkit": {
        "#3327185: Pattern Rendering Creates Excessive Twig String Template Cache Files": "https://www.drupal.org/files/issues/2023-01-10/patternkit--3327185-4--excessive-twig-cache-files.patch"
      }
    }
  }
}

Status: Needs review » Needs work
slucero’s picture

Status: Needs work » Needs review
StatusFileSize
new7.1 KB

Amending patch with changes to fix deprecation warnings causing test failures.

{
  ...
  "extra": {
    "patches": {
      "drupal/patternkit": {
        "#3327185: Pattern Rendering Creates Excessive Twig String Template Cache Files": "https://www.drupal.org/files/issues/2023-01-11/patternkit--3327185-6--excessive-twig-cache-files.patch"
      }
    }
  }
}

slucero’s picture

bfodeke’s picture

Reviewed and tested. I saw some signaficant reduction in twig cache files especially on nodes with complex patterns.

bfodeke’s picture

Status: Needs review » Reviewed & tested by the community

  • slucero committed 36d0977a on 9.1.x
    Issue #3327185 by slucero, bfodeke, mariohernandez: Pattern Rendering...
slucero’s picture

Status: Reviewed & tested by the community » Fixed

Merged for inclusion in the beta 7 release.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.