processwire-issues icon indicating copy to clipboard operation
processwire-issues copied to clipboard

InputfieldTinyMCE settings not loaded within AJAX loaded RepeaterPage

Open romaincazier opened this issue 1 year ago • 2 comments

Short description of the issue

Because of the way inputfields’ js config are added and only rendered when the admin theme itself is rendered, whenever an inputfield with an associated js config is loaded afterwards (e.g. InputfieldTinyMCE in an ajax-loaded RepeaterPage) its config is not added and thus can result in breaking behaviors.

Expected behavior

If an inputfield is rendered within an ajax call, its js config should be inlined.

Suggestion for a possible fix

An issue has already been raised but for CKEditor, and maybe the changes mentioned could be applied to TinyMCE, but from a quick test it doesn’t seem to solve the issue. One thing I did is to add this hook after InputfieldTinyMCE’s __render() call:

$wire->addHookAfter("InputfieldTinyMCE::render", function(HookEvent $event) {
	if($event->config->ajax) {
		$return = $event->return;
		/** @var InputfieldTinyMCE $inputfield */
		$inputfield = $event->object;
		$config = $event->config;
		$configName = $inputfield->getConfigName();
		$script = "script";
		$return .= "<$script>";
		$return .= "if(!(\"$configName\" in ProcessWire.config.{$inputfield->className()}.settings))";
		$return .= " ProcessWire.config.{$inputfield->className()}.settings.{$configName} = ";
		$return .= json_encode($config->js($inputfield->className())["settings"][$configName]);
		$return .= ";</$script>";
		$event->return = $return;
	}
});

Maybe this could be worth exploring / expanding for several other use cases (like the way it does now with Repeaters’ style)

romaincazier avatar Nov 12 '24 17:11 romaincazier

@romaincazier I can't seem to duplicate that. TinyMCE in repeater items are loading their configuration correctly in my testing here. Most Inputfields (including TinyMCE) load their configuration, scripts and any other prerequisites through their renderReady() method, which is called as part of the main (non-ajax) page rendering request. This is part of the reason for the renderReady method existing on Inputfields. Is there something different in your installation?

ryancramerdesign avatar Nov 15 '24 18:11 ryancramerdesign

@ryancramerdesign my bad, it’s actually not working when TinyMCE is part of an InputfieldFile’s custom field which is loaded in a repeater item. Then the renderReady() is indeed called but the page has already been rendered so the script can’t be output

romaincazier avatar Nov 15 '24 19:11 romaincazier