awaresoft/dynamic-block-bundle
Installation:
composer require awaresoft/dynamic-block-bundle
app/AppKernel.php:
new Awaresoft\DynamicBlockBundle\AwaresoftDynamicBlockBundle(),
First Use Case:
Awaresoft\DynamicBlockBundle\Entity\Block to define custom block types.
Example:
namespace AppBundle\Entity;
use Awaresoft\DynamicBlockBundle\Entity\Block;
class CustomBlock extends Block
{
// Add custom fields/methods
}
sonata_block services in config.yml:
sonata_block:
blocks:
custom_block:
class: AppBundle\Block\CustomBlockService
contexts: [cms]
Database Setup:
php app/console doctrine:schema:update --force
php app/console doctrine:fixtures:load
Block Creation & Management:
BlockManager to create/manage blocks dynamically:
$block = $blockManager->create('custom_block', ['title' => 'Hello']);
$blockManager->save($block);
Resources/views/ (e.g., AwaresoftDynamicBlockBundle:Block:block.html.twig).Sonata Integration:
sonata.admin to manage blocks via UI:
# config.yml
sonata_admin:
options:
delete:
confirmation: "Are you sure you want to delete this block?"
cms, dashboard) in sonata_block config.Reusable Components:
// src/AppBundle/Block/CustomBlockService.php
class CustomBlockService extends BlockService
{
public function execute(BlockContextInterface $blockContext, Response $response = null)
{
// Custom logic
}
}
services:
app.custom_block:
class: AppBundle\Block\CustomBlockService
tags:
- { name: sonata.block }
Caching:
config.yml:
awaresoft_dynamic_block:
cache: true
cache_lifetime: 3600
Symfony 2.x Legacy:
Entity/Block extends Awaresoft\DynamicBlockBundle\Entity\Block (not Sonata\BlockBundle\Model\Block).Twig_Environment). Use polyfills if needed.Sonata Admin Quirks:
ROLE_SONATA_ADMIN_BLOCK is assigned to users managing blocks.sonata_block or sonata_admin.Caching Issues:
php app/console cache:clear
config.yml to test live updates.Database Schema:
doctrine:schema:update after modifying Block entities to avoid schema mismatches.Log Block Events:
app/logs/dev.log for block-related errors.$this->get('logger')->debug('Block rendered', ['block_id' => $block->getId()]);
Template Overrides:
app/Resources/AwaresoftDynamicBlockBundle/views/ to avoid merging conflicts.php app/console assetic:dump --watch --env=dev
Symlinking Vendors:
autoload_psr4.php is updated:
composer dump-autoload
Custom Block Types:
Block entity and create a corresponding service (tagged as sonata.block).class VideoBlock extends Block
{
private $videoUrl;
// Getters/setters
}
Event Listeners:
sonata.block.event.BLOCK_PRE_RENDER):
// src/AppBundle/EventListener/BlockListener.php
class BlockListener
{
public function onPreRender(BlockEvent $event)
{
$block = $event->getBlock();
// Modify block data
}
}
Register in services.yml:
services:
app.block_listener:
class: AppBundle\EventListener\BlockListener
tags:
- { name: kernel.event_listener, event: sonata.block.event.BLOCK_PRE_RENDER, method: onPreRender }
Fixtures:
// src/AppBundle/DataFixtures/ORM/LoadBlockData.php
class LoadBlockData extends AbstractFixture
{
public function load(ObjectManager $manager)
{
$block = new CustomBlock();
$block->setTitle('Test Block');
$manager->persist($block);
$manager->flush();
}
}
php app/console doctrine:fixtures:load
API Integration:
// src/AppBundle/Controller/BlockController.php
class BlockController extends Controller
{
public function getBlocksAction()
{
return $this->get('block.manager')->findAll();
}
}
Route in routing.yml:
app_block_api:
path: /api/blocks
defaults: { _controller: AppBundle:Block:getBlocks }
How can I help you explore Laravel packages today?