PHPackages                             40q/block-handler - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. 40q/block-handler

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

40q/block-handler
=================

Handle custom Gutenberg blocks in a Roots/Radicle project

v1.0.9(9mo ago)25.4k↓50%1MITPHPPHP ^8.1

Since Oct 20Pushed 9mo ago2 watchersCompare

[ Source](https://github.com/40Q/block-handler)[ Packagist](https://packagist.org/packages/40q/block-handler)[ RSS](/packages/40q-block-handler/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (2)Versions (12)Used By (0)

40Q Block handler
=================

[](#40q-block-handler)

Handle custom Gutenberg blocks in a Roots/Radicle project

Installation
------------

[](#installation)

1. Install package with composer:

```
composer require 40q/block-handler

```

2. 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,
],
```

3. 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);
```

4. Create a `Blocks` folder inside `app` and put your handlers inside.

Block handlers
--------------

[](#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](https://github.com/40Q/40q-cli):

```
40q codegen block

```

**Example:**

```
