craftcms / craftcms/cms

[5.x]: CP tries to render Twig templates in CKEditor when deeply nesting entries and crashes when using custom Twig functions in templates

Open
#19,639 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
PHP
Stars
3.6k
Forks
705
Avg merge
1d 5h
Merged PRs (30d)
134

Description

What happened?
Description

It's best to explain that via a screenshot:

Image

I was only able to take this screenshot because I disabled to render the "Text" field inside the green "Collapsible Content" card. If I enable that the CP crashes when I try to edit that entry.

What I think happens is:

Craft tries to render the "Collapsible Content" card on the left. If I enable to output the content of "Text" inside the "Collapsible Content" entry type it loops over all elements of "Text". First it's just the paragraph "Level 2 before". Then comes a nested entry. It tries to render that nested entry using its Twig template. That works fine until I use a custom Twig function inside that template. In that case custom Twig functions aren't available and the rendering fails.

The error message literally says:

Twig Syntax Error

Unknown "html_classes" function in "_partials/entry/collapsibleContent" at line 23.

The website itself renders just fine. This happens with functions from twig/html-extra as well from modules containing custom functions.

Steps to reproduce

The steps marked with ❗ directly contribute to the bug appearing.

Setup
  1. Fresh Craft CMS instance
  2. Install twig/html-extra using Composer
  3. Install CKEditor plugin
Config
  1. Create a field "Text" with field type "CKEditor"
  2. Create an entry type "Collapsible Content"
    • Handle: "collapsibleContent"
    • Add the "Text" field
    • Enable "Text" under "Card Attributes" ❗
  3. Create an entry type "Content"
    • Add the "Text" field
  4. Edit the "Text" field again
    • Add the "+" button (Add nested content) to the toolbar
    • Add "Collapsible Content" under "Entry Types"
  5. Create a section "Content"
    • Section type: "Channel"
    • Entry Types: "Content"
  6. Create template _partials/entry/collapsibleContent.twig
    • Use function html_classes() inside template ❗
Create content
  1. Create a new "Content" entry
  2. Add "Collapsible Content" in "Text"
  3. Add another "Collapsible Content" inside "Text" of the first "Collapsible Content" ❗
    • Actual text content is not required for the error to show up
  4. Save all changes and create entry
  5. Open entry again in CP
  6. → Twig Syntax Error should appear
Expected behavior

Preview card renders in some way. And I think Twig shouldn't be used to render those preview cards because it doesn't get used on the first level. Only when a second level is involved.

Actual behavior

CP crashes when trying to edit an entry setup like above because it cannot find any custom Twig functions. "Twig Syntax Error" in dev mode or "Internal Server Error" in production mode.

Craft CMS version

5.11.1

PHP version

8.5.8 and 8.5.9

Operating system and version

MacOS 26, 27 and Ubuntu 24.04

Database type and version

MariaDB 10.11.14 and MySQL 8.0.46

Image driver and version

Imagick 3.8.1 (ImageMagick 7.1.1-43 / 6.9.12-98)

Installed plugins and versions
  • CKEditor 5.7.0
  • twig/html-extra 3.28

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the crash using the nested CKEditor setup and _partials/entry/collapsibleContent.twig with html_classes(). Start by tracing how the CP renders nested entry preview cards and which Twig environment they use. Done means reopening the nested entry no longer crashes and custom Twig functions are handled in the preview rendering.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
backend, content
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.