40q / block-handler
Handle custom Gutenberg blocks in a Roots/Radicle project
v1.0.7
2024-03-22 17:49 UTC
Requires
- php: ^8.1
- illuminate/support: *
Requires (Dev)
- phpunit/phpunit: ^9.0
README
Handle custom Gutenberg blocks in a Roots/Radicle project
Installation
- Install package with composer:
composer require 40q/block-handler
- Find the providers array in
config/app.php
, and add the service provider class to the end of it.
'providers' => [ // Other Service Providers... BlockHandler\Providers\BlockHandlerServiceProvider::class, ],
- Use the package in
BlocksServiceProvider.php
add_filter('render_block', function ($block_content, $block) { try { $factory = app(BlockHandler::class); $handlerClass = $factory->getHandler($block['blockName']); if ($handlerClass) { $handlerInstance = new $handlerClass(); return $handlerInstance($block_content, $block); } } catch (\Exception $e) { error_log($e->getMessage()); } return $block_content; }, 10, 2);
- Create a
Blocks
folder insideapp
and put your handlers inside.
Block handlers
The package will try to use each file inside app\Blocks
as a block handler. In order for this to work as expected, make sure all classes follow the BlockHandler
contract.
Blocks with their block handlers can be generated with the 40q cli tool:
40q codegen block
Example:
<?php namespace App\Blocks; use BlockHandler\Contracts\BlockHandler; class Modal implements BlockHandler { public function __invoke($block_content, $block) { return view('blocks.modal', [ 'block' => $block, 'blockContent' => $block_content, 'buttonText' => $block['attrs']['buttonText'] ?? null, 'heading' => $block['attrs']['heading'] ?? null, ]); } }