Search code examples
javascriptckeditor4.xsymfony5

Symfony 5 & FOSCKEDITOR : create a simple custom plugin, icon of this plugin does not appears in the Ckeditor toolbar


From a Symfony 5 website, I installed the useful bundle fosckeditor (CKEDITOR version 4).

All works fine, I get the CKEDITOR field on my page. Now I want to create a new simple plugin.

I scrupulously followed this official guide and created a new plugin in the <symfony_root_dir>/public/bundle/fosckeditor/plugins/ named 'timestamp' with some files:

enter image description here

In the plugin.js, I add this code:

CKEDITOR.plugins.add( 'timestamp', {
    icons: 'timestamp',
    init: function( editor ) {
        alert('hello test ?'); // this alert appears when I load the page containing the CKEDITOR
        editor.addCommand('insertTimestamp', {
            exec: function (editor) {
                var now = new Date();
                editor.insertHtml('The current date and time is: <em>' + now.toString() + '</em>');
            }
        });
        editor.ui.addButton('timestamp', {
            label: 'Insert Timestamp',
            command: 'insertTimestamp',
            toolbar: 'insert'
        })
    }
});

And, in <symfony_root_dir>/public/bundle/fosckeditor/config.js, I added:

CKEDITOR.editorConfig = function( config ) {
    config.extraPlugins = ['timestamp'];
    // same result if instead I add the custom plugin via a string : config.extraPlugins = 'timestamp';
};

For this simple example, I copy/paste an icon from another plugin, here is the timestamp icon file:

enter image description here

Finally, I reload my page (reload + clear caches). But the Ckeditor toolbar does not change, the custom plugin appears nowhere.

enter image description here

I tried to add the button in the fos_ckeditor.yaml file like this:

# ...
fos_ck_editor:
    # ...
    default_config: main_config
    configs:
        main_config:
            # ...
            toolbar:
                - {
                    items:
                      ['timestamp']
                }
    styles:
        # ...

But the button of my custom plugin keeps missing in the CKEditor toolbar. I have no javascript error in the browser console, I don't understand where I made the mistake.


Solution

  • Try this configuration:

    app/templates/ckeditor.html.twig

    {% extends "base.html.twig" %}
    
    {% block body %}
    
        <div class="ckeditor">
    
            {{ form_start(form) }}
                {{ form_row( form.content ) }}
            {{ form_end(form) }}
    
        </div>
    
    {% endblock %}
    

    app/src/Controller/TestController.php

    <?php
    
    namespace App\Controller;
    
    use Symfony\Component\Form\Extension\Core\Type\TextareaType;
    use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
    use FOS\CKEditorBundle\Form\Type\CKEditorType;
    
    class TestController extends AbstractController
    {
        public function index()
        {
            $form = $this->createFormBuilder()
                        ->add('content', CKEditorType::class)
                        ->getForm();
    
            return $this->render('ckeditor.html.twig', [
                'form' => $form->createView()
            ]);
        }
    }
    

    app/public/bundles/fosckeditor/plugins/timestamp/plugin.js

    CKEDITOR.plugins.add( 'timestamp', {
        init: function(editor) {
            editor.addCommand('insertTimestamp', {
                exec: function (editor) {
                    var now = new Date();
                    editor.insertHtml('The current date and time is: <em>' + now.toString() + '</em>');
                }
            });
    
            editor.ui.addButton('timestamp', {
                label: 'Insert Timestamp',
                command: 'insertTimestamp',
                toolbar: 'mode,0',                 // toolbar group and index
                icon: this.path + 'timestamp.png'  // icon file (PNG)
            });
        }
    })
    

    app/config/packages/fos_ckeditor.yaml

    twig:
        form_themes:
            - '@FOSCKEditor/Form/ckeditor_widget.html.twig'
    
    fos_ck_editor:
        default_config: test_config
    
        configs:
            test_config:
                extraPlugins: ["timestamp"]
    
        plugins:
            timestamp:
                path:     "bundles/fosckeditor/plugins/timestamp/"
                filename: "plugin.js"
    

    Screenshot:

    CKEditor Plugin Screeshot

    Plugin Directory Structure:

    app/public/bundles/fosckeditor/plugins/

    timestamp/
    ├── plugin.js
    └── timestamp.png
    

    Relevant links: