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.
- Install and enable the Patternkit, Patternkit Example, Patternkit Devel, and Devel modules
- Log in as an administrative user
- Navigate to
/devel/pattern-test - Clear all caches (
drush cr) or delete all compiled Twig files withinsites/default/files/php/twig - Select "@patternkit/atoms/example/src/example (Example)" from the "Pattern to render" selection field
- Enter the following configuration in the JSON configuration text field:
{ "text": "text value", "formatted_text": "<strong>My formatted text</strong>" } - Click the "Execute" button
- Expect the page to reload and render the pattern successfully
- Observe the presence of 3 "*___string_template__*" directories in the
sites/default/files/php/twigfolder - Change the value for "text" in the configuration value
- "Click the "Execute" button
- 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 issueSolution 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
| Comment | File | Size | Author |
|---|---|---|---|
| #6 | patternkit--3327185-6--excessive-twig-cache-files.patch | 7.1 KB | slucero |
Issue fork patternkit-3327185
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
Comment #2
sluceroI'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:
Comment #4
sluceroNow that tests are passing I'm uploading a patch of the changes for easy reference and inclusion in composer files.
Comment #6
sluceroAmending patch with changes to fix deprecation warnings causing test failures.
Comment #8
sluceroComment #9
bfodeke commentedReviewed and tested. I saw some signaficant reduction in twig cache files especially on nodes with complex patterns.
Comment #10
bfodeke commentedComment #12
sluceroMerged for inclusion in the beta 7 release.