I have a plugin that works perfectly with ckeditor v3. Problem is when there is no Ckeditor on the screen on admin panel I get following message in the console.
Uncaught ReferenceError: CKEDITOR is not defined - plugin.js?navohq:7

That's what I have on line 7
CKEDITOR.plugins.add('pluginName', {

How do I get rid of that error ?

Comments

TwoD’s picture

Well, you could wrap it all in if (window.CKEDITOR) { ...plugin code... }, but you really shouldn't need to do that since your plugin file shouldn't be loaded unless an editor has been loaded first. At least that would be the case if you're implementing hook_wysiwyg_plugin() to present the plugin to Wysiwyg module.

Are you using custom code to load this plugin?

atomicreach’s picture

I am not sure what would be custom code but this is the method I am using in the module.

function atomic_engager_wysiwyg_plugin($editor, $version = 0) {
    switch ($editor) {
        case 'tinymce':
            if (version_compare($version, 3, '>=') && version_compare($version, 4, '<')) {
                return array(
                    'customDictionaryContextMenu' => array(
                        // A URL to the plugin's homepage, optional.
                        'url' => 'http://www.atomicreach.com/',
                        // The full path to the native editor plugin, no trailing slash.
                        // Ignored when 'internal' is set to TRUE below.
                        //'path' => wysiwyg_get_path('tinymce_plugins', TRUE) . '/myplugin',
                        'path' => drupal_get_path('module', 'atomic_engager') . '/inc/customDictionaryContextMenu/editor_plugin.js',
                        // A list of editor extensions provided by this native plugin.
                        // Extensions are not displayed as buttons and touch the editor's
                        // internals, so you should know what you are doing.
                        'extensions' => array(
                            'internal_name_of_myplugin' => t('Atomic Engager Custom Dictionary'),
                        ),
                        // Boolean whether the editor needs to load this plugin. When TRUE,
                        // the editor will automatically load the plugin based on the 'path'
                        // variable provided. If FALSE, the plugin either does not need to
                        // be loaded or is already loaded by something else on the page.
                        // Most plugins should define TRUE here.
                        'load' => TRUE,
                        // Boolean whether this plugin is a native plugin, i.e. shipped with
                        // the editor. Definition must be ommitted for plugins provided by
                        // other modules. TRUE means 'path' and 'filename' above are ignored
                        // and the plugin is instead loaded from the editor's plugin folder.
                        'internal' => FALSE,
                    ),
                );
            }
            break;
        // Below is add to custom dictionary plugi for ckeditor v3.
        case 'ckeditor':
            if (version_compare($version, 3, '>=') && version_compare($version, 4, '<')) {
                return array(
                    'atomicengager' => array(
                        'path' => drupal_get_path('module', 'atomic_engager') . '/inc/ckeditor3/plugin/atomicengager',
                        'buttons' => array(
                            'atomicengager_button' => t('Atomic Engager'),
                        ),
                        'load' => TRUE,
                    ),
                );
            }
            break;
    }
}

Here are the first few lines of the plugin.js file.

(function($) {
    var target;
    if (typeof(CKEDITOR) !== 'undefined') {
    CKEDITOR.plugins.add('atomicengager', {

if(typeof(CKEDITOR)!== 'undefined') seems to fix the issue. However, I am not sure if this is the right way to fix this.

Thanks

TwoD’s picture

That looks good, but the question is why the plugin file would be included when there is no editor available.
Is the module using drupal_add_js() anywhere to include the file manually?

Could you give an example of where this error appears?

atomicreach’s picture

Module is not including that file anywhere but from what I shared above. An example would be every admin page when there is no ckeditor on screen. i.e find content page or settings page.

TwoD’s picture

Is Drupal.settings.wysiwyg set when there are no editors?

atomicreach’s picture

Sorry, I don't understand? Can you provide more info on it.

TwoD’s picture

Open the browser's developer tools (usually Ctrl+Shift+I or J), switch to the console and enter Drupal.settings.wysiwyg to see if the Wysiwyg settings object was created. You could also check the Net[work] or Scripts sections to see if wysiwyg.js and/or wysiwyg.init.js were loaded.

atomicreach’s picture

okay thanks for the explanation. Now if I refer back to your question

"Is Drupal.settings.wysiwyg set when there are no editors?"

so the answer is NO. When there is no editor, it returns 'undefined'.

TwoD’s picture

Ok, that suggests Wysiwyg is not the one including the plugin file, or it would likely be including its other scripts and the settings too.
Tracking exactly where the file gets included from is a bit difficult, but it should be in that module, since it's the only one, except for Wysiwyg, which "knows" the file exists.

Is JavaScript optimization/aggregation enabled on the performance page? Maybe it was included in an aggregated file by mistake...

Is the plugin file's script tag listed in the document header or in the body? (If it is listed at all and not included as part of aggregated scripts, always disable aggregation first when debugging things like this.) Wysiwyg includes all plugins in the header.