PHP code example of 40q / block-handler

1. Go to this page and download the library: Download 40q/block-handler library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

40q / block-handler example snippets


'providers' => [
    // Other Service Providers...

    BlockHandler\Providers\BlockHandlerServiceProvider::class,
],

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);



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,
        ]);
    }
}