diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index df45c5cc..5be1ab22 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -30,6 +30,12 @@ public function getConfigTreeBuilder() ->booleanNode('tinymce_jquery')->defaultFalse()->end() // Set init to true to use callback on the event init ->booleanNode('use_callback_tinymce_init')->defaultFalse()->end() + // Selector + ->scalarNode('selector')->defaultValue('.tinymce')->end() + // base url for content + ->scalarNode('base_url')->end() + // asset packageName + ->scalarNode('asset_package_name')->end() // Default language for all instances of the editor ->scalarNode('language')->defaultNull()->end() @@ -93,14 +99,14 @@ private function getTinymceDefaults() "advlist autolink lists link image charmap print preview hr anchor pagebreak", "searchreplace wordcount visualblocks visualchars code fullscreen", "insertdatetime media nonbreaking save table contextmenu directionality", - "emoticons template paste textcolor" + "emoticons template paste textcolor", ), "toolbar1" => "undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image", "toolbar2" => "print preview media | forecolor backcolor emoticons", "image_advtab" => true, ), - 'simple' => array() + 'simple' => array(), ); } } \ No newline at end of file diff --git a/DependencyInjection/StfalconTinymceExtension.php b/DependencyInjection/StfalconTinymceExtension.php index 000d14a5..a9ed99b7 100644 --- a/DependencyInjection/StfalconTinymceExtension.php +++ b/DependencyInjection/StfalconTinymceExtension.php @@ -42,7 +42,7 @@ public function load(array $configs, ContainerBuilder $container) $container->setParameter('stfalcon_tinymce.config', $configuration); // load dependency injection config - $loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); + $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); $loader->load('service.xml'); } diff --git a/Helper/LocaleHelper.php b/Helper/LocaleHelper.php index ca39cee7..2c75f10b 100644 --- a/Helper/LocaleHelper.php +++ b/Helper/LocaleHelper.php @@ -20,7 +20,7 @@ class LocaleHelper 'sl' => 'sl_SI', 'tr' => 'tr_TR', 'tw' => 'zh_TW', - 'uk' => 'uk_UA' + 'uk' => 'uk_UA', ); /** @@ -30,10 +30,8 @@ class LocaleHelper */ public static function getLanguage($locale) { - return isset(self::$locales[$locale]) ? self::$locales[$locale] : $locale; } } - \ No newline at end of file diff --git a/README.md b/README.md index 54d4c428..9b4db420 100644 --- a/README.md +++ b/README.md @@ -4,20 +4,22 @@ This bundle makes it very easy to add the TinyMCE WYSIWYG editor to your Symfony ## Installation -### Installation by Composer +### Choose the appropriate version -> NOTE! This version of TinyMCE bundle contains TinyMCE version 4 and works only with Symfony version >= 2.1. To upgrade your configuration, please read UPGRADE.md +| Bundle Version (X.Y) | PHP | Symfony | Comment | +|:--------------------:|:-------:|:------------------:|------------------------------------------| +| 2.0 | >= 5.4 | >= 3.0 | Actual version | +| 1.0 | >= 5.4 | >= 2.1 and <= 2.8 | | -Add TinyMCE bundle as a dependency to the composer.json of your application +> NOTE! To upgrade your configuration, please read UPGRADE.md - "require": { - ... - "stfalcon/tinymce-bundle": "dev-master" - ... - }, +### Add TinyMCE bundle as a dependency of your application via composer +``` +$ php composer.phar require stfalcon/tinymce-bundle='X.Y' +``` -## Add StfalconTinymceBundle to your application kernel. +### Add StfalconTinymceBundle to your application kernel. ```php // app/AppKernel.php @@ -32,10 +34,10 @@ Add TinyMCE bundle as a dependency to the composer.json of your application } ``` -The bundle needs to copy the resources necessary to the web folder. You can use the command below: +### The bundle needs to copy the resources necessary to the web folder. You can use the command below: -```bash - php app/console assets:install web/ +``` +$ php app/console assets:install web/ ``` ## Include in template @@ -52,6 +54,21 @@ You can also override the default configuration by passing an option like this: {{ tinymce_init({'use_callback_tinymce_init': true, 'theme': {'simple': {'menubar': false}}}) }} ``` + or + +``` + {{ tinymce_init({ + theme: {'simple':{'language': app.request.locale, 'height': 500 }}, + toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image", + autosave_ask_before_unload: false, + asset_package_name: 'backend'}) + }} +``` + + +***NEW !*** Added posibility to specify asset package [doc](http://symfony.com/doc/current/components/templating/helpers/assetshelper.html#multiple-packages) to generate proper js links, see above, parameter: asset_package_name + + ## Base configuration By default, tinymce is enabled for all textareas on the page. If you want to customize it, do the following: @@ -265,7 +282,7 @@ To initialize TinyMCE for new loaded textareas you should just call `initTinyMCE ```javascript jQuery(document).ready(function() { - $('form').on('sonata-collection-item-added', function(){ + $('form').on('sonata.add_element', function(){ initTinyMCE(); }); }); diff --git a/Resources/views/Script/init.html.twig b/Resources/views/Script/init.html.twig index ff0957ac..e907189c 100644 --- a/Resources/views/Script/init.html.twig +++ b/Resources/views/Script/init.html.twig @@ -2,16 +2,16 @@ {% endif %} {% if tinymce_jquery %} - - + + {% else %} - - - + + + {% endif %} diff --git a/Twig/Extension/StfalconTinymceExtension.php b/Twig/Extension/StfalconTinymceExtension.php index 56b068af..bb11ba60 100644 --- a/Twig/Extension/StfalconTinymceExtension.php +++ b/Twig/Extension/StfalconTinymceExtension.php @@ -12,14 +12,13 @@ class StfalconTinymceExtension extends \Twig_Extension { /** - * Container - * - * @var ContainerInterface + * @var ContainerInterface $container Container interface */ protected $container; /** * Asset Base Url + * * Used to over ride the asset base url (to not use CDN for instance) * * @var String @@ -67,15 +66,16 @@ public function getParameter($name) */ public function getFunctions() { - return array( - 'tinymce_init' => new \Twig_Function_Method($this, 'tinymceInit', array('is_safe' => array('html'))) - ); + return [ + new \Twig_Function('tinymce_init', 'tinymceInit', ['is_safe' => ['html']]) + ]; } /** * TinyMce initializations * * @param array $options + * * @return string */ public function tinymceInit($options = array()) @@ -84,28 +84,34 @@ public function tinymceInit($options = array()) $config = array_merge_recursive($config, $options); $this->baseUrl = (!isset($config['base_url']) ? null : $config['base_url']); + + // Asset package name + $assetPackageName = (!isset($config['asset_package_name']) ? null : $config['asset_package_name']); + unset($config['asset_package_name']); + /** @var $assets \Symfony\Component\Templating\Helper\CoreAssetsHelper */ - $assets = $this->getService('templating.helper.assets'); + $assets = $this->getService('assets.packages'); // Get path to tinymce script for the jQuery version of the editor if ($config['tinymce_jquery']) { $config['jquery_script_url'] = $assets->getUrl( - $this->baseUrl . 'bundles/stfalcontinymce/vendor/tinymce/tinymce.jquery.min.js' + $this->baseUrl.'bundles/stfalcontinymce/vendor/tinymce/tinymce.jquery.min.js', + $assetPackageName ); } // If the language is not set in the config... if (!isset($config['language']) || empty($config['language'])) { // get it from the request - $config['language'] = $this->getService('request')->getLocale(); + $config['language'] = $this->container->get('request_stack')->getCurrentRequest()->getLocale(); } $config['language'] = LocaleHelper::getLanguage($config['language']); - $langDirectory = __DIR__ . '/../../Resources/public/vendor/tinymce/langs/'; + $langDirectory = __DIR__.'/../../Resources/public/vendor/tinymce/langs/'; // A language code coming from the locale may not match an existing language file - if (!file_exists($langDirectory . $config['language'] . '.js')) { + if (!file_exists($langDirectory.$config['language'].'.js')) { unset($config['language']); } @@ -141,13 +147,14 @@ public function tinymceInit($options = array()) { // Parse the content_css of each theme so we can use 'asset[path/to/asset]' in there foreach ($themeConfig['theme'] as $themeName => $themeOptions) { - if(isset($themeOptions['content_css'])) - { + if (isset($themeOptions['content_css'])) { // As there may be multiple CSS Files specified we need to parse each of them individually - $cssFiles = explode(',', $themeOptions['content_css']); + $cssFiles = $themeOptions['content_css']; + if (!is_array($themeOptions['content_css'])) { + $cssFiles = explode(',', $themeOptions['content_css']); + } - foreach($cssFiles as $idx => $file) - { + foreach ($cssFiles as $idx => $file) { $cssFiles[$idx] = $this->getAssetsUrl(trim($file)); // we trim to be sure we get the file without spaces. } @@ -164,14 +171,26 @@ public function tinymceInit($options = array()) } } - return $this->getService('templating')->render('StfalconTinymceBundle:Script:init.html.twig', array( - 'tinymce_config' => preg_replace( - '/"file_browser_callback":"([^"]+)"\s*/', 'file_browser_callback:$1', - json_encode($config) + $tinymceConfiguration = preg_replace( + array( + '/"file_browser_callback":"([^"]+)"\s*/', + '/"file_picker_callback":"([^"]+)"\s*/', + '/"paste_preprocess":"([^"]+)"\s*/', + ), + array( + 'file_browser_callback:$1', + 'file_picker_callback:$1', + '"paste_preprocess":$1', ), - 'include_jquery' => $config['include_jquery'], - 'tinymce_jquery' => $config['tinymce_jquery'], - 'base_url' => $this->baseUrl + json_encode($config) + ); + + return $this->getService('templating')->render('StfalconTinymceBundle:Script:init.html.twig', array( + 'tinymce_config' => $tinymceConfiguration, + 'include_jquery' => $config['include_jquery'], + 'tinymce_jquery' => $config['tinymce_jquery'], + 'asset_package_name' => $assetPackageName, + 'base_url' => $this->baseUrl, )); } @@ -195,12 +214,12 @@ public function getName() protected function getAssetsUrl($inputUrl) { /** @var $assets \Symfony\Component\Templating\Helper\CoreAssetsHelper */ - $assets = $this->getService('templating.helper.assets'); + $assets = $this->getService('assets.packages'); $url = preg_replace('/^asset\[(.+)\]$/i', '$1', $inputUrl); if ($inputUrl !== $url) { - return $assets->getUrl($this->baseUrl . $url); + return $assets->getUrl($this->baseUrl.$url); } return $inputUrl; diff --git a/composer.json b/composer.json index ad1d5682..712b1221 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "stfalcon/tinymce-bundle", "description": "This Bundle integrates TinyMCE WYSIWYG editor into a Symfony2 project.", - "keywords": ["wysiwyg", "tinymce", "editor"], + "keywords": ["wysiwyg", "tinymce", "editor", "symfony2", "bundle"], "type": "symfony-bundle", "license": "MIT", "authors": [ @@ -18,11 +18,11 @@ { "name": "Symfony Community", "homepage": "https://github.com/stfalcon/TinymceBundle/contributors" - } + } ], "require": { - "php": ">=5.3.0", - "symfony/symfony": ">=2.1" + "php": ">=5.4.0", + "symfony/symfony": ">=2.8" }, "autoload": { "psr-0": { @@ -32,7 +32,7 @@ "target-dir" : "Stfalcon/Bundle/TinymceBundle", "extra": { "branch-alias": { - "dev-master": "0.3.x-dev" + "dev-master": "2.x-dev" } } }