Installation:
composer require bdjurisic/tinymce-bundle
(Note: The README mentions stfalcon/tinymce-bundle, but the package name in the prompt is bdjurisic/tinymce-bundle. Use the correct package name.)
Enable the Bundle:
Add to config/bundles.php (Symfony 4+):
return [
// ...
Bdjurisic\TinymceBundle\BdjurisicTinymceBundle::class => ['all' => true],
];
Install Assets:
php bin/console assets:install public
First Usage in Twig:
{{ tinymce('content') }}
(Renders a TinyMCE editor for the content field.)
{{ tinymce('field_name') }} in a form to replace a standard textarea with TinyMCE.
Example:
{{ form_row(article.content) }}
{{ tinymce('article_content') }} {# Override default textarea #}
Form Integration:
form_row() or form_widget() for a TextareaType field:
{{ form_row(article.body) }}
{{ tinymce('article_body') }}
{{ form_row(article.body, {'attr': {'class': 'tinymce'}}) }} if you need to conditionally enable TinyMCE via JS.Configuration via YAML:
Define global settings in config/packages/bdjurisic_tinymce.yaml:
bdjurisic_tinymce:
tinymce_jquery: true # Use jQuery version
selector: 'textarea.tinymce' # Target specific textareas
plugins: ['advlist', 'autolink', 'lists']
toolbar: 'bold italic bullist numlist link'
Dynamic Configuration: Override settings per field in Twig:
{{ tinymce('field_name', {
'plugins': ['image', 'code'],
'toolbar': 'bold italic | image code'
}) }}
Asset Management:
mkdir -p public/build/tinymce
cp vendor/bdjurisic/tinymce-bundle/Resources/public/tinymce/* public/build/tinymce/
config/packages/bdjurisic_tinymce.yaml to point to your custom path:
bdjurisic_tinymce:
base_url: '/build/tinymce'
Laravel-Specific Adaptation:
SymfonyBridge to integrate with Laravel’s Blade:
// In a service provider
$this->app->singleton('tinymce', function () {
return new \Bdjurisic\TinymceBundle\Twig\Extension\TinymceExtension();
});
// In a Blade service provider
Blade::directive('tinymce', function ($expression) {
return "<?php echo \$this->tinymce->render($expression); ?>";
});
Usage in Blade:
@tinymce('content')
API-Driven Content:
setup callback to attach Laravel-specific logic (e.g., image uploads):
{{ tinymce('content', {
'setup': 'function(editor) {
editor.on("init", function() {
editor.addButton("laravel_upload", {
text: "Upload",
onclick: function() { /* Laravel API call */ }
});
});
}'
}) }}
Bundle Name Mismatch:
stfalcon/tinymce-bundle, but the package is bdjurisic/tinymce-bundle. Ensure you install the correct package to avoid ClassNotFoundException.Asset Installation:
assets:install will break TinyMCE’s JS/CSS loading. Always run:
php bin/console assets:install public --symlink
jQuery Dependency:
tinymce_jquery: true, ensure jQuery is loaded before TinyMCE. In Laravel, add to resources/views/layouts/app.blade.php:
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
Symfony 5+ Configuration:
config/packages/, not app/config/. Move YAML files to the correct location.Twig Extension Conflicts:
VichUploaderBundle), ensure namespace collisions don’t occur. Prefix the extension class if needed:
class BdjurisicTinymceExtension extends \Twig\Extension\AbstractExtension
Laravel Mix/Webpack:
webpack.mix.js:
mix.excludeChunks(['tinymce']);
Console Errors:
Uncaught ReferenceError: tinymce is not defined. This usually means:
selector in config doesn’t match your textarea’s class/ID.Configuration Overrides:
dump() in Twig to verify settings:
{{ dump(tinymce_config('field_name')) }}
Plugin Loading:
plugins: [...]).Laravel-Specific Features:
csrf_token() in JS:
{{ tinymce('content', {
'setup': 'function(editor) {
editor.on("init", function() {
$.ajaxSetup({
headers: { "X-CSRF-TOKEN": "{{ csrf_token() }}" }
});
});
}'
}) }}
Performance:
{{ tinymce('content', { 'init': false }) }}
<script>
$(document).ready(function() {
tinymce.init({ selector: 'textarea#content' });
});
</script>
Custom Upload Handler:
Route::post('/tinymce-upload', [YourController::class, 'upload'])->name('tinymce.upload');
{{ tinymce('content', {
'images_upload_handler': 'function(blobInfo, success, failure) {
var formData = new FormData();
formData.append("file", blobInfo.blob(), blobInfo.filename());
$.ajax("{{ route("tinymce.upload") }}", {
method: "POST",
data: formData,
processData: false,
contentType: false,
success: function(url) { success(url); }
});
}'
}) }}
Version Pinning:
composer.json to avoid breaking changes:
"require": {
"bdjurisic/tinymce-bundle": "^3.0"
}
Testing:
$twig = new \Twig\Environment($loader);
$twig->addExtension(new \Bdjurisic\TinymceBundle\Twig\Extension\TinymceExtension());
tinymce in tests to verify output:
$this->assertStringContainsString('tinymce.init', $twig->render('template.html.twig'));
How can I help you explore Laravel packages today?