Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Hey, I just wanted to port this CKEditor code...But I am having trouble with the examples to form off of...It just outputs a piece of text (token) to the body of the editor and I'd like to be able to use it using the WYSIWYG JS API so I can hook into it from another module. Here is the CKEditor code:
CKEDITOR.config.editorial_tokens = [
{
name:'insertcontent',
icon:'images/content-icon.png',
html:'[tokenname]',
title:'A token'
}
];
CKEDITOR.plugins.add( 'editorial_tokens',
{
init : function( editor )
{
var buttonsConfig = editor.config.editorial_tokens;
console.log(buttonsConfig);
if (!buttonsConfig)
return;
function createCommand( definition )
{
return {
exec: function( editor ) {
editor.insertHtml( definition.html );
}
};
}
// Create the command for each button
for(var i=0; i<buttonsConfig.length; i++)
{
var button = buttonsConfig[ i ];
console.log('/sites/all/modules/custom/settings/editorial/ckeditor/plugins/editorial_tokens/' + button.icon);
var commandName = button.name;
editor.addCommand( commandName, createCommand(button, editor) );
editor.ui.addButton( commandName,
{
label : button.title,
command : commandName,
icon : '/sites/all/modules/custom/settings/editorial/ckeditor/plugins/editorial_tokens/' + button.icon
});
}
} //Init
} );
Please help me address.
Comments
Comment #2
TwoDComment #3
TwoDUPDATE: This code is now tested and verified.
You need 3 files.
pga_editorial/pga_editorial.info:
pga_editorial/pga_editorial.php:
pga_editorial/ckeditor/plugins/editorial_tokens/plugin.js:
Enable the Drupal module and the buttons will show up in the Wysiwyg profile GUI.
Comment #4
rayvan CreditAttribution: rayvan commentedThat didn't work unfortunately...The JS asset is loading, and I verified the text format profiles checkboxes are enabled for Insert Editorial Token, and I made sure the icon path is a valid path.
Please let me know if you have other suggestions.
Comment #5
TwoDAre you getting JavaScript errors in the browser's debug tools?
Is
console.log(buttonsConfig);
not printing the correct values, if any at all?Does the button show up in the editor?
I just noticed I missed one property in the
hook_wysiwyg_plugin()
implementation:'init' => TRUE
.I'll edit my previous post and add it. It tells the editor to actually include the plugin in its list of plugins to load...
Comment #6
rayvan CreditAttribution: rayvan commentedThanks for the immediate help TwoD, here is the finalized code that worked with your help:
In .module
In plugin.js:
Comment #7
rayvan CreditAttribution: rayvan commentedTwoD resolved this issue.
This works as designed but I feel there could be more information inside of wysiwyg.api.js or README to the effect of how TwoD resolved this, in this thread. Perhaps just throw the working code sample in there...
Comment #8
rayvan CreditAttribution: rayvan commentedOh, apparently I also needed this in my hook_init for the module (I initially thought it was just loaded by AJAX, but its not):
Comment #9
TwoDThe relevant documentation is in wysiwyg.api.php. Since you have a native CKEditor plugin, you only need
hook_wysiwyg_plugin()
and not the information in wysiwyg.api.js. The other plugin hooks and wysiwyg.api.js are for when you want to write a cross-editor plugin using just Wysiwyg module's API.The
hook_init()
implementation should not be needed. It would load the plugin on every page, vs just on the edit forms it's needed.The
'load' => TRUE
,'internal' => FALSE
'path' => ....
and'init' => TRUE
directives inhook_wysiwyg_plugin()
should tell the editor to load the plugin file from a non-interrnal (not relative to the editor library folder) from the specified path and initialize the plugin on its own.Initializing and loading, in the case of CKEditor would equate to WYSIWYG adding the plugin name to the
config.plugins
list. If it's an internal plugin either bundled with the CKEditor library or afterwards installed into its own plugins folder, CKEditor itself knows where to find the plugin file. If'internal'
isFALSE
, WYSIWYG sends along the full path to the plugin file and registers that path withCKEDITOR.plugins.addExternal()
before creating any editor instances.Comment #10
TwoDI've updated my code in #3 and tested it.
This version removes the need for duplicating the button definitions on both the PHP and JS side of things as the definitions in PHP are sent down to the CKEditor configuration anyway.
It also keeps the paths correct if the module is installed elsewhere and there's no need to add the plugin script using
hook_init()
.The path to the plugin should be relative to the Drupal root, so no
base_path()
there, but the paths to the icons needed to be absolute or CKEditor will interpret them as relative to its own root.