Skip to content

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

Description

@alinnert

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions