Troubleshooting Contao 6 input encoding changes after upgrade
0 reputation · 04 Apr 2023, 23:46 UTC
0 reputation · 04 Apr 2023, 23:46 UTC
After upgrading to Contao 6, input encoding is disabled and double encoding is enabled by default, which can cause unexpected output if templates are not adjusted.
What specific steps should be taken to verify that output is properly encoded when using Twig templates or raw output from hooks, and how can the double‑encoding behavior be controlled during the upgrade process?
ini_set('default_charset', 'UTF-8'); or set default_charset = UTF-8 in php.ini and verify mbstring extension is enabled.config/databases.yml set charset: utf8mb4 and driver_options: { 1002: 'SET NAMES utf8mb4' } (PDO_MYSQL).utf8mb4_unicode_ci (run SHOW FULL COLUMNS FROM tl_content; etc.).php vendor/bin/contao-console cache:clear and php vendor/bin/contao-console cache:warmup.<meta charset="UTF-8"> is present; ensure no form contains an conflicting accept-charset attribute.Contao 6 requires PHP 7.4+, expects UTF‑8 end‑to‑end, and has removed the legacy ISO‑8859‑1 fallbacks that existed in Contao 4.x. The framework now normalises input via Symfony Request and StringUtil, rejecting or replacing invalid UTF‑8 sequences instead of silently passing them.
If the site previously used ISO‑8859‑1/latin1 or a mixed charset, the mismatch between PHP’s default_charset, the database connection/table collation, and the HTML meta charset can cause browsers to submit bytes that Contao reinterpreted, resulting in double‑encoding or mojibake after the upgrade.
To determine whether a simple configuration fix suffices or a full charset conversion is required, please confirm:
character_set_server and collation_server values before the upgrade.If the prior setup was latin1, a dump‑convert‑reimport of the database is needed; otherwise, the steps above should resolve the issue.
Use comments to ask for clarification. Post a solution as an answer.
34,775 reputation · 05 Apr 2023, 04:52 UTC
To specifically address the Twig templates and hooks mentioned, it is important to distinguish between application-level encoding and template-level escaping. Since Contao 6 leverages Twig, the autoescape feature is active by default, which can lead to the "double-encoding" appearance if data is already escaped before reaching the template.
|raw filter in Twig to prevent the engine from encoding it a second time.StringUtil::htmlspecialchars() before being assigned to the template variable.{% autoescape false % ... {% endautoescape %} if a large section of a template handles pre-encoded legacy content.When testing, use the browser's "View Page Source" rather than the Inspector; the Inspector shows the decoded DOM, whereas the source reveals if & (double-encoded) is actually present.