alexdin/yii2-ckeditor-widget

CKEditor widget for Yii2.

Installs: 45

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 106

Type:yii2-extension

2.0.6 2016-11-30 09:51 UTC

This package is auto-updated.

Last update: 2024-03-12 22:29:56 UTC


README

Latest Version Software License Build Status Coverage Status Quality Score Total Downloads

Renders a CKEditor WYSIWYG text editor plugin widget.

Installation

The preferred way to install this extension is through composer.

Either run

composer require alexdin/yii2-ckeditor-widget

or add

"alexdin/yii2-ckeditor-widget" : "2.0"

to the require section of your application's composer.json file.

Skins & Plugins

This widget works with default's dev-full/stable branch of CKEditor, with a set of plugins and skins. If you wish to configure a different skins or plugins that the one proposed, you will have to download them separately and configure the widget's clientOptions attribute accordingly.

Usage

The library comes with two widgets: CKEditor and CKEditorInline. One is for classic edition and the other for inline editing respectively.

Using a model with a basic preset:


use alexdin\ckeditor\CKEditor;


<?= $form->field($model, 'text')->widget(CKEditor::className(), [
        'options' => ['rows' => 6],
        'preset' => 'basic'
    ]) ?>

Using inline edition with basic preset:


use alexdin\ckeditor\CKEditorInline;

<?php CKEditorInline::begin(['preset' => 'basic']);?>
    This text can be edited now :)
<?php CKEditorInline::end();?>

How to add custom plugins

This is the way to add custom plugins to the editor. Since version 2.0 we are working with the packagist version of the CKEditor library, therefore we are required to use its configuration API in order to add external plugins.

Lets add the popular Code Editor Plugin for example. This plugin would allow us to add a button to our editor's toolbar so we can add code to the content we are editing.

Assuming you have downloaded the plugin and added to the root directory of your Yii2 site. I have it this way:

+ frontend 
+ -- web 
    + -- pbckcode 

We can now add it to our CKEditor widget. For this example I am using CKEditorInline widget. One thing you notice on this example is that we do not use the preset attribute; this is highly important as we want to add a customized toolbar to our widget. No more talking, here is the code:

<?php
 
use alexdin\ckeditor\CKEditorInline;

// First we need to tell CKEDITOR variable where is our external plufin 
$this->registerJs("CKEDITOR.plugins.addExternal('pbckcode', '/pbckcode/plugin.js', '');");

// ... 
// Using the plugin
<?php CKEditorInline::begin(['preset' => 'custom', 'clientOptions' => [
    'extraPlugins' => 'pbckcode',
    'toolbarGroups' => [
        ['name' => 'undo'],
        ['name' => 'basicstyles', 'groups' => ['basicstyles', 'cleanup']],
        ['name' => 'colors'],
        ['name' => 'links', 'groups' => ['links', 'insert']],
        ['name' => 'others', 'groups' => ['others', 'about']],
        
        ['name' => 'pbckcode'] // <--- OUR NEW PLUGIN YAY!
    ]
]]) ?>

<p>
    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et
    dolore magna aliqua. 
</p>
<?php CKEditorInline::end() ?>

About extra assets

You maybe wonder why there is file alexdin-ckeditor.widget.js. The reason is that due to the way Yii2 works with forms and Cross-Site Request Forgery (csrf). CKEditor does not trigger the on change event nor collects the CSRF token when using file uploads.

The asset tackles both issues.

Testing

To test the extension, is better to clone this repository on your computer. After, go to the extensions folder and do the following (assuming you have composer installed on your computer):

$ composer install --no-interaction --prefer-source --dev

Once all required libraries are installed then do:

$ vendor/bin/phpunit

Further Information

Please, check the CKEditor plugin site documentation for further information about its configuration options.

Contributing

Please see CONTRIBUTING for details.

Credits

License

The BSD License (BSD). Please see License File for more information.

alexdin!
Web development has never been so fun!
www.alexdin.us