agence-adeliom/easy-gutenberg-bundle
Integrate WordPress’s Gutenberg editor into Symfony EasyAdmin. Add a GutenbergField to CRUD forms, use the provided form theme, and generate custom blocks via a console maker command. Compatible with Symfony 5.4–7.x and PHP 8.0.2+.
Installation:
composer require agence-adeliom/easy-gutenberg-bundle
Ensure your composer.json includes the custom Flex endpoint:
"extra": {
"symfony": {
"endpoint": [
"https://api.github.com/repos/agence-adeliom/symfony-recipes/contents/index.json?ref=flex/main",
...
],
"allow-contrib": true
}
}
Enable the Bundle:
Add to config/bundles.php:
return [
// ...
AgenceAdeliom\EasyGutenbergBundle\EasyGutenbergBundle::class => ['all' => true],
];
First Use Case:
Modify a CRUD controller (e.g., PageCrudController) to use GutenbergField:
use AgenceAdeliom\EasyAdminBundle\Field\GutenbergField;
$this->crud->addField(GutenbergField::new('content')->setLabel('Gutenberg Content'));
config/packages/easy_gutenberg.yaml (if auto-generated).GutenbergField in src/Field/GutenbergField.php.tests/ for usage examples (if available).Basic Gutenberg Integration:
Replace or extend existing TextField/HtmlField with GutenbergField for rich content editing:
$this->crud->addField(
GutenbergField::new('article_body')
->setLabel('Article Content')
->setRequired(true)
);
Dynamic Configuration:
Use setConfig() to customize Gutenberg editor behavior:
->setConfig([
'allowedBlocks' => ['core/paragraph', 'core/image', 'core/heading'],
'mediaUpload' => ['image', 'video'],
]);
Entity-Specific Setup:
Override configureFields() in your CRUD controller to conditionally apply Gutenberg:
public function configureFields(string $pageName): iterable {
yield GutenbergField::new('description')
->setVisible($pageName === 'blog_post');
}
Asset Management:
Ensure easyadmin and easy-gutenberg assets are built:
yarn encore dev --watch
Or configure Symfony Encore to auto-load assets.
Form Integration:
For non-CRUD forms, use GutenbergType in Symfony forms:
use AgenceAdeliom\EasyGutenbergBundle\Form\Type\GutenbergType;
$builder->add('content', GutenbergType::class, [
'config' => ['allowedBlocks' => ['core/paragraph']],
]);
easyadmin-bundle (tested with 3.x).3.x branch.TEXT column (default behavior).setTranslationDomain() for translated labels:
->setTranslationDomain('messages')
Asset Loading:
yarn encore dev or clear cache (php bin/console cache:clear).404 errors on /build/gutenberg-*.Block Restrictions:
setConfig():
->setConfig(['allowedBlocks' => ['core/paragraph']])
Database Schema:
TEXT columns may not handle Gutenberg JSON well.json column type (MySQL 5.7+) or validate JSON with:
->setValidatorConstraints([
new Assert\Json(),
]);
Symfony Cache:
GutenbergField config may not reflect immediately.->setCacheKey(null) to bypass caching.Media Uploads:
mediaUpload in setConfig() and ensure easyadmin media handling is set up.Log Gutenberg Config:
Override configureField() to log the final config:
$field->addModelTransformer(new class implements FieldTransformer {
public function transform($value, Field $field) {
if ($field instanceof GutenbergField) {
error_log('Gutenberg Config:', $field->getConfig());
}
return $value;
}
});
Check Network Requests:
Use browser dev tools to verify Gutenberg assets and API calls (e.g., /api/easyadmin/gutenberg/blocks).
Custom Blocks: Extend the editor with custom blocks by:
wp-block-* script to your assets.setConfig(['allowedBlocks' => ['custom/block']]).Field Transformers: Customize how data is saved/loaded:
$field->setModelTransformer(new class implements FieldTransformer {
public function transform($value, Field $field) {
return json_encode($value, JSON_PRETTY_PRINT);
}
public function reverseTransform($value, Field $field) {
return json_decode($value, true);
}
});
Event Listeners: Hook into Gutenberg events (e.g., block validation) via Symfony events:
# config/services.yaml
services:
App\EventListener\GutenbergListener:
tags:
- { name: kernel.event_listener, event: easy_gutenberg.block_validate, method: onBlockValidate }
Override Templates:
Customize the editor UI by overriding Twig templates in templates/easy_gutenberg/.
setConfig([]) to override all defaults.{{ easy_gutenberg_assets() }}
ROLE_ADMIN).How can I help you explore Laravel packages today?