arnaudbagnis / acf-gutenberg-custom-block
Lumberjack Framework 的 Acf Gutenberg 自定义块
dev-master
2023-08-30 19:05 UTC
Requires
- php: >=7.3||>=8.0
- rareloop/lumberjack-core: >=5
This package is auto-updated.
Last update: 2024-09-30 01:30:35 UTC
README
此软件包通过 Lumberjack 主题和 ACF Pro 插件 的帮助提供了自定义块创建功能。它包括一个 Provider 和一个 AbstractBlock 类,允许您快速轻松地创建自定义块。
安装
要使用此软件包,您需要在项目中安装并激活 ACF Pro。您可以通过运行以下命令使用 composer 安装此软件包
composer require arnaudbagnis/acf-gutenberg-custom-block
用法
通过扩展此软件包提供的 AbstractBlock 类,在主题的块目录中创建一个新的块。以下是一个示例
<?php
namespace App\Guttenberg\Block;
use Timber\Timber;
class MyCustomBlock extends AbstractBlock
{
public static function getName(): string
{
return "my-custom-block";
}
public static function getTitle(): string
{
return "My Custom Block";
}
public static function getDescription(): string
{
return "This is a description";
}
public static function getCategory(): string
{
return "formatting";
}
public static function getIcon(): string
{
return "admin-comments";
}
public static function getKeywords(): array
{
return array('my-custom-block', 'quote');
}
public static function getExample(): array
{
return array();
}
public static function getEnqueueAssets()
{
// Add a style/js for only this block
return null;
}
public static function getLocalFieldGroup(): array
{
// Add a php field acf block for stay in memories
return [];
}
public static function getController($block)
{
$context = Timber::context();
// retrieve an ACF field here
$context['settings'] = [
'title' => get_field('title'),
'description' => get_field('description'),
'image_right' => get_field('image_right'),
'cta' => get_field('cta'),
'explanations' => get_field('explanations'),
];
Timber::render('templates/blocks/' . static::getName() . '.twig', $context);
}
}
在主题的配置目录中创建一个 block.php 文件,并将以下代码添加到其中
<?php
use App\Guttenberg\Block\MyCustomBlock;
return [
'styles' => [
'./assets/dist/main.css'
],
'blocks' => [
MyCustomBlock::class,
]
];
styles 数组允许您向 Gutenberg 编辑器添加全局样式,而 blocks 数组允许您添加您创建的块。
就这样!您自定义的块现在可以在 Gutenberg 编辑器中使用。