Add WYSIWYG ckeditor to Zend_Textarea element of Zend_Form

Adding a WYSIWYG editor to Zend_Textarea element of zend framework . I don't know whether this is the right way to this . Anyway I would love to share the piece of code to you . I am using the ckeditor the opensource WYSIWYG editor which was previously called fckeditor. I love the new look of it than the tiny mce . So switched to it .

Extract the files of ckeditor and upload to public folder . I am keeping this in the /js/ckeditor .

Add the below piece of code to the layout . As we dont need to load the js of the ckeditor every time we can add a flag.

<?php 
    if ( $this->ckeditor ) { 
        echo $this->headScript()->appendFile( $this->baseUrl().'/js/ckeditor/ckeditor.js');
     }
?>

Now you need a helper . Keep the below code in /application/views/helpers/SetupEditor.php .

<?php
class Zend_View_Helper_SetupEditor {

    function setupEditor( $textareaId ) {
        return "<script type=\"text/javascript\">
    CKEDITOR.replace( '". $textareaId ."' );
        </script>";
    }
}
?>

I have used a baseUrl helper also . So if you are new I am keeping it for those .

<?php
class Zend_View_Helper_BaseUrl {
    function baseUrl() {
        $fc = Zend_Controller_Front::getInstance();
        return $fc->getBaseUrl();
    }
}
?>

Now you can include the small piece of code in the view script where you are printing the form . For this eg: I am using the application/views/scripts/index/edit.phtml

<?php
    $this->ckeditor = 'ckeditor'; //To tell the layout ckeditor must be loaded . 
?>
<h2>Edit Post</h2>
<div><?php echo $this->postForm; ?></div>
<!-- Description is the id of the textarea -->
<?php echo $this->setupEditor( 'Description' ); ?>

You must know the name of the id of the textarea to pass to the setupEditor . I have used the name Description when creating the textarea element . You must have atleast tried the Zend framework quickstart to understand how it works . I hope I have not missed any .

Tags: 

Comments

I've added something

c_girl's picture

I like the simplicity of this solution, but I didn't like having to manually add the js in the headscript.
I added:

public function setView(Zend_View_Interface $view){

$this->view = $view;

}

and then included the js in the setupEditor method

echo $this->view->headScript()->appendFile( $this->baseUrl().'/js/ckeditor/ckeditor.js');

Now there is only a single line of code in my views, and nothing in my layout.Very nice.

Again, thankyou.

thanks

ron's picture

hi

first, this is great. can you write about how to use the image upload options? and how to add or remove tool bars?

best regards

ron

Solution to multiple elements

Jan Keller Catalan's picture

I did it like this:

A form element like the Textarea element in the framework:

class MyLibrary_Form_Element_Wysiwyg extends Zend_Form_Element_Xhtml
{
/**
* Use formWysiwyg view helper by default
* @var string
*/
public $helper = 'formWysiwyg';
}

and a view helper, subclassing the Zend_View_Helper_FormTextarea:

class Zend_View_Helper_FormWysiwyg extends Zend_View_Helper_FormTextarea
{
/**
* How many instances of the wysiwyg editor is set in the form
*
* @var int
*/
static $instances = 0;

/**
* Generates a 'wysiwyg' textarea element.
*
* @access public
*
* @param string|array $name If a string, the element name. If an
* array, all other parameters are ignored, and the array elements
* are extracted in place of added parameters.
*
* @param mixed $value The element value.
*
* @param array $attribs Attributes for the element tag.
*
* @return string The element XHTML.
*/
public function formWysiwyg($name, $value = null, $attribs = null)
{
$info = $this->_getInfo($name, $value, $attribs);
extract($info); // name, value, attribs, options, listsep, disable

$xhtml = $this->formTextarea($name, $value, $attribs);
$xhtml .= '
CKEDITOR.replace( "'. $this->view->escape($id) .'" );
';
$this->instances += 1;
if ($this->instances == 1) {
$this->view->headScript()->appendFile( $this->view->baseUrl().'/js/ckeditor/ckeditor.js');
}
return $xhtml;
}
}

A little mistake & request.

Anonymous's picture

in the last part
$this->ckeditor = 'ckeditor'; //To tell the layout ckeditor must be loaded

should be

$this->layout()->ckeditor = ckeditor;

and in layout.phtml

$this->layout()->ckeditor

shd be used.

how will you make this work with zend_form can post an tutorial for that too??

Thanks in Adance.

Parabens

Jonathas Zeferino's picture

ótima dica realmente me surpreendeu a praticidade de integrar o ckeditor com zend framework Parabéns!

I changed some, and only what

matlas's picture

I changed some, and only what we need it's only create view helper (and download ckeditor):


class Zend_View_Helper_SetupEditor extends Zend_View_Helper_Abstract{

function setupEditor($textareaId) {
$ctrl = Zend_Controller_Front::getInstance();
$this->view->headScript()->appendFile( $ctrl->getBaseUrl().'/js/ckeditor/ckeditor.js');

return '
CKEDITOR.replace( "'. $textareaId .'" );
';
}
}

Yes but i made more changes,

matlas's picture

Yes but i made more changes, good helper (and load once js library) is:


<?php
class Zend_View_Helper_SetupEditor extends Zend_View_Helper_Abstract{

function setupEditor($textareaIdArray) {
$ctrl = Zend_Controller_Front::getInstance();
$this->view->headScript()->appendFile( $ctrl->getBaseUrl().'/js/ckeditor/ckeditor.js');

$script = '';
foreach($textareaIdArray as $textareaId)
$script .= 'CKEDITOR.replace("' . $textareaId . '");';

return $script . '';
}
}

so in view, you can use:

<?= $this->setupEditor(array('name1','name2')); ?>

but i have one problem, how add in zend framework to CKeditor, CKFinder http://ckfinder.com/ ?

better implementation

openbsdiste's picture

I read carefully this thread.
Really interesting !

In fact, a better implementation should contain 3 elements :
- a form element
- a view helper
- a decorator

adding javascript isn't a job for the helper ! A decorator can do it fine and using options on the decorator makes you able to give parameters to CKEDITOR.replace !

Why bother this much?

Erdal YAZICIOGLU's picture

This is a great solution but there is a better way to do it. I might be wrong please correct me if I am wrong. This CKEditor editor is mostly needed by the administration side of the project. So instead of runing it in the view, we can run it directly inside the admin layout. This works very good for me...

Let create a text area with Zend Form

class Application_Form_BugReportForm extends Zend_Form {

public function init(){

$description = $this->createElement('textarea', 'description');
$description->setLabel('Issue description:');
$description->setRequired(true);
$this->addElement($description);
}
}

And then call it in admin layout.

window.onload = function()
{
CKEDITOR.replace( 'description' );
};


Thats it.

Cheers

I am newbie here and like you

Jabangtutuka's picture

I am newbie here and like you post. I have try your method but when i try to edit existing post, my form replace the content and I have to type it again. Please advice me how to solve my problem

class

Adri Korpershoek's picture

class Ckeditor_Form_Decorator_Ckeditor extends Zend_Form_Decorator_Abstract implements Zend_Form_Decorator_Interface
{
/**
* @var Ckeditor_Ckeditor
*/
protected $_editor;
/**
* @param Zend_Form_Element
* @return Ckeditor_FOrm_Decorator_Ckeditor
*/
public function setElement( $element )
{
if( !$element instanceof Zend_Form_Element_Textarea )
{
throw new Zend_Form_Decorator_Exception( 'Invalid type passed into decorator must be of type Zend_Form_Element_Textarea' );
}
$this->_element = $element;
return $this;
}

/**
* @return Ckeditor_Ckeditor
*/
public function getEditor()
{
if( !$this->_editor instanceof Ckeditor_Ckeditor )
{
$editor = new Ckeditor_Ckeditor();
$this->setEditor( $editor );
$this->_editor->basePath= $this->getOption('basePath');
}
return $this->_editor;
}

/**
* @param Ckeditor_Ckeditor
* @return Ckeditor_Form_Decorator_Ckeditor
*/
public function setEditor( Ckeditor_Ckeditor $editor)
{
$this->_editor = $editor;
return $this;
}

public function render( $content )
{

$this->getEditor()->returnOutput = true;
$js = $this->getEditor()->replace($this->getElement()->getName(), $this->getOption('config'), $this->getOption('events') );
return $content. $js;

}

}

I have CKEditor implemented using Zend_Form_Decorators

And using the CKEditor class with comes with CKEditor him self But I renamed it to Ckeditor_Ckeditor.

Hi, I am using your code to

Cristina's picture

Hi,

I am using your code to integrate CKEditor in a web site.
Many thanks. It works fine.
But now i have tried a few modifications to it in order to integrate CKFinder. Unfortunately nothing happens, that is, I don't get the Browse Server button at all.
Did you get round to making CKFinder work within your code?
Be most grateful if you could help.
Regards,

Save in database

Anonymous's picture

The implementation of the editor is working well, but now I want to save the coded text into my database. But I guess I've got to much security on zend to add all the html tags as well. Cause it's saving just the text, is there a solution to save the code into a database? Or are there a few things a certainly may not add in the form or in the controller when i get the value of the textarea?

Code

Marimuthu's picture

Hi hari

Thanks for quick reply

Can you please share some snippets about this validation for fckeditor( :) ) inputs?

Thanks

Add new comment