1
1
# Cacheability Metadata Checker (CMC)
2
2
3
- A Drupal module that helps developers identify missing cache tags in their pages by tracking loaded entities and verifying their cache tags are properly bubbled up to the response.
4
-
5
- ## Overview
6
-
7
- The Cacheability Metadata Checker monitors entity loads during page requests and verifies that all cache tags from those entities are present in the final response. This helps ensure proper cache invalidation by catching situations where entity cache tags are accidentally stripped or forgotten.
3
+ A Drupal module that helps developers identify missing cache tags in their pages by tracking loaded entities and
4
+ verifying their cache tags are properly bubbled up to the response.
8
5
9
6
## Features
10
7
11
- - Tracks loaded content entities and their cache tags
8
+ - Tracks loaded entities and their cache tags, compares with tags bubbled to the HTTP response object
12
9
- Configurable operation modes (disabled, display errors, strict)
13
10
- Option to check only front-end pages or include admin pages
14
11
- Ability to skip specific URLs
15
12
- API for other modules to exclude certain entities from tracking
13
+ - Custom PHPCS sniff for Twig template best practices
16
14
17
15
## Configuration
18
16
@@ -26,7 +24,8 @@ Visit `/admin/config/development/cmc` to configure the module. The following opt
26
24
27
25
### Skip Admin Pages
28
26
29
- When enabled (default), the module only checks pages using the default theme. When disabled, it also checks admin pages using the admin theme.
27
+ When enabled (default), the module only checks pages using the default theme. When disabled, it also checks admin
28
+ pages using the admin theme.
30
29
31
30
### Skip URLs
32
31
@@ -53,7 +52,107 @@ function mymodule_cmc_skip_tracking(EntityInterface $entity): bool {
53
52
54
53
## Best Practices
55
54
56
- 1 . Enable "Strict" mode during development to catch cache tag issues early
57
- 2 . Use "Display errors" mode on staging environments for visual feedback
58
- 3 . Disable the module in production environments
59
- 4 . Consider excluding admin pages if you're only concerned with front-end caching
55
+ 1 . Enable "Strict" mode during development to catch cache tag issues early.
56
+ 2 . Use "Display errors" mode on staging environments for visual feedback or to use tools such as VisualDiff tests.
57
+ 3 . Never enable the module in production environments.
58
+ 4 . Consider excluding admin pages to avoid unrelated issues when developing the front-end theme.
59
+
60
+ ## Coding Standards
61
+
62
+ The module includes a custom PHPCS sniff that helps maintain Drupal best practices in Twig templates:
63
+
64
+ ### Direct Field Access Sniff
65
+
66
+ This sniff detects direct field access in Twig templates using patterns like ` node.field_* ` , ` content.field_* ` , etc.
67
+ This pattern is discouraged because it may introduce caching bugs by leaving out the proper cache tags in the top-level
68
+ render array.
69
+
70
+ #### Installation
71
+
72
+ 1 . Install development dependencies:
73
+ ``` bash
74
+ composer require --dev drupal/coder squizlabs/php_codesniffer
75
+ ```
76
+
77
+ 2 . Register the custom standard:
78
+ ``` bash
79
+ vendor/bin/phpcs --config-set installed_paths /path/to/web/modules/custom/cmc/phpcs
80
+ ```
81
+ Note: the example above will set your ` installed_paths ` to include only the path to this module's sniffers, which
82
+ is likely not what you want. You can pass a comma-separated list of paths to the above command, or include the line
83
+ below in your ` phpcs.xml.dist ` configuration file:
84
+
85
+ ``` xml
86
+ <config name =" installed_paths" value =" ../../drupal/coder/coder_sniffer,../../sirbrillig/phpcs-variable-analysis,../../slevomat/coding-standard,../../../web/modules/custom/cmc/phpcs" />
87
+ ```
88
+
89
+ 3 . Add the standard to your ` phpcs.xml.dist ` :
90
+ ``` xml
91
+ <rule ref =" web/modules/custom/cmc/phpcs/ruleset.xml" />
92
+ ```
93
+
94
+ Also make sure you allow ` twig ` as one of the extensions to be sniffed, in case you have an ` <arg name="extensions"> `
95
+ config value.
96
+
97
+ 4 . Run the sniffer:
98
+ ``` bash
99
+ vendor/bin/phpcs /path/to/your/templates ` --standard=CMC`
100
+ ```
101
+
102
+ If you included the standard in your ` phpcs.xml.dist ` configuration file, you can omit the ` --standard=CMC ` flag.
103
+
104
+
105
+ #### Skip Conditions
106
+
107
+ The sniff will automatically skip the warning under these conditions:
108
+
109
+ 1 . If the template renders the full ` content ` variable:
110
+ ``` twig
111
+ {{ content }}
112
+ ```
113
+
114
+ 2 . If the template renders cache metadata explicitly:
115
+ ``` twig
116
+ {{ content['#cache'] }}
117
+ ```
118
+
119
+ 3 . If you explicitly opt-out using the special comment:
120
+ ``` twig
121
+ {# cmc_direct_field_access_sniff_opt_out #}
122
+ ```
123
+
124
+ #### Example Usage
125
+
126
+ ❌ Not recommended:
127
+ ``` twig
128
+ {# Direct field access will trigger warning #}
129
+ {{ node.field_image }}
130
+ {{ content.field_tags }}
131
+ {{ media.field_media_image }}
132
+ ```
133
+
134
+ ✅ Recommended:
135
+ ``` twig
136
+ {# Render the full content variable #}
137
+ {{ content }}
138
+
139
+ {# Render cache metadata & assets explicitly #}
140
+ {{ node.field_image }}
141
+ {{ {'#cache': content['#cache'], '#attached': content['#attached']} }}
142
+
143
+ {# Or opt-out if you know this is being handled elsewhere #}
144
+ {# cmc_direct_field_access_sniff_opt_out #}
145
+ {{ node.field_special_case }}
146
+ ```
147
+
148
+ Example warning:
149
+ ```
150
+ FILE: /path/to/template.html.twig
151
+ --------------------------------------------------------------------------------
152
+ FOUND 1 WARNING AFFECTING 1 LINE
153
+ --------------------------------------------------------------------------------
154
+ 15 | WARNING | Direct field access using "node.field_image" is not recommended.
155
+ | | Try to always render full render arrays that come from the backend.
156
+ --------------------------------------------------------------------------------
157
+ ```
158
+
0 commit comments