1. Go to this page and download the library: Download 40q/acf-gutenberg 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 / acf-gutenberg example snippets
namespace App\Fields;
use AcfGutenberg\Field;
use StoutLogic\AcfBuilder\FieldsBuilder;
class Example extends Field
{
/**
* The field group.
*
* @return array
*/
public function fields()
{
$example = new FieldsBuilder('example');
$example
->setLocation('post_type', '==', 'post');
$example
->addRepeater('items')
->addText('item')
->endRepeater();
return $example->build();
}
}
namespace App\Fields\Partials;
use AcfGutenberg\Partial;
use StoutLogic\AcfBuilder\FieldsBuilder;
class ListItems extends Partial
{
/**
* The partial field group.
*
* @return array
*/
public function fields()
{
$listItems = new FieldsBuilder('listItems');
$listItems
->addRepeater('items')
->addText('item')
->endRepeater();
return $listItems;
}
}
namespace App\Fields;
use AcfGutenberg\Field;
use StoutLogic\AcfBuilder\FieldsBuilder;
use App\Fields\Partials\ListItems;
class Example extends Field
{
/**
* The field group.
*
* @return array
*/
public function fields()
{
$example = new FieldsBuilder('example');
$example
->setLocation('post_type', '==', 'post');
$example
->addFields($this->get(ListItems::class));
return $example->build();
}
}
namespace App\Blocks;
use AcfGutenberg\Block;
use StoutLogic\AcfBuilder\FieldsBuilder;
class Example extends Block
{
/**
* The block name.
*
* @var string
*/
public $name = 'Example';
/**
* The block description.
*
* @var string
*/
public $description = 'Lorem ipsum...';
/**
* The block category.
*
* @var string
*/
public $category = 'common';
/**
* The block icon.
*
* @var string|array
*/
public $icon = 'star-half';
/**
* Data to be passed to the block before rendering.
*
* @return array
*/
public function with()
{
return [
'items' => $this->items(),
];
}
/**
* The block field group.
*
* @return array
*/
public function fields()
{
$example = new FieldsBuilder('example');
$example
->addRepeater('items')
->addText('item')
->endRepeater();
return $example->build();
}
/**
* Return the items field.
*
* @return array
*/
public function items()
{
return get_field('items') ?: [];
}
}
namespace App\Widgets;
use AcfGutenberg\Widget;
use StoutLogic\AcfBuilder\FieldsBuilder;
class Example extends Widget
{
/**
* The widget name.
*
* @var string
*/
public $name = 'Example';
/**
* The widget description.
*
* @var string
*/
public $description = 'Lorem ipsum...';
/**
* Data to be passed to the widget before rendering.
*
* @return array
*/
public function with()
{
return [
'items' => $this->items(),
];
}
/**
* The widget title.
*
* @return string
*/
public function title() {
return get_field('title', $this->widget->id);
}
/**
* The widget field group.
*
* @return array
*/
public function fields()
{
$example = new FieldsBuilder('example');
$example
->addText('title');
$example
->addRepeater('items')
->addText('item')
->endRepeater();
return $example->build();
}
/**
* Return the items field.
*
* @return array
*/
public function items()
{
return get_field('items', $this->widget->id) ?: [];
}
}
namespace App\Options;
use AcfGutenberg\Options as Field;
use StoutLogic\AcfBuilder\FieldsBuilder;
class Example extends Field
{
/**
* The option page menu name.
*
* @var string
*/
public $name = 'Example';
/**
* The option page document title.
*
* @var string
*/
public $title = 'Example | Options';
/**
* The option page field group.
*
* @return array
*/
public function fields()
{
$example = new FieldsBuilder('example');
$example
->addRepeater('items')
->addText('item')
->endRepeater();
return $example->build();
}
}