Sonata – add CKEditor in admin textareas

CKEditor is one of the best wysiwyg. It is possible to use CKEditor with sonataFormatterBundle, but there is an easier way to add CKEditor in Sonata admin textareas.
Add the file:
Acme/AdminBundle/Resources/views/CRUD/edit.html.twig

{% extends 'SonataAdminBundle:CRUD:base_edit.html.twig' %}

{% block javascripts %}
{{ parent() }}
    <script src="//cdn.ckeditor.com/4.5.0/standard/ckeditor.js"></script>
    <script type="text/javascript">
        $(function() {
            $('textarea.ckeditor').ckeditor();
        });
    </script>
{% endblock %}

It supposed that jquery is already loaded. If it’s not, add it in the block javascripts

in /app/config/config.yml, override the sonata edit file

sonata_admin:
    // ...
    templates:
        edit:                AcmeAdminBundle:CRUD:edit.html.twig

Then in your admin file, in the configureFormFields function, use:

$formMapper->add('description', 'textarea', array('attr' => array('class' => 'ckeditor')));

Here, description is the entity field.

Sources : http://stackoverflow.com/questions/20269617/how-to-add-ckeditor-to-sonata-admin-backends-textareas

Raphaël has written 45 articles

One thought on “Sonata – add CKEditor in admin textareas

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>