0

As I want to use two different Redactor plugins, table.js and properties.js, I decided to write a simple Craft Plugin to load the JS using includeJsResouce in the init() function, as so;

function init() {

    if ( craft()->request->isCpRequest() && craft()->userSession->isLoggedIn() )
    {
        craft()->templates->includeJsResource('myextensions/js/properties.js');
        craft()->templates->includeJsResource('myextensions/js/table.js');
    }

}

However, I have found that these JS files are being loaded in the HTML before the Redactor.js file, so I get a JS error in the console, as Redactor is undefined. [Uncaught TypeError: Cannot read property 'prototype' of undefined]

Is there a way to ensure that my plugin JS is loaded after Redactor that does not entail hacking the core of Craft?

The version of Craft I am using is the current, Craft Pro 2.5.2753.

1

You need to alter your properties.js and table.js files. Instead of assigning the plugin function to $.Redactor.prototype you should assign it to the RedactorPlugins object.

Standard Redactor Plugin setup:

$.Redactor.prototype.myplugin = function() {
    return {
    // Plugin code here...
    }
}

Craft CMS Redactor Plugin setup:

// Initialize the RedactorPlugins object if it doesn't exist
if ( !RedactorPlugins ) { var RedactorPlugins = {}; }

// Assign the redactor plugin function
RedactorPlugins.myplugin = function() {
    return {
    // Plugin code here...
    }
}

I'm not sure where the documentation on this is, but you can see it in Pixel & Tonic's own RedactorClips plugin here: https://github.com/pixelandtonic/RedactorClips/blob/master/redactorclips/RedactorClipsPlugin.php

  • I have tried your solution and it no longer throws the exception, thanks, but the plugins are still not being loaded. I modified standard.json to { "buttons": ["format","bold","italic","lists","link","file","horizontalrule","table","properties"], "plugins":["source","fullscreen", "table","properties"] } I checked the RedactorPlugins object and my two redactor plugins are there. What could I be missing? [Note: I am simply trying to implement the imperavi.com/redactor/plugins/table and imperavi.com/redactor/plugins/properties plugins, but within Craft 2.5.2753.] – graney Dec 9 '15 at 14:09
  • Ok, I was being stupid. I had picked an entry type that had the rich text field set to use the default and not the standard config, and that was why it was not showing up. Everything works fine now, thanks a lot on the pointers. – graney Dec 9 '15 at 14:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.