40q/block-handler

Handle custom Gutenberg blocks in a Roots/Radicle project
6,134 2
Install
composer require 40q/block-handler
Latest Version:v1.0.9
PHP:^8.1
Laravel:4 – 12
License:MIT
Last Updated:Aug 7, 2025
Links: GitHub  ·  Packagist
Maintainer: jdebuchy

40Q Block handler

Handle custom Gutenberg blocks in a Roots/Radicle project

Installation

  1. Install package with composer:
composer require 40q/block-handler
  1. 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,
],
  1. 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);
  1. Create a Blocks folder inside app 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,
        ]);
    }
}

Related Packages

doctrine/dbal

Powerful PHP database abstraction layer (DBAL) with many features for database s...

644,549,754 9,698
laravel/framework

The Laravel Framework.

598,760,099 34,966
laravel/tinker

Powerful REPL for the Laravel framework.

507,193,856 7,441
laravel/serializable-closure

Laravel Serializable Closure provides an easy and secure way to serialize closur...

423,477,751 607
nunomaduro/collision

Cli error handling for console/command-line PHP applications.

403,217,078 4,664

Version History

Version Released PHP Laravel License
v1.0.9 ^8.1 * MIT
v1.0.8 ^8.1 * MIT
v1.0.7 ^8.1 * MIT
v1.0.6 ^8.1 ^9.0 MIT
v1.0.5 ^8.1 ^9.0 MIT