Sonata News Bundle - Add CKEditor - php

I'm a beginner in Symfony and I am trying to set up CKEditor for the Sonata News Bundle.
I have looked everywhere (even on German forums whereas I don't speak German !) but I cannot find any answer anywhere.
Does anyone have a clue or a solution to my question ?
Thank you everyone.
Louis

I would suggest you to use IvoryCKEditorBundle
Download the Bundle:
composer require egeloen/ckeditor-bundle
Register the Bundle, Then, update your app/AppKernel.php
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = array(
new Ivory\CKEditorBundle\IvoryCKEditorBundle(),
// ...
);
// ...
}
}
If you're using Symfony <= 2.8:
php app/console ckeditor:install
php app/console assets:install web
If you're using Symfony >= 3.0:
php bin/console ckeditor:install
php bin/console assets:install web
Then usage

The way I do it (symfony: 4.3.5, sonata: 3.54.1).
Download ckeditor JS lib and place it inside public/js so i.e. you should have the file:
your_app/public/js/ckeditor/ckeditor.js
and beside that file dirs: adapters, lang, plugins and skins and some js/css/md files.
Then override base sonata edit template with:
{% extends '#SonataAdmin/CRUD/base_edit.html.twig' %}
{% block javascripts %}
<script src="{{ asset('js/ckeditor/ckeditor.js') }}" type="text/javascript"></script>
{{ parent() }}
{% endblock %}
This code will include ckeditor to your pages and should be placed at: your_app/templates/edit.html.twig
Then, add to "templates" section edit:edit.html.twig so template you created would be used:
sonata_admin:
title: 'Your app Admin'
dashboard:
blocks:
- { type: sonata.admin.block.admin_list, position: left }
templates:
edit: edit.html.twig
This is file: your_app/config/packages/sonata_admin.yaml
Then, when you are adding new field from sonata admin bundle, inside configureFormFields your ckeditor fields should look like:
->add('field_name', null, array('attr'=> array('class' => 'ckeditor')))
Important part is adding array of attributes with class attribute.
Clear the cache and CKEditor should be working now.

Related

Disable console.log() messages generated by Sonata admin bundle

I'm using Sonata Admin bundle in my Symfony project. I see many console log messages in the browser generated by Sonata admin bundle.
These messages are generated by Sonata Admin from /vendor/sonata-project/admin-bundle/src/Resources/public/Admin.js by the code,
/**
* render log message
* #param mixed
*/
log: function() {
var msg = '[Sonata.Admin] ' + Array.prototype.join.call(arguments,', ');
if (window.console && window.console.log) {
window.console.log(msg);
} else if (window.opera && window.opera.postError) {
window.opera.postError(msg);
}
},
Does anyone know how to disable these log messages? I don't even get any kind of result from Google search. Is there a way to control these from configuration file or something?
Thanks in advance!!
I have opened a issue in Sonata Admin GitHub repo https://github.com/sonata-project/SonataAdminBundle/issues/5278 and found the solution.
You have to override the sonata admin's standard_layout like,
/config/packages/sonata_admin.yaml
sonata_admin
templates:
layout: 'sonata_admin/layout.html.twig'
Now create layout.html.twig inside /templates/sonata_admin/ and use the following code.
{% extends '#SonataAdmin/standard_layout.html.twig' %}
{% block javascripts %}
{{ parent() }}
<script>
if ('undefined' !== typeof window.Admin) {
window.Admin.log = function() {}
}
</script>
{% endblock %}
If you don't need any Sonata comments at all, I suggest you to comment that code and you should be done. If some error appears (for example, if the function "log()" is called anywhere in the code), you can just comment these two lines:
window.console.log(msg);
window.opera.postError(msg);

Symfony / Orocrm : yml file is not set?

Website : Open source frameword based on Symfony 2.7
My entitie : Entity/Download.php (with properties "id", "creation_date" and "name")
I'm working on my own Bundle and I try to create a new datagrid based on my own entitie, I follow this tutorial. Below is what I've done :
Create /resources/config/datagrid.yml, with a very light datagrid.
Edit DependencyInjection/MyBundleExtension.php to load datagrid.yml.
Edit my twig view to render the datagrid :
{% import 'PimDataGridBundle::macros.html.twig' as dataGrid %}
{% block content %}
{{ dataGrid.renderGrid('custom_download', { localeCode: locale_code() }) }}
{% endblock %}
But this error is generated :
Twig_Error_Runtime: "An exception has been thrown during the rendering
of a template ("A configuration for "custom_download" datagrid was not
found.
-> If I try the same code with another gridname already existing that works then I believe the problem comes from my new datagrid but I don't understand what's wrong : does the kernel include my YML config file ? I don't think so.
Thanks for your help.
You create datagrid.yml in /resources/config folder.
But according OroPlatform conventions configuration file should be placed in Resources/config/oro folder of your bundle and named datagrids.yml.
Exactly in this path OroPlatform is looking for datagrid configuration.
https://github.com/oroinc/platform/blob/2.5/src/Oro/Bundle/DataGridBundle/Provider/ConfigurationProvider.php#L154-L160
Please move your configuration into correct directory and filename. And It will be work. And of course you should remove this file loading from DependencyInjection/MyBundleExtension.php OroPlatform do it automatically.
For more detailed understanding how you can use OroPlatform datagrid features please read bundle documentation
https://github.com/oroinc/platform/blob/2.5/src/Oro/Bundle/DataGridBundle/Resources/doc/backend/datagrid.md

Register Twig Extension in Symfony

I want to use nochso/html-compress-twig extension to compress all html, inline css and js.But it is the first time that I register a new extension on Twig and I am bit confused about where I should add the following lines in my project:
$twig = new Twig_Environment($loader);
$twig->addExtension(new \nochso\HtmlCompressTwig\Extension());
I was reading the Twig's documentation but it didn't helped me much as they put the same example and just add the following:
Twig does not care where you save your extension on the filesystem, as all extensions must be registered explicitly to be available in your templates.
You can register an extension by using the addExtension() method on your main Environment object:
I only want enable the extension globally and be able to use {% htmlcompress %}{% endhtmlcompress %} in any twig template
You can register your twig extension as a tagged service this way:
services:
htmlcompress:
class: '\nochso\HtmlCompressTwig\Extension'
tags:
- { name: twig.extension }
To enable a Twig extension, add it as a regular service... http://symfony.com/doc/current/reference/dic_tags.html#twig-extension

Add custom Sonata page route to the navbar

I've created a custom Sonata page
Simple route
medapp_adminStreamCommands:
path: /admin/stream
defaults: { _controller: MedAppBundle:VideoChat/VideoChat:adminStreamCommands }
Controller that returns the admin pool
public function adminStreamCommandsAction(Request $request)
{
return $this->render('#MedApp/AdminSonata/Stream/stream_commands.html.twig', array(
'admin_pool' => $this->get('sonata.admin.pool')));
}
Plain view template
{% extends '#MedApp/AdminSonata/standard_layout.html.twig' %}
{% block content %}
foobar
{% endblock content
This works, I can access it on my website with /admin/foo and I get a page which has the Sonata admin template with my 'foobar' content.
My question is, how can I add this route to the left and top navbar without having to modify the default template?
That is because the left menu is rendered by a KNP menu:
{% block side_bar_nav %}
{% if app.user and is_granted('ROLE_SONATA_ADMIN') %}
{{ knp_menu_render('sonata_admin_sidebar', {template: admin_pool.getTemplate('knp_menu_template')}) }}
{% endif %}
{% endblock side_bar_nav %}
And I somehow need to add my new page to be rendered by this menu.
Normally, a page is added through a service, but these are built on top of an entity:
servicename:
class: Bundle\Class
arguments: [~, Bundle\Entity\Entityname, ~]
tags:
- { name: sonata.admin, manager_type: orm, group: admin, label: CustomName}
My page is not using an entity, though, just static content or content that is not dependant on an entity.
I know already that I can modify the blocks that generate the menus, but I was thinking that the best way would be to add my class as a service tagged as sonata.admin that doesn't have an orm manager_type, in other words, is not an Entity. How can that be done?
You should override standard_layout and modify content of side_bar_nav block. This is simple and fast way. Or you can dig into sonata code to find how to inject something into admin_pool.dashboardgroups - have fun :)
I don't think that's possible, you have to create a new layout, copy the sonata admin layout and customize it to your need.
You can change the layout used by changing the yml configuration for sonata_admin (templates -> layout) or extending the SonataAdmin bundle and creating your own layout.html.twig.

Is It Possible to Register or Publish Asset Files css,js from Bundle in Symfony 2

How to register js,css file to head tag from inside bundle automatically? so we don't need to add it manually to layout.
In zend framework there is HeadScript and InlineScript Helper, prepend or append method to do this. How about symfony2 ? is it possible too?
In zend framework we can register it like this from module bootstrap :
public function onBootstrap($e) {
$sm = $e->getApplication()->getServiceManager();
$headLink = $sm->get('viewhelpermanager')->get('headLink');
$headLink->appendStylesheet('/assets/MyModule/css/mystylesheet.css');
}
I mean, I want register it from e.g AcmeBlogBundle.php so we don't need to add manually to <head></head> in layout.
I'm not sure if this is what you are looking for, but check this out, this shows you how to include every js-file inside a directory, without listing them all manually.
You can't do it like in Zend. But you can define the appropriate block with js in base layout only once. And then from any template you can put anything you want into this block. Symfony implements a bit different logic than Zend does.
In your layout:
<head>
{% block custom_head_js %}
<some basic js files for all the pages>
{% endblock%}
</head>
In your templeate:
{% block custom_head_js
'#YourBundleNamespace/YourBundleName/Resources/public/js/your_js_filename.js'
%}
<script src="{{ asset_url }}"></script>
{% endblock %}
So, as I said it's not necessary to change base layout every time you want to add js into 'head' js files - you have to do it only once.

Categories