Blocks and zones for Studio
How a module adds its own block to the Studio block list: the block class, fields for the owner, variants, rendering, data caching and zones above and below the module's page.
By the end your module has a "Top players" block that the site owner finds in the Studio block list, places on any page and configures: title, number of rows, layout. The module's own page also gets two zones, above and below its content, where the owner can put any blocks. How the owner works with blocks is covered in Blocks and sections in Studio.
Minimal example
A block extends Flute\Core\Modules\Sections\Sections\Section. Its key is the owner and a name joined by a dot: example.top.
<?php
declare(strict_types = 1);
namespace Flute\Modules\Example\Studio;
use Flute\Core\Modules\Sections\Fields\Field;
use Flute\Core\Modules\Sections\Sections\DataPolicy;
use Flute\Core\Modules\Sections\Sections\Section;
use Flute\Core\Modules\Sections\Sections\SectionContext;
use Flute\Core\Modules\Sections\Sections\SectionMeta;
use Flute\Modules\Example\Services\Leaderboard;
final class TopSection extends Section
{
public const KEY = 'example.top';
public static function key(): string
{
return self::KEY;
}
public static function meta(): SectionMeta
{
return new SectionMeta(
label: 'example.block.label',
icon: 'sl.linear.cup-star',
category: 'game',
dataPolicy: new DataPolicy(ttl: 60, errorTtl: 30),
);
}
public static function fields(): array
{
return [
Field::text('title')->max(80)->label('example.block.title'),
Field::number('limit')->range(1, 20)->default('5')->label('example.block.limit'),
Field::toggle('avatars')->default('1')->label('example.block.avatars'),
];
}
public static function variants(): array
{
return ['default', 'cards'];
}
public static function variantLabel(string $variant): string
{
return 'example.block.variants.' . $variant;
}
public static function view(): string
{
return 'example::blocks.top';
}
public function data(array $settings, SectionContext $ctx): array
{
return ['rows' => app(Leaderboard::class)->top((int) ( $settings['limit'] ?? 5 ))];
}
public static function placeholderData(array $settings): array
{
return ['rows' => [['name' => 'Player', 'score' => 1200, 'avatar' => null]]];
}
}The view gets whatever data() returned as separate variables ($rows), next to $settings and $variant. The data-fs-slot attributes tell the Studio canvas where the block's content is.
<div class="example-top" data-variant="{{ $variant }}">
@if (($settings['title'] ?? '') !== '')
<h2 class="example-top__title" data-fs-slot="text">{{ $settings['title'] }}</h2>
@endif
<ol class="example-top__list" data-fs-slot="items">
@foreach ($rows as $row)
<li class="example-top__row" data-fs-slot="card">
@if (!empty($settings['avatars']) && $row['avatar'] !== null)
<img src="{{ $row['avatar'] }}" alt="" width="32" height="32" loading="lazy">
@endif
<span class="example-top__name">{{ $row['name'] }}</span>
<span class="example-top__score">{{ $row['score'] }}</span>
</li>
@endforeach
</ol>
</div>Labels of the block, its fields and variants are translation keys in the module's domain: example.block.label, example.block.variants.cards and example.zones live in Resources/lang/en/example.php and Resources/lang/ru/example.php. The block and the zones are registered in the provider's boot():
<?php
declare(strict_types = 1);
namespace Flute\Modules\Example\Providers;
use DI\Container;
use Flute\Core\Modules\Sections\Sections\ModuleBlocks;
use Flute\Core\Modules\Sections\Sections\ModuleZones;
use Flute\Core\Support\ModuleServiceProvider;
use Flute\Modules\Example\Studio\TopSection;
final class ExampleProvider extends ModuleServiceProvider
{
public function register(Container $container): void
{
}
public function boot(Container $container): void
{
$this->bootstrapModule();
$this->loadViews('Resources/views', 'example');
$this->loadScss('Resources/assets/scss/example.scss');
ModuleZones::register('example', 'example.zones');
if (!ModuleBlocks::has(TopSection::KEY)) {
ModuleBlocks::register(TopSection::KEY, TopSection::class);
}
}
}ModuleZones::register('example', ...) declares the zones example.above and example.below. The module's page view prints them itself:
@extends('flute::layouts.app')
@push('content')
<div class="container example-page">
{!! section_zone('example.above') !!}
@include('example::partials.table')
{!! section_zone('example.below') !!}
</div>
@endpushThe News, Stats, Discord and Monitoring modules build their blocks this way; Clans, Forum, Stats and Demos declare zones.
How it works
boot() runs once per process: on every request under plain PHP, once per worker in worker mode. A module provider loads in every context, the console included. ModuleBlocks::register() validates the key and the class and puts the block into SectionRegistry, which page rendering, Studio and the pre-publish check all read.
The Studio block list is built by Studio\Content\BlockCatalog from every registry entry. The group follows category: game goes to "Game", community to "Community", anything else from a module to "From modules". A block with system: true, or whose standalone() returns false, is not listed. The shell category (SectionMeta::SHELL) marks a frame block, offered only in header and footer zones.
The settings panel is built from fields(). SettingsValidator checks what the owner saves: keys missing from fields() are dropped, and values arrive as strings. A number is '5', a toggle '1' or '0', so default() takes a string too.
To render, the core creates the block with new $class() and no arguments, calls shown(), then data() and the view. The view gets $type, $settings, $data, $variant, $children and every key from data(). The result is wrapped in <section class="fs fs-example-top fs-v-cards" data-fs-block="example.top"> with the look attributes the owner picked: data-surface, data-width, data-radius and others. A block without children that renders an empty string does not appear on the published page at all.
Data and caching
With a ttl above zero the result of data() is cached under sections.data.<block key>.<schema version>.<settings hash>.<site context>.<locale>. The key does not depend on the visitor, so data() returns public data only: the same for a guest and an admin. Read anything viewer-specific in the view.
An exception in data() with failSoft: true is logged to the sections channel and the block gets empty data. The failure is remembered for errorTtl seconds so a broken source is not hit on every render. An empty array is not cached unless cacheEmpty: true is set.
In the Studio editor editorMode decides where data comes from: real calls data(), placeholder uses placeholderData(), disabled gives nothing. If a preview has no data, the view also gets placeholderData(), so return the same keys there as in data().
A block with live data can refresh itself. Put data-lx-live="30s" on the root element of the view (10s, 15s, 30s, 60s and 5m are accepted), and the core adds an htmx poll to the wrapper. The Monitoring module's servers block works this way. There is no polling in the editor or for a block outside a published page.
Reference
Section method | Default | What it does |
|---|---|---|
static key(): string | required | the owner.name key, equal to the first argument of register() |
static meta(): SectionMeta | required | label, icon, category, data policy |
static fields(): array | required | the Field list for the settings panel |
static view(): string | required | Blade view name, example::blocks.top |
static variants(): array | ['default'] | layout variants, the value arrives in $variant |
static variantLabel(string $variant): string | sections.catalog.<key>.variants.<variant> | translation key of a variant's name |
data(array $settings, SectionContext $ctx): array | [] | live data |
static placeholderData(array $settings): array | [] | sample for previews |
static shown(array $settings, string $variant): bool | true | false skips the block before data() |
static schemaVersion(): int, static migrateSettings(array $settings, int $fromVersion): array | 1, unchanged | migrate old settings when their format changes |
static axes(): array | all axes | which look settings (width, spacing, surface, radius…) the block offers |
SectionMeta(label, icon, category, dataPolicy, system = false). DataPolicy(editorMode = 'real', ttl = 0, errorTtl = 5, cacheEmpty = false, failSoft = true); for a block without live data use DataPolicy::none().
Fields come from the factories Field::text(), textarea(), richtext(), link(), image(), number(), select(), toggle(), color(), server(), file(), repeater(), icon(), then a chain: ->label($key) (without it, sections.studio.fields.<field key>), ->default($value), ->required(), ->max($n) (string length, or repeater rows with a default of 20), ->range($min, $max), ->options([$value => $labelKey]), ->fields([...]) for repeater rows, ->literal() for a value that is never translated.
| Zones | What it does |
|---|---|
ModuleZones::register(string $owner, string $labelKey) | zones <owner>.above and <owner>.below, Studio group label from $labelKey |
section_zone(string $key): string | the zone's HTML, an empty string for an unregistered key |
Markup for any theme
A block ends up in any theme and any Studio column, so the screen width tells it nothing. Switch layout by the block's own width: container-type: inline-size on the root and @container. Take colours, radii, fonts and shadows from tokens only (--accent, --surface, --border, --radius, --foreground), with no hex values and no [data-theme=light] checks. The token table is in Theme parameters.
.example-top {
container: example-top / inline-size;
}
.example-top__row {
display: flex;
align-items: center;
gap: 12px;
border-radius: var(--radius);
background: var(--surface);
color: var(--foreground);
}
@container example-top (min-width: 40rem) {
.example-top[data-variant='cards'] .example-top__list {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(min(100%, 14rem), 1fr));
}
}When the owner gives the block a surface, the wrapper gets data-surface="soft" or "accent". Inner cards should then use --fs-card-bg and --fs-card-hover under .fs:is([data-surface="soft"], [data-surface="accent"]), or they blend into the surface. Styles live in the module's stylesheet, never in an inline <style>. Check the block in standard with the light and dark scheme and in one dark game theme, at 1440, 840 and 390 wide.
Common mistakes
| Message or symptom | Cause |
|---|---|
Section key 'example.top' is already registered | the block is registered twice in one process: guard register() with ModuleBlocks::has() |
Invalid module block key 'Example.Top' (expected 'owner.name') | the key does not match ^[a-z0-9]+\.[a-z0-9-]+$: capitals, no dot |
Module block 'example.top' does not match Flute\Modules\Example\Studio\TopSection::key() | the first argument of register() differs from the class's key() |
Invalid zone owner 'example-page' (expected lowercase letters and digits) | the zone owner has a hyphen, a dot or a capital letter |
the block vanished from the page, sections.render_failed in the sections log | an exception in the view, or a constructor with arguments: the core builds the block without them, so fetch dependencies with app() inside data() |
| a guest sees another user's data | data() returned personal data, which is cached for everyone and ends up in the anonymous page cache |
| the Studio canvas treats the block as empty although it has data | the view has no data-fs-slot at all |
a database error from fields() | a select field's options() reads a module table, and the block list is read even before that table exists. Catch the database error and return [], as the News module does |