PHP code example of itinerisltd / acf-gutenblocks

1. Go to this page and download the library: Download itinerisltd/acf-gutenblocks 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/ */

    

itinerisltd / acf-gutenblocks example snippets


use Fully\Qualified\Namespace\Testimonial;
use App\Blocks\Banner;

add_filter('acf_gutenblocks/blocks', function (array $blocks): array {
    $new_blocks = [
        Testimonial::class,
        Banner::class,
    ];
    return array_merge($blocks, $new_blocks);
});

# Blocks/Testimonial/Testimonial.php


declare(strict_types=1);

namespace App\Blocks\Testimonial;

use Itineris\AcfGutenblocks\AbstractBlock;

class Testimonial extends AbstractBlock
{
    public function __construct()
    {
        parent::__construct([
            'title' => __('Testimonial', 'sage'),
            'description' => __('Testimonial description', 'sage'),
            'category' => 'formatting',
            'post_types' => ['post', 'page'],
            // Other valid acf_register_block() settings
        ]);
    }

    /**
     * Make $items available to your template
     */
    public function with(): array
    {
        return [
            'items' => (array) get_field('items'),
        ];
    }
}

add_filter('acf_gutenblocks/blade_engine_callable', function (string $callable): string {
    return '\Roots\view';
});

public function with(): array
{
    return [
        'items' => (array) get_field('items'),
    ];
}

# Blocks/Testimonial/views/frontend.php
 foreach ($items as $item) : 

protected function registerFields(): array
{
    return [
        // Any valid field settings
    ];
}

protected function registerFields(): array
{
    $testimonial = new FieldsBuilder('testimonial');

    $testimonial
        ->setLocation('block', '==', 'acf/testimonial');

    $testimonial
        ->addText('quote')
        ->addText('cite')
        ->addRepeater('list_items')
            ->addText('list_item')
            ->addTrueFalse('enabled', [
                'ui' => 1,
                'default_value' => 1,
            ])
        ->endRepeater();

    return $testimonial->build();
}

  Blocks/
    └── Testimonial/
        ├── views/
        │   └── frontend.php # Block template file
        └── Testimonial.php # Block constructor and controller