Logo du Blog de Kromack (Samuel Sanchez)

Ressources et tutoriaux : Web 2.0 – PHP – CodeIgniter – Webdesign

CKEditor Helper for CodeIgniter

CKEditor Helper for CodeIgniter

2 jan 2010

Using CKEditor as a plugin into your CodeIgniter applications

CKEditor is a powerfull WYSIWYG text editor licensed under the GPL, LGPL and MPL open source licenses. CKEditor can easilly be added to any web page, you will find below a simple way to integrate CKeditor to your CodeIgniter applications.

Downloading CKEditor

The first step is to download the CKEditor editor package, note that the helper have only be tested over CKEditor 3.0.2. Once done, you should consider to remove the _samples and _sources directories from the uncompressed files.

Then, place the entire ckeditor directory into a /js/ folder. You can place it anywhere but remember to set the correct path when initializing the helper.

Adding the CKEditor helper for CodeIgniter

Download and place the ckeditor_helper.php file into the CodeIgniter’s system/application/helpers folder.

This helper can manage all CKEditor’s available configuration options, custom styles definitions, multiple intances of the editor on the same page and extra config parameters such as toolbar definition.

Creating the controller

First of all, we are going to create a controller that will set all the helper’s configuration options. In this exemple, we are going to instanciate two CKEditors with different configuration values. You are able to set all CKEditor’s available configuration options inside the config array. We are also going to define custom styles to replace the CKEditor’s default styles. Note that the id must match the textarea’s id in the view. Since the 2010-08-28 version, each styles definitions are applied to the associated CKEditor instance, allowing you to use different option set by instance. Since this version, you can also add severals CKEditor instance on the same page and define custom toolbars (thanks to the ronan’s patch ;) ).

<?php
 
class Ckeditor extends Controller {
 
	public $data 	= 	array();
 
	public function __construct() {
 
		parent::Controller();
 
		$this->load->helper('url'); //You should autoload this one ;)
		$this->load->helper('ckeditor');
 
 
		//Ckeditor's configuration
		$this->data['ckeditor'] = array(
 
			//ID of the textarea that will be replaced
			'id' 	=> 	'content',
			'path'	=>	'js/ckeditor',
 
			//Optionnal values
			'config' => array(
				'toolbar' 	=> 	"Full", 	//Using the Full toolbar
				'width' 	=> 	"550px",	//Setting a custom width
				'height' 	=> 	'100px',	//Setting a custom height
 
			),
 
			//Replacing styles from the "Styles tool"
			'styles' => array(
 
				//Creating a new style named "style 1"
				'style 1' => array (
					'name' 		=> 	'Blue Title',
					'element' 	=> 	'h2',
					'styles' => array(
						'color' 	=> 	'Blue',
						'font-weight' 	=> 	'bold'
					)
				),
 
				//Creating a new style named "style 2"
				'style 2' => array (
					'name' 	=> 	'Red Title',
					'element' 	=> 	'h2',
					'styles' => array(
						'color' 		=> 	'Red',
						'font-weight' 		=> 	'bold',
						'text-decoration'	=> 	'underline'
					)
				)				
			)
		);
 
		$this->data['ckeditor_2'] = array(
 
			//ID of the textarea that will be replaced
			'id' 	=> 	'content_2',
			'path'	=>	'js/ckeditor',
 
			//Optionnal values
			'config' => array(
				'width' 	=> 	"550px",	//Setting a custom width
				'height' 	=> 	'100px',	//Setting a custom height
				'toolbar' 	=> 	array(	//Setting a custom toolbar
					array('Bold', 'Italic'),
					array('Underline', 'Strike', 'FontSize'),
					array('Smiley'),
					'/'
				)
			),
 
			//Replacing styles from the "Styles tool"
			'styles' => array(
 
				//Creating a new style named "style 1"
				'style 3' => array (
					'name' 		=> 	'Green Title',
					'element' 	=> 	'h3',
					'styles' => array(
						'color' 	=> 	'Green',
						'font-weight' 	=> 	'bold'
					)
				)
 
			)
		);		
 
 
	}
 
	public function index() {
 
		$this->load->view('ckeditor', $this->data);
 
	}
}

Creating the view

The ckeditor.php view only has to display two textarea elements with matched ids and call the display_ckeditor() helper’s function.

<textarea name="content" id="content" ><p>Example data</p></textarea>
<?php echo display_ckeditor($ckeditor); ?>
<textarea name="content_2" id="content_2" ><p>Example data</p></textarea>
<?php echo display_ckeditor($ckeditor_2); ?>
</textarea>

That’s all ! If you’ve followed all the steps correctly, two CKEditors should shows up in the view. Please note that I assume that you are loading also a correct header and footer view with all the xHTML required stuff.

Downloading the tutorial

Source files of this tutorial (controller, helper, and view) can be downloaded here.

Changelog

  • 2010-08-28: Ronan’s patch for custom toolbar definition added (thanks dude!).
  • 2010-08-28: Configuration options are now only applied on the associated CKEditor.
  • 2010-08-28: Several editors can now be displayed on the same page.
  • 2010-01-12: All the stuff moved out of system/plugins.
  • 2010-01-30: Fixed Internet Explorer compatibility issue.

Troubleshooting

  • If you are using the .htaccess file given by the CodeIgniter’s user guide and have placed the ckeditor’s folder into system/plugins, be sure to allow the directory system to be called via HTTP in order to allow access to the plugins directory

Licence

Creative Commons License
CKEditor plugin for CodeIgniter by Samuel Sanchez is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.
Based on a work at codeigniter.com.
Permissions beyond the scope of this license may be available at http://www.kromack.com/contact-kromack-developpeur-france/.

Please note that this helper is based on an original idea discussed in this CodeIgniter thread.

40 comments

  1. Karel /

    I have tried your tutorial, but it doesnt work. Should the textarea id same on both controller and view? You have mentioned in controller « contenu » and in the view « content » ?? can you please help me? thanks

  2. It was an error, both id have to match, sorry about that !

  3. Kabeza /

    How can I do to avoid writing the same code in each controller function?
    I mean, one thing would be to autoload the ckeditor helper…
    but I’d like to know how to avoid creating the $this->data['ckeditor'] array each time I want to send ckeditor to a view…

    thanks! @kabeza

  4. Hi, you should use a config file for the helper in order to build the config array. Then, put $this->data['ckeditor'] = $this->config->item(‘ckeditor_config’); in a pre_controller, or in a hook, or in your constructor ;)

    See http://codeigniter.com/user_guide/libraries/config.html for more informations. Good luck !

  5. The controller example have been updated to put all the initialization stuff into the constructor. The helper have also been updated to work outside system/plugins. Use the ‘path’ key to locate your own ckeditor folder.

  6. What does this path means in helper?
    CKEDITOR_BASEPATH = ‘ » . base_url() . « system/plugins/ckeditor/’;

Leave a Reply

Optimized by SEO Ultimate