开发者

Changing the height of ckeditor from a zend form

开发者 https://www.devze.com 2023-04-03 08:27 出处:网络
I am trying to set the height of a ckeditor I am using. Here is what I currently have: $this->addElement(\'textarea\', \'t开发者_如何学编程ext_field\', array(

I am trying to set the height of a ckeditor I am using. Here is what I currently have:

      $this->addElement('textarea', 't开发者_如何学编程ext_field', array(
        'filters'    => array('StringTrim'),
        'validators' => array(
            array('StringLength', true, array(0, 3000)),
        ),
        'decorators' => array('ViewHelper'),
        'required'   => false,
        'attribs'    => array('class' => 'ckeditor'),
        'label'      => 'Please enter text below',
        'value'      => isset($this->_text_data[0]['text']) ? $this->_text_data[0]['text'] : ''
    ));

This comes from my form, this is then called in my .phtml file by the following:

<?=$this->element->getElement('text_field')?>

I have looked everywhere and tried adding:

'height' => '100px',

and:

'config' => array(
          'toolbar' =>  'Full',     
          'width'   =>  '550px',    
          'height'  =>  '100px',
 ),

But neither of these have worked. The main reason I need this is I have a text area (using the ckeditor in order to allow the input information to be formatted in a particular way) which is quite long (the default height I am assuming) but it is only ever a few lines input into the box, hence the reason I want it smaller as it takes up too much space on the page.

Thanks in advance

Iain


I made a form element and a helper using ZendX_JQuery_View_Helper_UiWidget to create a CKEditor with the jQuery adapter. Here's the code of both files :

ZendExt_Form_Element_CKEditor :

class ZendExt_Form_Element_CKEditor extends ZendX_JQuery_Form_Element_UiWidget
{
    /**
     * Use formCKeditor view helper by default
     * @var string
     */
    public $helper = 'formCKEditor';

    /**
     * Default ckeditor options
     *
     * @var array
     */
    public $jQueryParams = array(
        'toolbar' => 'Basic'
    );
}

And ZendExt_View_Helper_FormCKEditor :

class ZendExt_View_Helper_FormCKEditor extends ZendX_JQuery_View_Helper_UiWidget
{
    static $set = false;

    public function formCKEditor($name, $value = null, $params = null, $attribs = null)
    {
        $hTextA = new Zend_View_Helper_FormTextarea();
        $hTextA -> setView($this -> view);
        $xhtml = $hTextA -> formTextarea($name, $value, $attribs);
        $xhtml .= '<script type="text/javascript">$(document).ready(function(){$("#' . $this->_normalizeId($name) . '").ckeditor(' . (!is_null($params) ? 'function(){},' . Zend_Json_Encoder::encode($params) : '') . ')});</script>';
        if (self::$set == false) {
            $this -> view -> headScript() -> appendFile($this -> view -> baseUrl() . '/js/ckeditor/ckeditor.js');
            $this -> view -> headScript() -> appendFile($this -> view -> baseUrl() . '/js/ckeditor/adapters/jquery.js');
            self::$set = true;
        }
        return $xhtml;
    }
}

You can use it as any other ZF form element once you copied these 2 files into :
* libraries/ZendExt/Form/Element/ for ZendExt_Form_Element_CKEditor class
* libraries/ZendExt/View/Helper/ for ZendExt_View_Helper_FormCKEditor class
and added the ZendExt namespace in your configuration file (or if you already have a library of yours and want to use it, just put both files in it and change the name of the classes to reflect yours). Then, you'll have tel ZF that ZendExt/View/Helper is a directory to look in for view helpers (in a .ini config file it would look like : resources.view.helperPath.ZendExt_View_Helper = "ZendExt/View/Helper").

Then in your code, just call $ckEditor = new ZendExt_Form_Element_CKEditor(); to create a new CKEditor. You may then add all params you want to the element using $ckEditor -> setJQueryParam($key, $value); as specified in the documentation here : http://framework.zend.com/manual/fr/zendx.jquery.html . For example : $ckEditor -> setJQueryParam('height', '100px');. I understand it's not a jQuery component, but it was the easiest way to be able to make it as everything needed is available there.

To display it, in your view just do <?=$this -> ckEditor?> and you're good.

Make sure you put your ckeditor.js and adapters/jquery.js in your public directory under /js/ckeditor/ or change the path accordingly in the helper.


You'll need to specify the dimensions of the editor when you create it (i.e., in the Javascript part). CKEditor replaces the original form element with its own code, so your changes in the dimensions will be lost.

For instance, if you create it using the jQuery interface, it would be something like the following:

var config = {
    width: '550px',
    height: '100px'
};

// Initialize the editor.
$('.jquery_ckeditor').ckeditor(config);

Hope that helps...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号