Skip to content

#[AsBlock]

Register a class as a native Gutenberg block.

Signature

php
#[Attribute(Attribute::TARGET_CLASS)]
final readonly class AsBlock
{
    public function __construct(
        public string $name,
        public string $title,
        public string $category = 'widgets',
        public ?string $icon = null,
        public ?string $description = null,
        public array $keywords = [],
        public array $supports = [],
        public ?string $parent = null,
        public array $ancestor = [],
        public bool $interactivity = false,
        public ?string $interactivityNamespace = null,
        public ?string $template = null,
        public ?string $editorScript = null,
        public ?string $editorStyle = null,
        public ?string $style = null,
        public ?string $viewScript = null,
    ) {}

    public function getInteractivityNamespace(): string {}
}

Parameters

ParameterTypeDefaultDescription
namestringBlock name with namespace (required)
titlestringDisplay title (required)
categorystring'widgets'Block category
icon?stringnullDashicon name or SVG
description?stringnullBlock description
keywordsstring[][]Search keywords
supportsarray[]Block supports configuration
parent?stringnullParent block name
ancestorstring[][]Ancestor block names
interactivityboolfalseEnable WordPress Interactivity API
interactivityNamespace?stringBlock nameCustom interactivity namespace
template?stringAuto-resolvedTemplate path
editorScript?stringnullEditor script path
editorStyle?stringnullEditor styles path
style?stringnullFrontend styles path
viewScript?stringnullFrontend script path

Usage

Basic Block

php
<?php

namespace App\Blocks\Alert;

use Studiometa\Foehn\Attributes\AsBlock;
use Studiometa\Foehn\Contracts\BlockInterface;
use Studiometa\Foehn\Contracts\ViewEngineInterface;
use WP_Block;

#[AsBlock(
    name: 'theme/alert',
    title: 'Alert',
    category: 'widgets',
    icon: 'warning',
)]
final readonly class AlertBlock implements BlockInterface
{
    public function __construct(
        private ViewEngineInterface $view,
    ) {}

    public static function attributes(): array
    {
        return [
            'type' => ['type' => 'string', 'default' => 'info'],
            'message' => ['type' => 'string', 'default' => ''],
        ];
    }

    public function compose(array $attributes, string $content, WP_Block $block): array
    {
        return [
            'type' => $attributes['type'],
            'message' => $attributes['message'],
        ];
    }

    public function render(array $attributes, string $content, WP_Block $block): string
    {
        return $this->view->render('blocks/alert', $this->compose($attributes, $content, $block));
    }
}

Interactive Block

php
<?php

namespace App\Blocks\Counter;

use Studiometa\Foehn\Attributes\AsBlock;
use Studiometa\Foehn\Contracts\InteractiveBlockInterface;
use WP_Block;

#[AsBlock(
    name: 'theme/counter',
    title: 'Counter',
    interactivity: true,
    viewScript: 'blocks/counter/view.js',
)]
final readonly class CounterBlock implements InteractiveBlockInterface
{
    public static function attributes(): array
    {
        return [
            'initialCount' => ['type' => 'number', 'default' => 0],
        ];
    }

    public static function initialState(): array
    {
        return ['totalClicks' => 0];
    }

    public function initialContext(array $attributes): array
    {
        return ['count' => $attributes['initialCount']];
    }

    public function compose(array $attributes, string $content, WP_Block $block): array
    {
        return ['context' => $this->initialContext($attributes)];
    }

    public function render(array $attributes, string $content, WP_Block $block): string
    {
        // ...
    }
}

With Supports

php
#[AsBlock(
    name: 'theme/card',
    title: 'Card',
    supports: [
        'align' => ['wide', 'full'],
        'color' => ['background' => true, 'text' => true],
        'spacing' => ['padding' => true],
        'html' => false,
    ],
)]

Required Interfaces

  • Basic blocks: BlockInterface
  • Interactive blocks: InteractiveBlockInterface

Released under the MIT License.