symfony-cmf/block-bundle
Symfony CMF BlockBundle lets you create and manage reusable content blocks in Symfony apps, integrating with PHPCR-ODM and the CMF stack. Define blocks in code or the CMS, render them in templates, and reuse them across pages with flexible block types.
Installation
composer require symfony-cmf/block-bundle
Ensure sonata-block-bundle is also installed (dependency).
Configuration
Add to config/packages/sonata_block.yaml:
sonata_block:
default_contexts: [cms]
blocks:
sonata.block.service.block: ~
sonata.block.service.text: ~
# Add PHPCR-specific blocks if needed
First Use Case
Sonata\BlockBundle\Block\BaseBlockService.services.yaml:
App\Block\CustomPhpcrBlock:
tags: [sonata.block]
Template Path Update (2.1.1+)
CmfBlockBundl:Block:some-template.html.twig) with the new Twig namespace format:
{% extends '@CmfBlock/Block/some-template.html.twig' %}
Fetching Blocks from PHPCR
Use PHPCR\Session to query blocks stored in a PHPCR repository (e.g., Jackalope):
$session = $this->get('phpcr.session');
$query = $session->createQuery(
"SELECT [jcr:path] FROM [nt:base] WHERE [jcr:path] LIKE '/blocks/%'",
Query::JCR_SQL2
);
$results = $query->execute();
Dynamic Block Rendering
Override execute() in a custom block service:
public function execute(BlockContextInterface $blockContext, Response $response = null)
{
$blockData = $this->getPhpcrBlockData($blockContext->getSetting('path'));
return $this->renderView('@CmfBlock/Block/custom.html.twig', ['data' => $blockData]);
}
Caching Strategies Cache PHPCR queries and block responses:
# config/packages/cache.yaml
app.cache.phpcr_blocks:
provider: 'file_system'
namespace: 'phpcr_blocks'
Sonata\AdminBundle\Admin\Admin to manage PHPCR blocks via the admin panel.sonata.block.event.BLOCK_VIEW to pre-process block data from PHPCR.$this->twig->addExtension(new PhpcrBlockExtension($this->get('phpcr.session')));
Ensure Twig paths use the new namespace format (e.g., @CmfBlock/Block/...).Deprecated PHPCR Support
Session Management
services:
app.phpcr.session:
factory: ['@phpcr.session_factory', 'get']
arguments: ['default']
public: false
Query Performance
/blocks/**) can be slow. Use jcr:like sparingly.Template Path Breaking Change (2.1.1)
CmfBlockBundl:Block:some-template.html.twig (deprecated).@CmfBlock/Block/some-template.html.twig.# config/packages/monolog.yaml
handlers:
phpcr:
type: stream
path: "%kernel.logs_dir%/phpcr.log"
level: debug
blockContext->getSetting('path') exist in PHPCR.@CmfBlock/Block/ namespace.Custom Block Types
Extend Sonata\BlockBundle\Block\BlockContextInterface to add PHPCR-specific settings:
class PhpcrBlockContext extends BlockContext
{
public function getPhpcrNode(): NodeInterface
{
return $this->session->getNode($this->getSetting('phpcr_path'));
}
}
Repository Abstraction Decouple PHPCR logic with a service:
class PhpcrBlockRepository
{
public function findBlock(string $path): ?NodeInterface
{
// Custom logic to fetch from PHPCR
}
}
Migration Path If moving away from PHPCR, use Doctrine Migrations to export block data to a relational DB.
How can I help you explore Laravel packages today?