-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue with custom TwigFunction after CKEditor 4.1.0 #248
Comments
Hi, @thomascoppein, what’s the issue you’ve encountered? Can you please provide more info on this? |
Hi @i-just , when adding a new Entry to the CKEditor field I get an Twig Syntax Error that my function is unknown. |
Can you please share the stack trace for this? |
Yes, it appears when I save in the admin environment.
|
Ran into something similar. In my case the issue was that I was conditionally bootstrapping my custom Twig extension for site requests only, i.e. if (Craft::$app->getRequest()->getIsSiteRequest()) {
Craft::$app->getView()->registerTwigExtension(new CustomTwigExtension());
} Removing that condition (making sure that the custom Twig extension is registered for any and all requests) fixed the issue on my end. |
Thanks, @mmikkel - that’s a very good point. @thomascoppein - based on the stack trace, I can see that you’re using an Entry Type with an auto-generated title. To be clear, do you get this error when saving the nested entry? What’s the “Title Format” that entry? |
@i-just - yes, that's correct. The error persists even after updating the title to static text. The error now occurs differently; it is now present in the entry card. See screenshot below. The title of this entry type is ' Update: by changing the code as @mmikkel suggested, this error is resolved. However, a new error has appeared. I am using the nocache plugin, and now this behavior is 'unexpected'. |
@thomascoppein Based on your stack trace, this is happening for an entry type whose title format begins with |
@brandonkelly yes sure: |
Looks like basically the same thing; the Nocache plugin only registers its Twig extension for site requests: https://github.com/ttempleton/craft-nocache/blob/52e99ca49d1e6fcc12e4f92d0b8c24ca4ae5b641/src/Plugin.php#L41 I.e., something is causing your nested CKEditor entries to render their partial templates when you save the entry. Presumably you use the In my case it was search index jobs failing, which made sense to me as I assume Craft is rendering CKEditor content for fields that are indexable. Not sure what's going on in your case though, and I'm also pretty sure this behaviour wasn't occurring prior to CKEditor 4.1. |
@mmikkel Yes, that's correct. This issue did not exist prior to CKEditor 4.1. |
@i-just could you provide an update about this problem since it's breaking some logic which was working prior to version 4.1? if (Craft::$app->getRequest()->getIsSiteRequest()) {
Craft::$app->getView()->registerTwigExtension(new TwigExtension());
} |
Hi @thomascoppein,
It’s like @mmikkel said. If you wrap registration of the twig extension in the I hope this helps. Are you still having issues after mitigating the problem of the twig extension only being available for the front-end requests? |
Hi @i-just ,
Despite this, I’m still encountering the following error: ![]() My question is: why is it still throwing this error? Or perhaps the better question is: why does it still renders the twig code while in the editor? |
I haven't dug into it, but I assume that what's happening here is that Craft is rendering the nested entries when the owner entry is being search indexed (explains how this occurs on entry save). Or something similar to that. It's not clear to me why this only started happening w/ the CKEditor 4.1 update, but regardless: Considering that partial templates are generally assumed to render on the front end only (they live in the sites template folder, after all), would it make sense if Craft basically faked a site request whenever it has to render these templates under the hood (for example, when entries are saved or re-saved from the CP or CLI)? In @thomascoppein's case, NoCache is a publicly available plugin and I assume they have little control over how that plugin bootstraps its Twig extension. And more generally; implicitly prohibiting partial templates from containing any code that will fail for a non-site request, seems like something that's going to generate a lot of gotchas going forward, considering that partial templates is a front end concept to begin with? Most developers are probably not going to be aware that they need to make sure that their partial templates doesn't contain any code (first or third party) that will trigger an error for CP or CLI requests... |
@mmikkel - the search index issue was fixed in 4.2.0: “Fixed a bug where CKEditor fields’ search keywords were including nested entries’ rendered partial templates rather than nested entries’ search keywords.” @thomascoppein - I think I might be missing something here; I can’t reproduce the partials getting rendered when adding a nested entry to a CKEditor field (on 4.1.0 or 4.2.0 versions). Can you please let me know if this is correct:
Is the above correct or have I missed or got anything wrong? |
@i-just - CKEditor has been updated to version 4.2.0, but unfortunately, the issue persists.
After some investigation, I identified the root of the problem. I’m working with a Matrix field that contains an entry type called “Text,” which uses a CKEditor field. The Title Format for this entry type is dynamic, defined as However, when the Title Format is configured this way, the issue arises. The problem disappears when I switch to a default Title Format or a regular input field. While it’s not always necessary for users to manually enter a title, this workaround resolves the issue for now. |
Thank you for confirming and for the additional info on your setup - that was the missing piece of the puzzle! What happens here is - you’ve set the title of the “Text” entry type to be dynamic and to use the content of the CKEditor field. When using dynamic titles, your “pattern” is rendered to find out what the result should be (more info). Your pattern references a CKEditor field, which triggers the rendering of the content of that field. That means your This setup started to be problematic for you because CKEditor 4.1.0 introduced the concept of chunks. |
The reason this issue cropped up in 4.2.0 is because prior to that, when your CKEditor field was getting rendered for the Title Format, in was assumed that it was being rendered for the CP field input. So nested entries were getting rendered as cards, when they should have been rendered using the partial templates. 4.2.0 fixed that, which exposed ttempleton/craft-nocache#31. Closing this as technically everything is working as expected now (besides the No Cache issue). |
Description
Since the latest update to CKEditor 4.1.0, I have encountered an issue with a custom module that provides a Twig function for use in my longform components. This functionality was working correctly in the previous version of CKEditor.
Steps to reproduce
Additional info
The text was updated successfully, but these errors were encountered: