Great module. Thanks!
When I build an editor configuration, I export it to a feature. I'd love to include the templates in the feature, but I don't always know the path at which the feature will be installed. The configuration for the template path provided by this module requires me to know that.
It would be cool if we could use some kind of token syntax to allow us to more dynamically reference module paths.
{my_magic_feature}/templates.js
to...
/modules/custom/my_magic_feature/templates.js
This code, added to CkeditorTemplates.php, seems to do the trick.
// Set template files default value if set.
if (isset($settings['plugins']['templates']['template_path']) && !empty($settings['plugins']['templates']['template_path'])) {
// Allow token replacement for module paths.
$path = preg_replace_callback(
'/{([^}]+)}/',
function ($matches) {
if (\Drupal::moduleHandler()->moduleExists($matches[1])) {
return base_path() . drupal_get_path('module', $matches[1]);
}
else {
return $matches[0];
}
},
$settings['plugins']['templates']['template_path']
);
$config['templates_files'] = [ $path ];
}
Comments
Comment #2
jasonlttl commentedHere's a patch with the code above.
Comment #3
jasonlttl commentedAs an aside, another hurdle one can encounter is that the ckeditor plugin expects a "imagePaths" javascript variable to point at the folder where the preview images live. Since it's in the javascript template.js file, it's hard to get the module's install path (known to php). There are probably multiple ways around that, but here's an example that adds a route to serve the images. This is probably not the most efficient approach.
my_module/my_module.routing.yml
my_module/src/Controllers/MyModuleImageController.php
Comment #4
jasonlttl commentedIt occurs to me that I can just use the same approach for template.js that I'm using for imagePaths and that this is probably a better solution than adding diy tokenizing support to the config field.
I'm going to go ahead and close my ticket and relabel support.
Comment #5
jasonlttl commentedComment #6
lucaslg commentedHi Jason,
Thanks for your feedback.
Your solution of using custom routes definitely works but it feels a little bit overkill to have a controller only to return image and javascript files.
However, I have never had the need to export this configuration to a feature yet and I admit I have some trouble understanding you exact use case.
To me, the templates are closely related to your theme and the possibility of reuse without any modification by another website seems pretty low. That's why we implemented a kind of auto-discovery of the template file in the current theme and didn't do any equivalent for the modules.
But this might be wrong and there might be something missing. What kind of templates do you include in your feature and how do you use it afterward ? Does it includes some basic structures with simple html and some classes and you then personalise with css only on each website ?
Your first solution was fine. Maybe if we need it, I would look more into using Drupal token system https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Utility%21Token.php/class/Token/8.2.x to avoid, like you said, to use a DIY method.
Comment #7
jasonlttl commentedWe're a large university. The basic gist is that we expect to have multiple sites and themes within our office (marketing) and we'd also like to make our editing package (along with other components of our distribution) available to other units on campus (colleges, business units) many of which have their own teams, themes, and distributions. We also just purchased acquia lift/content hub, so the more resyndicatable our content is across different themes, the better.
With all these factors, and themes being the hardest thing to agree on, packaging templates in a module just seems a lot more natural.
Adding a controller is pretty heavy, but it's not hard. That's fine for me. I wouldn't worry about this.
Thanks for the followup and thanks again for the great module.