Installation:
src/Awaresoft directory and symlink it:
mkdir -p src/Awaresoft
git clone [repo-url] src/Awaresoft/SonataBlockBundle
rm -rf vendor/awaresoft/sonata-block-bundle
composer dump-autoload
Enable the Bundle:
Add to config/bundles.php:
return [
// ...
Awaresoft\SonataBlockBundle\SonataBlockBundle::class => ['all' => true],
];
First Use Case:
Create a basic block type (e.g., TextBlock) in src/Entity/Block/TextBlock.php:
namespace App\Entity\Block;
use Awaresoft\SonataBlockBundle\Model\BlockInterface;
class TextBlock implements BlockInterface {
private $content;
// Implement BlockInterface methods (getType(), setSettings(), etc.)
}
Register it in config/packages/sonata_block.yaml:
sonata_block:
blocks:
text:
class: App\Entity\Block\TextBlock
type: text
Clear Cache:
php bin/console cache:clear
Block Creation & Management:
Awaresoft\SonataBlockBundle\Model\BlockInterface for custom blocks.BlockManager service to fetch/manage blocks:
$blockManager = $this->container->get('sonata.block.manager');
$blocks = $blockManager->getBlocksByContext('admin');
Dynamic Block Rendering:
# config/services.yaml
services:
App\Service\CustomBlockRenderer:
arguments:
- '@sonata.block.renderer'
class CustomBlockRenderer extends AbstractRenderer {
public function renderBlock(BlockInterface $block) {
// Custom logic
return $this->renderView('blocks/custom.html.twig', ['block' => $block]);
}
}
Block Contexts:
sonata_block.yaml:
sonata_block:
contexts:
admin: ~
frontend: ~
$block->setContext(['admin', 'frontend']);
Block Settings:
setSettings()/getSettings() to store block-specific data:
$block->setSettings(['color' => 'red']);
$settings = $block->getSettings();
Admin Integration:
sonata_admin:
templates:
layout: 'SonataAdminBundle::standard_layout.html.twig'
/admin/block.Twig Integration:
{{ render(block) }}
{{ render(blocks|render('frontend')) }}
Event Listeners:
sonata.block.pre_render):
services:
App\EventListener\BlockListener:
tags:
- { name: 'kernel.event_listener', event: 'sonata.block.pre_render', method: 'onPreRender' }
Database Schema:
$table->json('settings')->nullable(); // For custom settings
Security:
use Awaresoft\SonataBlockBundle\Security\BlockVoter;
security:
access_control:
- { path: ^/admin/block, roles: ROLE_ADMIN }
Symlink Issues:
src/Awaresoft/SonataBlockBundle to the project root or not updating autoload_psr4.php will cause autoloading failures.composer dump-autoload.Backward Compatibility:
composer update awaresoft/sonata-block-bundle@dev.Cache Invalidation:
php bin/console cache:pool:clear sonata_block.cache_pool
Context Mismatches:
sonata_block.yaml and assigned to blocks.Database Migrations:
Block Not Rendering:
sonata_block.yaml and assigned to a context.BlockInterface fully.Settings Not Persisting:
setSettings() is called before saving the block entity.settings exists (e.g., json type in PostgreSQL).Admin Panel Issues:
php bin/console sonata:cache:clear
Custom Block Types:
Awaresoft\SonataBlockBundle\Block\BaseBlockService for reusable block logic.Block Renderers:
Awaresoft\SonataBlockBundle\Renderer\RendererInterface for custom rendering pipelines.Block Events:
sonata.block.post_save) for post-processing:
$event = new BlockEvent($block);
$this->eventDispatcher->dispatch($event, 'sonata.block.post_save');
Block Settings Forms:
Awaresoft\SonataBlockBundle\Form\Type\BlockSettingsType for custom admin forms.Block Security:
Awaresoft\SonataBlockBundle\Security\BlockInterface for granular permissions.sonata.block.manager to dynamically fetch blocks by context or type:
$blocks = $blockManager->getBlocksByType('text', 'frontend');
twig:
globals:
current_blocks: '@sonata.block.manager'
BlockManager to update multiple blocks:
$blockManager->updateBlocks($blocks, ['context' => ['frontend']]);
BlockInterface in PHPUnit:
$block = $this->createMock(BlockInterface::class);
$block->method('getType')->willReturn('text');
How can I help you explore Laravel packages today?