PHP code example of starring-jane / acf-gutenberg-blocks

1. Go to this page and download the library: Download starring-jane/acf-gutenberg-blocks 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/ */

    

starring-jane / acf-gutenberg-blocks example snippets


use StarringJane\GutenbergBlocks\Block;

class MyCustomBlock extends Block
{
    protected $name = "custom";

    protected $title = "My Custom block";

    protected $description = "This is a custom block!";

    protected $category = 'custom';
    
    public function render()
    {
        // Render the HTML
    }
}

protected $name;

protected $title;

protected $description;

protected $keywords = [];

protected $category;

protected $icon;

protected $mode = 'preview';

protected $default_align = '';

protected $align = ['wide', 'full'];

protected $allow_mode_switch = true;

protected $allow_multiple = true;

protected $parent = [];

protected $inner_blocks = false;

protected $wrap = true;

add_filter('acf/init', function () {
    \StarringJane\GutenbergBlocks\Gutenberg::register([
        MyCustomBlock::class,
    ]);
});

add_filter('acf/init', function () {
    // Registers all blocks in `wp-content/themes/{current_theme}/blocks`
    \StarringJane\GutenbergBlocks\Gutenberg::register('blocks');
});

class ChildBlock extends Block
{
    // ...

    protected $parent = [
        ParentBlock::class,
    ];
}

public function fields()
{
    return [
        Text::make('Title')
            ->ired()
            ->instructions('The url that should be displayed.'),
    ];
}

// Retrieve all fields as an array
$data = $this->data();

// Retrieve only the title
$title = $this->get('title');

// Retrieve the body and provide a default value as fallback
$body = $this->get('body', 'Hello world!');

// ...

// Override the default classes
protected function setClasses($block)
{
    return [
        'my-custom-css-class',
        //
    ]);
}

// Extend the default classes
protected function setClasses($block)
{
    return array_merge(parent::setClasses($block), [
        'my-custom-css-class',
    ]);
}

public function render()
{
    // Make the data available in our template
    set_query_var('data', $this->data());

    return get_template_part('path-to-my-block-template');
}

use StarringJane\GutenbergBlocks\Block;
use StarringJane\WordpressBlade\RendersBlade;

class MyCustomBlock extends Block
{
    use RendersBlade;

    // ...

    public function render()
    {
        return $this->view('blade.custom-block', [
            'data' => $this->data(),
        ]);
    }
}


public function render()
{
    // Make the CSS-classes available as a variable in the template
    set_query_var('default_classes', $this->getClasses());

    return get_template_part('path-to-my-block-template');
}