sonata-project/twig-extensions
Provides a set of useful Twig extensions for Sonata projects, adding extra filters, functions, and helpers to enhance templates. Install via Composer and integrate with Twig to simplify common view tasks and formatting.
Installation Add the package via Composer (ensure compatibility with Symfony 8+ and PHP 8.2+):
composer require sonata-project/twig-extensions:^2.6.0
Register the service provider in config/app.php under providers:
SonataProject\Twig\Extensions\TwigExtensionsServiceProvider::class,
First Use Case
Use the sonata_block Twig extension to render a block in a template:
{{ sonata_block('block_name') }}
Verify the block exists in your Sonata admin configuration (e.g., config/packages/sonata_admin.yaml).
Note: Ensure your Symfony version is 8.0+ (this release drops support for Symfony 7.x).
Where to Look First
src/Twig/Extension/ for core extensions.config/packages/sonata_twig_extensions.yaml for default settings.composer.json constraints for PHP 8.2+ and Symfony 8+.Dynamic Block Rendering
Use sonata_block with dynamic names (e.g., {{ sonata_block('user_' ~ user.id) }}) for user-specific blocks.
Example: Render a dashboard block conditionally:
{% if is_granted('ROLE_ADMIN') %}
{{ sonata_block('admin_dashboard') }}
{% endif %}
Form Integration
Leverage sonata_form for enhanced Symfony forms:
{{ form_start(form) }}
{{ sonata_form_row(form.field) }} {# Renders field with Sonata styling #}
{{ form_end(form) }}
Asset Management
Use sonata_asset for optimized asset handling:
{{ sonata_asset('bundles/sonataadmin/css/style.css') }}
Localization
Extend translations with sonata_trans:
{{ 'sonata.admin.block.title'|trans }}
symfony/flex is up to date.php bin/console cache:clear after adding new blocks or extensions.App\Twig\AppExtension:
use SonataProject\Twig\Extensions\TwigExtensions;
public function getExtensions(TwigEnvironment $env) {
return array_merge(parent::getExtensions($env), [
new TwigExtensions(),
]);
}
Note: Test with PHP 8.2+ and Symfony 8+ to avoid compatibility issues.Block Registration
sonata_block fails with "Block not found" errors.services.yaml or sonata_admin.yaml):
sonata.block:
blocks:
sonata.admin.block.admin_list:
class: Sonata\AdminBundle\Block\BlockAdminListBlock
contexts: [admin]
Twig Environment Conflicts
# config/bundles.php
Twig\ExtraBundle\TwigExtraBundle::class => ['all' => false],
Asset Paths
sonata_asset returns 404 for bundles.config/bundles.php and assets are built:
php bin/console assets:install
PHP/Symfony Version Mismatch
composer.json to enforce PHP 8.2+ and Symfony 8+:
{
"require": {
"php": "^8.2",
"symfony/*": "^8.0"
}
}
config/packages/twig.yaml:
twig:
debug: '%kernel.debug%'
strict_variables: '%kernel.debug%'
{% if app.debug %}
{{ dump(sonata_block('block_name', {'debug': true})) }}
{% endif %}
Custom Filters
Create a custom Twig filter in App\Twig\AppExtension:
public function getFilters() {
return [
new \Twig\TwigFilter('custom_filter', [$this, 'customFilterMethod']),
];
}
Override Existing Extensions Replace Sonata’s default extensions by redefining the service:
# config/services.yaml
SonataProject\Twig\Extensions\TwigExtensions:
arguments:
$someDependency: '@custom.service'
Event Listeners
Hook into Sonata’s block events (e.g., sonata.block.pre_render):
use Sonata\BlockBundle\Event\BlockEvent;
public static function onPreRender(BlockEvent $event) {
$event->getBlock()->setSetting('custom_key', 'value');
}
Note: Test event listeners with Symfony 8+ to ensure compatibility.
Symfony 8-Specific Adjustments
How can I help you explore Laravel packages today?