Writing Java Plugins

This website contains links to software which is either no longer maintained or will be supported only until the end of 2019 (CKFinder 2). For the latest documentation about current CKSource projects, including software like CKEditor 4/CKEditor 5, CKFinder 3, Cloud Services, Letters, Accessibility Checker, please visit the new documentation website.

If you look for an information about very old versions of CKEditor, FCKeditor and CKFinder check also the CKEditor forum, which was closed in 2015. If not, please head to StackOverflow for support.

CKFinder functionality can be extended with server-side plugins. Although the full source code of the CKFinder server connector is available and can be modified in any way desired, a much better way of enhancing the CKFinder connector is to create a plugin.

The main advantages of plugins are:

  • Upgrades are much easier.
  • The plugin code is stored in a single place.
  • Plugins can be easily disabled when they are not needed anymore.

Common use cases:

  • Adding a new server-side command (i.e. fileditor and imageresize plugin).
  • Working with uploaded files (i.e. watermark plugin).
  • Extending information returned by the Init command (i.e. imageresize plugin).

Creating CKFinder for Java Plugins

As mentioned in the Using CKFinder Plugins article, the functionality of CKFinder can be extended with code that is delegated to dedicated plugins. This architecture ensures that you do not need to modify CKFinder core to change the behavior of the application — using plugins is much more convenient.

This tutorial shows how to create a CKFinder plugin on the example of a FileSizePlugin which, as the name suggests, returns the size of the file.

In order to create a CKFinder plugin, you will need to follow the steps below:

  • Step 1: Create the plugin folder and file.
  • Step 2: Add the plugin definition.
  • Step 3: Make CKFinder aware of the new plugin.

Please note that for plugins that just add server-side functionality and do not contain any client-side scripts you can skip the first step.

Creating Plugin Folder and File

For a start, you need to create a directory for your plugin inside the /CKFinderJava-X.Y/ckfinder/plugins directory. Please note that by default CKFinder comes with three client-side plugins: dummy, fileeditor, and imagresize.

important note
Remember that for CKFinder the name of the plugin folder is important and has to be the same as the name of the plugin, otherwise the application will not be able to recognize it.


Since our plugin is named filesizeplugin, the same name will be used for its folder. Inside the newly created filesizeplugin folder we are going to place the plugin.js file that will contain the plugin logic. Open the plugin.js file in an editor and add the following source code:

CKFinder.addPlugin( 'filesizeplugin', function( api ) {		// The name of the plugin.
	api.addFileContextMenuOption( { label : 'File Size', command : "FileSize" } , function( api, file )
	{
		api.connector.sendCommand( 'FileSize', { fileName : api.getSelectedFile().name }, function( xml )
		{
			if ( xml.checkError() )
				return; 
			var size = xml.selectSingleNode( 'Connector/FileSize/@size' );
			api.openMsgDialog( "", "The exact size of a file is: " + size.value + " bytes");
		} );
	});
});

Since the purpose of this tutorial is to show how to integrate plugins with CKFinder, we are not going to go into details of the JavaScript code. To sum it up, the plugin code above adds a new menu item to the user interface. This item, when selected, sends the FileSize command to the server, reads the XML response and displays the size of the queried file in a dialog window.

For more information on how to create JavaScript plugins refer to the Writing JavaScript Plugins article.

Making CKFinder Aware of the Plugin

Fast forward one step, this section describes how to make CKFinder aware that the plugin exists. Open the config.xml file and add a new plugin node to the plugins section.

<plugin>
	<name>filesizeplugin</name>
	<class>com.ckfinder.connector.plugins.FileSizePlugin</class>
	<params></params>
</plugin>

A plugin entry contains the following elements:

  • <name> – the name of the plugin that should be the same as the plugin folder name on the client side.
  • <class> – the plugin class extended from com.ckfinder.connector.configuration.Plugin.
  • <params> – additional plugin parameters. A parameter is added with a param node:
    <param name="smallThumb" value="90x90" />

Java Plugin System

Events

CKFinder provides several events that can be used to extend the functionality of a CKFinder application. Assigning a class (also known as an event handler) to an event will cause that function to be called at an appropriate point in the main CKFinder code to perform whatever additional task(s) the developer considers useful at that point. Each event can have multiple event listeners assigned to it.

Available Events

Hook Since Description
AfterFileUpload 2.0 Executed after successful file upload.
BeforeExecuteCommand 2.0 Executed before a server side command is executed.
InitCommand 2.0 Executed straight before sending the result of the Init command.