attuladzan/gravity-editor-bundle
## Getting Started
### Minimal Setup
1. **Install the bundle**:
```bash
composer require attuladzan/gravity-editor-bundle
For non-Flex projects, add to config/bundles.php:
Attuladzan\MarkdownEditorBundle\AttuladzanMarkdownEditorBundle::class => ['all' => true],
Install assets (required for frontend functionality):
php bin/console attuladzan:markdown-editor:install-assets --build
Use --symlink for development to avoid rebuilding assets repeatedly.
Basic configuration (optional, defaults are provided):
# config/packages/attuladzan_markdown_editor.yaml
attuladzan_markdown_editor:
editor:
lang: en # or 'ru'
Add the editor to a form field:
use Attuladzan\MarkdownEditorBundle\Form\MarkdownEditorType;
$builder->add('description', MarkdownEditorType::class, [
'label' => 'Content',
'required' => false,
]);
Customize editor options per field:
$builder->add('content', MarkdownEditorType::class, [
'editor_options' => [
'allow_html' => true,
'sticky_toolbar' => false,
],
]);
Validation: Use Symfony’s validation constraints (e.g., NotBlank, Length) as usual. The editor handles markdown input, but validation applies to the raw output.
Data transformation: Override setData() or getData() in a custom form type to preprocess markdown or sanitize HTML if allow_html: true.
Embed the editor in templates:
{{ gravity_markdown_editor({
name: 'article_content',
value: article.content,
attributes: { class: 'custom-editor' }
}) }}
Dynamic configuration: Pass editor options via the editor_options parameter:
{{ gravity_markdown_editor({
name: 'bio',
editor_options: { lang: 'ru', allow_html: false }
}) }}
Preview mode: For read-only previews, use the readonly option:
{{ gravity_markdown_editor({ value: article.content, readonly: true }) }}
Add the field to a CRUD controller:
use Attuladzan\MarkdownEditorBundle\EasyAdmin\Field\MarkdownEditorField;
yield MarkdownEditorField::new('longDescription')
->setFormTheme('@AttuladzanMarkdownEditor/Form/attuladzan_markdown_editor_widget.html.twig');
Enable markdown preview (optional):
composer require twig/extra-bundle
markdown filter if available).Customize per entity:
yield MarkdownEditorField::new('notes')
->setEditorOptions(['allow_html' => false, 'lang' => 'ru']);
Enable plugins in config/packages/attuladzan_markdown_editor.yaml:
attuladzan_markdown_editor:
plugins:
mermaid: true
latex: true
Clear cache afterward:
php bin/console cache:clear
Use plugins in forms/Twig:
$...$ for inline math and $$...$$ for block math.Debugging plugins: Verify plugin inclusion with:
php bin/console debug:config attuladzan_markdown_editor
Check the plugins section for enabled features.
Development workflow:
--symlink to avoid rebuilding assets:
php bin/console attuladzan:markdown-editor:install-assets --symlink
cd vendor/attuladzan/gravity-editor-bundle && npm install && npm run build
Production:
--build to ensure assets are compiled:
php bin/console attuladzan:markdown-editor:install-assets --build
Assets not loading:
install-assets or assets weren’t rebuilt.php bin/console attuladzan:markdown-editor:install-assets --build and check browser console for 404 errors.Editor not rendering in EasyAdmin:
->addFormTheme('@AttuladzanMarkdownEditor/Form/attuladzan_markdown_editor_widget.html.twig') and ensure easycorp/easyadmin-bundle is installed.Plugin features missing:
node_modules.XSS vulnerabilities:
allow_html: true with untrusted input.htmlpurifier or validate input strictly.Check editor initialization:
Open browser dev tools (F12) and inspect the editor’s console logs for errors. Look for:
gravity-markdown-editor.js.Validate configuration: Use the debug command to verify settings:
php bin/console debug:config attuladzan_markdown_editor
Compare output with your config/packages/attuladzan_markdown_editor.yaml.
Clear cache aggressively: After changing config or plugins, run:
php bin/console cache:clear --env=prod
Customize the editor instance: Override the Twig function or form type to pass additional options:
{{ gravity_markdown_editor({
name: 'content',
editor_options: {
custom_plugin: {
enabled: true,
config: { /* ... */ }
}
}
}) }}
Extend plugins: The bundle supports Gravity UI’s plugin system. To add a custom plugin:
resources/js/app.js or override it in your project’s assets.Override templates:
Copy the bundle’s Twig templates to your project (e.g., templates/bundles/AttuladzanMarkdownEditor/) to customize rendering.
Lazy-load plugins:
Disable unused plugins (e.g., mermaid, latex) to reduce bundle size:
attuladzan_markdown_editor:
plugins:
mermaid: false
latex: false
Asset optimization:
For production, ensure assets are minified and cached. Use --build flag during installation.
Form type optimization:
Avoid passing large editor_options objects to forms. Use bundle-level config for shared settings and override only what’s necessary per field.
Language support:
The editor supports en and ru out of the box. Add other languages by:
Form labels:
Localize form labels in your Symfony translation files (e.g., translations/messages.ru.yaml).
HTML sanitization:
If allow_html: true, always sanitize output before rendering in templates:
{{ article.content|striptags }}
Or use a library like htmlpurifier.
Plugin safety:
Plugins like latex or mermaid may introduce XSS risks if user input is rendered unsafely. Validate and sanitize dynamic content.
CSRF protection: Ensure forms using the editor include Symfony’s CSRF token for security.
---
How can I help you explore Laravel packages today?