numencode / wn-bricks-plugin
Translatable, block-based content builder for Winter CMS static pages: editors compose pages from bricks defined as YAML fields and Twig markup in plugins or themes.
Package info
github.com/numencode/wn-bricks-plugin
Type:winter-plugin
pkg:composer/numencode/wn-bricks-plugin
Requires
- php: ^8.2
- composer/installers: ^1.0 || ^2.0
- numencode/wn-fundamentals-plugin: ^2.1
Requires (Dev)
None
Suggests
- winter/wn-pages-plugin: Enables bricks on static pages.
- winter/wn-translate-plugin: Enables translations of the bricks per locale.
Provides
None
Conflicts
None
Replaces
None
README
The Bricks Plugin is a translatable, block-based content builder for Winter CMS static pages. Editors compose a page from a list of bricks (headings, text, images, galleries, columns, components, …): they add, reorder, duplicate, hide and delete them. Developers define brick types as YAML fields + Twig markup in plugins or themes, and themes can override every brick's markup.
Features
- Bricks on static pages – one tag in a layout turns the page's content into a list of bricks.
- A base set of 11 bricks – heading, text, image, gallery, columns, button, quote, video, divider, spacer and HTML, with plain, semantic markup.
- Your own brick types – a YAML file with standard backend form fields plus a Twig file, in a plugin or a theme.
- Theme overrides – a theme can replace the markup (
partials/bricks/<code>.htm) or the fields of any brick type. - Bricks inside bricks – a brick type can hold other bricks (Columns holds a list of bricks per column), with the same editor, permissions and translations at every level.
- Bricks on any model – a
type: bricksform field stores bricks in a model's attribute, e.g. on blog posts. - Component bricks – CMS components become bricks, with fields generated from their properties. Every Winter.Pages snippet component (e.g. NumenCode.Widgets) is a brick automatically.
- Translations – with Winter.Translate, each language has its own list of bricks, with "copy from" (and machine translation of the marked fields); or, per field, one shared structure that other languages only translate (per field, with fallback and machine translation).
- A comfortable editor – collapsed bricks show their type and a summary of their content; "Expand all" / "Collapse all"; duplicate, hide, and copy and paste between pages and languages; an anchor and a CSS class on every brick; the "Add brick" list in sections (the theme's own bricks first, then the general ones, then each plugin's); "Edit …" links to where a component brick's content is managed; the page preview as its own tab.
- Safe editing – brick types can require permissions (checked on the server, too), and bricks whose type no longer exists keep their content.
- Developer commands –
create:brickscaffolds a brick type or copies one into the theme,bricks:listshows where each type comes from and what it overrides,bricks:validatechecks definitions and markup in CI,bricks:convert-pagesturns existing pages (rich editor content and snippets) into bricks. - English and Slovenian backend translations.
Requirements
- PHP 8.2 or newer
- Winter CMS 1.2 (tested with 1.2.15)
- NumenCode.Fundamentals 2.1 or newer (installed automatically)
- Winter.Pages – bricks live on static pages
- Optional: Winter.Translate for translations
Installation
composer require numencode/wn-bricks-plugin php artisan winter:up
Quick start
Add a bricks field to a layout and render it:
description = "Page built from bricks"
[staticPage]
useContent = 0
==
{variable type="bricks" name="bricks" label="Bricks" tab="Content"}{/variable}
<!DOCTYPE html>
<html>
<body>
<main>
{{ renderBricks() }}
</main>
</body>
</html>
- Winter.Pages only offers layouts that contain the
[staticPage]component. useContent = 0removes the page's rich-editor "Content" field, which a bricks layout usually doesn't need. Leave it out to combine the rich-editor content with bricks.
Pages that still have rich-editor content can fall back to it while they have no bricks:
{% set bricksHtml = renderBricks() %}
{% if bricksHtml|trim %}{{ bricksHtml|raw }}{% else %}{% page %}{% endif %}
The layout tag
{variable type="bricks" name="bricks" label="Bricks" tab="Content" allow="text,image,gallery" ignore="html"}{/variable}
| Option | Description |
|---|---|
name |
The field name; the bricks are stored in the page's viewBag[<name>]. A layout can have several bricks fields (e.g. bricks and aside). |
label, tab, placement |
As for other layout variables. |
prompt |
The text of the "Add brick" button. |
allow |
Only these brick types are offered, in this order (comma separated codes). |
ignore |
These brick types are not offered. |
translation |
With Winter.Translate: lists (default) – every language has its own list of bricks; fields – one list shared by all languages, which only translate its texts. See Choosing a translation mode. |
Bricks of types a field doesn't offer (because of allow/ignore or permissions) keep their content: they can't be
added, but existing ones are shown and saved unchanged.
Editing bricks
- Adding: "Add brick" lists the brick types: the base set and the theme's types first, then each plugin's types under its name (e.g. "NumenCode Widgets"). The list has a search box.
- Collapsed bricks show the type and a summary of their content, e.g. "Text: Our story began in…". "Expand all" and "Collapse all" open or close every brick.
- Actions on each brick: copy, duplicate (the copy appears right after it), hide on the website (the brick is dimmed and marked "Hidden"; it is kept but not rendered), collapse and delete.
- Copy and paste: a copied brick is kept in the browser, and "Paste brick" (next to "Expand all") adds it at the end of any bricks field that offers its type – on another page, in another language or in another browser tab. The × next to it forgets the copied brick.
- Advanced: every brick has an optional anchor (for links like
#contact) and CSS class. - "Edit …" links: a component brick whose content is managed elsewhere links there, e.g. a NumenCode.Widgets brick opens its group's form in a new tab.
- Preview link: the crosshairs button of a brick opens Winter.Pages' preview and scrolls to the brick, which blinks there. Clicking a brick's bar also makes it the one shown when the preview is opened. Edits and reordering refresh the preview.
- Preview tab: with the bricks in the main tabs (NumenCode.Fundamentals'
pages_viewbag_primary_tabs, on by default), Winter.Pages' preview becomes a full-height tab, and the tabs are ordered Content, Preview, Meta, Settings. - Unknown bricks: a brick whose type no longer exists (e.g. a theme definition was removed) is shown as "Unknown brick" with a notice. Saving the page keeps its content unchanged; it isn't rendered. Move or delete it.
Base set
| Brick | Code | Fields |
|---|---|---|
| Heading | heading |
text, level (h2, h3, h4) |
| Text | text |
content (rich editor) |
| Image | image |
image, alt, caption, width (content, wide, full) |
| Gallery | gallery |
images (image, alt, caption), columns (2, 3, 4) |
| Columns | columns |
columns (2, 3), column_1 … column_3 (bricks inside: a list of bricks per column) |
| Button | button |
text, url (translatable, not machine-translated), style (primary, secondary, link), new_window |
| Quote | quote |
quote, author, source |
| Video | video |
url (YouTube, embedded from youtube-nocookie.com, or Vimeo), title |
| Divider | divider |
– |
| Spacer | spacer |
size (small, medium, large) |
| HTML | html |
html (code editor); needs the permission numencode.bricks.use_html |
The markup is plain HTML with BEM-style classes and no CSS framework: brick brick--<code>, elements brick__* and
modifiers such as brick--image-wide. Style it in your theme, or override the markup.
Defining brick types
A brick type is a YAML definition (<code>.yaml) and Twig markup (<code>.htm). The code (the file name) may contain
a-z, 0-9, - and _ and must start with a letter.
# callout.yaml name: Callout # plain text or a lang key description: A highlighted note icon: icon-bullhorn titleFrom: text # the field shown on a collapsed brick (default: the first filled field) fields: # standard backend form fields text: label: Text type: textarea translatable: true # machine-translated with Winter.Translate's "copy from" link: label: Link type: text translatable: true # each language can link to its own page … machineTranslation: false # … but a link is never sent to machine translation style: label: Style type: dropdown options: info: Info warning: Warning
{# callout.htm #} <aside class="callout callout--{{ style }}{% if brick.class %} {{ brick.class }}{% endif %}"{% if brick.anchor %} id="{{ brick.anchor }}"{% endif %}> {{ text|nl2br }} </aside>
| Key | Description |
|---|---|
name |
Required. The name in the editor. |
description |
Shown in the "Add brick" list. |
icon |
An icon class (default icon-cube). |
fields |
Backend form fields. A type without fields (e.g. a divider) only needs markup. |
titleFrom |
The field summarised on a collapsed brick. |
component |
Makes it a component brick (see below). |
permissions |
A permission code or a list; only backend users with one of them may add or change these bricks. |
editUrl |
A backend URL where the brick's content is edited, with {field} placeholders, e.g. acme/shop/products/update/{product}. |
The field names _group, _hidden, _anchor, _class, _id and brick are reserved.
Translatable fields: translatable: true marks the fields other languages translate in a
shared structure; they are also the ones machine-translated (Winter.Translate's
"copy from", Bricks' translate buttons). Add machineTranslation: false to a translatable field that must be
translated by hand only, such as a link, an e-mail address or a code: it is editable per language, but never sent to a
translation provider.
Markup variables: every field by its name, the page's variables, and brick:
| Variable | Description |
|---|---|
brick.code |
The brick type. |
brick.index |
The brick's position in its field (from 0). |
brick.field |
The bricks field's name (e.g. bricks). |
brick.id |
The brick's stable id (a short random string that stays the same when the brick is moved or edited). |
brick.anchor |
The anchor, as a slug ("Our team" → our-team), or empty. |
brick.class |
The CSS classes (letters, digits, - and _), or empty. |
brick.lists |
A container's bricks per bricks field (see Bricks inside bricks). |
All CMS filters and functions work (|media, |resize, |page, |_, …).
Bricks inside bricks
A field type: bricks in a brick type makes it a container: the field is a list of bricks inside the brick, with
allow / ignore like the layout tag:
# tabs.yaml name: Tabs fields: title: label: Title type: text translatable: true items: label: Tabs type: bricks allow: [text, image, gallery]
-
In the markup the field is the rendered bricks (
{{ items|raw }}), andbrick.lists.itemslists them for markup that places them itself: each withhtml,code,id,indexandvalues(the brick's fields).{% for child in brick.lists.items %} <section class="tab" id="tab-{{ child.id }}">{{ child.html }}</section> {% endfor %} -
Hidden bricks inside are skipped; nothing inside a hidden brick is rendered. Component bricks inside work, also their AJAX handlers on static pages (alias e.g.
brick_bricks_2_column_1_0). -
In the editor every list has its own "Add brick" list, duplicate, copy and paste (a brick with its bricks inside), hide and the preview button.
-
Depth: by default 2 levels – a page's bricks and the bricks inside them, but no container inside a container. Change it with
max_depthinconfig/numencode/bricks/config.php(1: no nesting). Bricks stored deeper than allowed keep their content, but it can't be edited. -
Only a brick type's own fields can be
type: bricks, not fields inside a repeater's form (bricks:validatereports it).
In a plugin
public function registerBricks(): array { return [ 'callout' => '$/acme/shop/bricks/callout', // callout.yaml + callout.htm, without the extension ]; }
Brick types can also be registered from code with
BrickRegistry::instance()->registerCallback(fn ($registry) => $registry->registerBricks([...])).
In a theme
themes/<theme>/bricks/<code>.yaml– a new brick type, or the fields of an existing one (the definition replaces it). The theme's new types come first in the "Add brick" list, under the theme's name; a replaced type stays in its section.themes/<theme>/partials/bricks/<code>.htm– the markup of a brick type, plugin types included. It's a regular CMS partial.
Later definitions replace earlier ones with the same code: snippet components, the base set, other plugins, callbacks, the theme.
Console commands
create:brick – scaffold a brick type
php artisan create:brick call-to-action # in the active theme php artisan create:brick call-to-action --plugin=Acme.Shop # in a plugin php artisan create:brick panel --container # a brick holding other bricks php artisan create:brick image --override # copy a brick type into the theme, to change it there php artisan create:brick image --markup # copy only its markup
A new type gets a definition with a title and a text field and matching markup: bricks/<code>.yaml +
partials/bricks/<code>.htm in a theme (in the "Add brick" list right away), or bricks/<code>.yaml + .htm in a
plugin (the command prints the line for registerBricks()); --container gives it a bricks field instead of the
text. --override copies the definition and markup of the brick
type in use into the theme; --markup only the markup. A component brick from a snippet gets a definition with its
component, to override the generated fields.
Options: --theme= (default: the active theme), --name=, --icon=, --force (overwrite existing files).
bricks:list – the brick types and their overrides
php artisan bricks:list [--theme=demo] [--overrides]
Lists every brick type with where it comes from (a plugin, theme <name>, a snippet or a callback), its definition and
markup files, and what it replaces – an earlier definition with the same code, or only its markup (a theme partial).
--overrides shows only the types that replace something.
bricks:validate – check the definitions (for CI)
php artisan bricks:validate [--theme=demo] [--theme-only] [--strict]
Reads every definition separately (also the replaced ones), so one broken file doesn't hide the others, and exits with 1 when there are errors – add it to your CI pipeline or a deploy check.
- Errors (they break the editor or the website): invalid definitions, unknown field types (also in nested forms),
Twig syntax errors and unknown filters, functions or tags in the markup, missing or unregistered components, an
unknown
translationoption of a bricks field in the theme's layouts. - Warnings: lang keys that don't exist, permissions that aren't registered, a component brick's field that isn't one
of its properties or a
titleFromthat isn't a field, theme partials that no brick type uses (partials starting with_are helpers and skipped) or that a component brick ignores.
--theme-only reports only problems in the theme's files; --strict fails on warnings too.
bricks:convert-pages – turn existing pages into bricks
php artisan bricks:convert-pages about /contact --dry-run # by file name or URL; show the bricks, save nothing php artisan bricks:convert-pages --all # all static pages, every language
Converts the content of static pages (Winter.Pages' rich editor) into bricks, in the default language's file and in every translation that has its own content. The top-level parts of the content become:
| Content | Brick |
|---|---|
A snippet (<figure data-snippet … data-property-…>) |
Its component brick, with the properties as values |
<h2>–<h4> with plain text (an id becomes the anchor) |
Heading |
A paragraph or <figure> with only an image from the media library (and a caption) |
Image |
<hr> |
Divider |
| Everything else (paragraphs, lists, tables, headings with links, …) | Text, one per run between the bricks above |
Empty paragraphs between them are dropped. --no-split makes only component and text bricks. Snippets inside other
HTML (e.g. a table) and snippets without a brick type stay in a text brick, with a warning. Layout tables are best
rebuilt by hand, e.g. as Columns bricks.
The page's old content is kept, so nothing is lost and the page still has it if the bricks are removed. Pages that
already have bricks are skipped unless --force is given (which replaces their bricks). The command warns when a
page's layout has no bricks field yet.
Options: --all, --locale= (repeatable; default or a language code), --field= (default bricks), --theme=,
--no-split, --dry-run, --force.
bricks:clear-markup – remove the old content
php artisan bricks:clear-markup --all --dry-run php artisan bricks:clear-markup --all
Once you've checked the converted pages in the browser, this removes their old content. Only files with their own
bricks are cleared, and only when the page's layout has the bricks field (otherwise the layout would still show the
old content). It asks before removing (--force doesn't). Same options as bricks:convert-pages, without --no-split.
Component bricks
A component brick runs a CMS component with the brick's values as its properties. Its fields are generated from the
component's properties (string → text, text → textarea, dropdown, checkbox, set → checkbox list; dropdown options from
the component's get<Property>Options()). The component runs on the page like a Winter.Pages snippet, so onRun() and
AJAX handlers work.
-
Snippets: every component a plugin registers as a Winter.Pages snippet is a brick automatically, with the code
snippet-<snippet code>(e.g.snippet-gallery). Limit or switch this off inconfig/numencode/bricks/config.php:return [ 'snippets' => true, // all snippet components (default) // 'snippets' => [\NumenCode\Widgets\Components\Gallery::class], // only these // 'snippets' => false, // none ];
-
Explicitly, with the
componentkey;fieldsthen changes the generated fields:name: Highlights icon: icon-star component: NumenCode\Widgets\Components\Highlights fields: layout: hidden: true
A component can declare its brick's icon ('icon' => 'icon-star' in componentDetails(); otherwise its plugin's
icon) and where its content is edited (a static bricksEditUrl(): ?string returning a URL pattern like editUrl, or
null for no link, e.g. without permission).
A component brick's markup is the component's own; with an anchor or CSS class it is wrapped in a <div> with them.
Rendering
| Twig | Description |
|---|---|
{{ renderBricks() }} |
The current static page's bricks field. |
{{ renderBricks('aside') }} |
Another bricks field of the page. |
{{ renderBricks(post, 'bricks') }} |
A model's bricks field, in the visitor's language (also with a shared structure, translation: fields). |
{{ renderBricks(list, 'name') }} |
A list of bricks from elsewhere: renderBricks(post.bricks, 'post'). The name keeps its component bricks apart from the page's. |
{{ renderBrick(brick, index, field) }} |
One brick, e.g. in {% for brick in bricks %}{{ renderBrick(brick, loop.index0) }}{% endfor %}. |
{{ url|bricksVideoUrl }} |
The embed URL of a YouTube (youtube-nocookie.com) or Vimeo video, or empty. |
Hidden bricks and bricks of unknown types are not rendered (an unknown type is logged in debug mode).
Component bricks of a static page's fields are added to the page before it runs, so their AJAX handlers work. Component
bricks of other lists (e.g. a model's) are created while rendering: their onRun() runs, but their AJAX handlers can't
be reached.
Bricks on other models
Any model can have bricks fields, e.g. blog posts (NumenCode.BlogExtension
adds them to Winter.Blog posts). Store the bricks in a jsonable text attribute and use type: bricks in the form:
class Project extends Model { public $implement = ['@Winter.Translate.Behaviors.TranslatableModel']; public $translatable = ['title', 'bricks']; // one list of bricks per language (optional) protected $jsonable = ['bricks']; // a (medium)text column }
# fields.yaml tabs: fields: bricks: tab: Content type: bricks allow: text, image, gallery # optional, like the layout tag
{{ renderBricks(project, 'bricks') }}
The field offers the active theme's brick types and works like on static pages: "Add brick", the editor's actions,
permissions checked on save, empty options dropped, bricks of missing types kept. With Winter.Translate, list the
attribute in $translatable to get one list per language with "copy from"; Winter.Pages' preview tab is only on
static pages.
Translations
With Winter.Translate, a bricks field is translated in one of two ways. You choose the mode per bricks field with the
translation option: on the layout tag for static pages, or in fields.yaml for models. A page gets the mode of its
layout's field, so a theme can have one layout of each kind. renderBricks() is the same in both modes.
Choosing a translation mode
Per-language lists (translation="lists", the default) |
Shared structure (translation="fields") |
|
|---|---|---|
| What is translated | Each language has its own list of bricks, a copy of the default language's list. | There is one list of bricks; the other languages translate only its texts. |
| Structure in other languages | Free: add, remove, reorder and configure bricks per language. | Locked: the bricks, their order and their settings come from the default language. |
| Which fields can be translated | Every field (it's a separate list). | Only the fields marked translatable: true in the brick type. |
| A brick added later in the default language | Appears in the other languages only after copying it there (or copying the whole list again). | Appears in every language at once, with the default text until it is translated. |
| An untranslated language | Shows the default language's whole list while its own list is empty. | Shows the default text for each untranslated field. |
| Machine translation (with a provider) | The translatable: true fields, when a list is copied from another language. |
The empty translatable: true fields, or one brick at a time. |
| Stored in the other languages | The whole list (viewBag[bricks]; on models, the translated attribute). |
Only the translated values per brick (viewBag[bricks_translations]; on models, <attribute>_translations). |
| On a model | List the attribute in $translatable. |
Don't list the attribute in $translatable; Bricks stores the translations itself. Render it with renderBricks(post, 'bricks'). |
Use a shared structure when the languages are versions of the same page: the same sections and images in the same order, with translated texts. Editors arrange the page once, and the languages can't drift apart.
Use per-language lists when the languages need different content: a section only for one market, other images
or links, a different order. They are also the only choice for fields whose values differ per language but aren't
marked translatable: true (e.g. a different video per language).
Changing the mode of an existing field (or a page's layout) doesn't convert the stored translations, but doesn't delete them either: both kinds are kept side by side in the other languages, and each mode uses only its own. After switching from lists to fields, the other languages show the default text until they are translated; after switching from fields to lists, they show their old lists (or the default language's list where they have none). Switching back brings the earlier translations back.
Per-language lists (translation: lists)
The field becomes Translate's multilingual repeater:
- Edit the page in the default language.
- Switch the bricks field to another language. While its list is empty, a hint says so: the website shows the default language's bricks there.
- "Copy the … bricks" (or "copy from" in the language menu) copies the list. With a translation provider configured,
the fields marked
translatable: trueare machine-translated. - Edit and save.
The lists are independent after the copy: adding, moving or removing a brick in one language doesn't change the other languages. A component brick's own content (e.g. a widget group) is translated where it is managed.
Shared structure (translation: fields)
One list of bricks for all languages; the other languages translate its translatable: true fields:
{variable type="bricks" name="bricks" tab="Content" translation="fields"}{/variable}
# a model's fields.yaml (the attribute must not be in $translatable; Bricks stores the translations itself) bricks: type: bricks translation: fields
- The bricks are arranged in the default language: adding, moving, hiding and removing bricks, and every value.
- The language buttons above the bricks show another language: the same bricks, locked. Only the fields marked
translatable: truein the brick type can be edited; an empty field shows the default text as a placeholder (or below the field). Other fields are shown read-only. - On the website an untranslated field shows the default language's value (fallback per field), so a new brick appears in every language at once.
- With a Winter.Translate translation provider (DeepL, Google:
winter.translate::providerswith a key), "Translate empty fields from …" machine-translates every empty field, and the translate button of a brick translates that brick again. - NumenCode.Fundamentals' form language selector (on model forms) switches the field too.
- On models, render it with
renderBricks(post, 'bricks'): a plain list (post.bricks) has no translations.
Bricks inside bricks are part of the structure too: locked in other languages, their translatable fields translated
like the others (the translate button of a container translates the bricks inside it as well). Fields marked
translatable: true inside a brick's repeater (e.g. the gallery's alt texts and captions) are translated per item: the
items get an id, so a caption stays with its image when the images are reordered. Collapsed bricks show the default
language's text, greyed out, until they are translated. Saving checks permissions in every language, and keeps the
translations of bricks whose type no longer exists.
Permissions
numencode.bricks.use_html– add and edit HTML bricks.- Any brick type can require permissions (
permissionsin its definition). Users without them don't see the type in "Add brick", and its existing bricks are read-only (they can still be moved, hidden, duplicated and deleted). - The server checks it on save, in every language: a request that adds or changes such a brick without the permission is rejected.
Data format
Bricks are stored in the page's viewBag (the page file in content/static-pages, the other languages in
content/static-pages-<locale>), one entry per brick, with the type in _group and a stable id in _id (assigned when
the page is saved; a duplicated or pasted brick gets a new one):
[viewBag] title = "About" url = "/about" layout = "bricks" bricks[0][text] = "Our story" bricks[0][level] = "h2" bricks[0][_group] = "heading" bricks[0][_id] = "k3v9q0x2ma" bricks[1][content] = "<p>It began in a small workshop…</p>" bricks[1][_anchor] = "story" bricks[1][_group] = "text" bricks[1][_id] = "p8d1w5rj4c" bricks[2][size] = "large" bricks[2][_hidden] = 1 bricks[2][_group] = "spacer" bricks[2][_id] = "z0t7h2nb6e" bricks[3][columns] = "2" bricks[3][column_1][0][content] = "<p>Left</p>" bricks[3][column_1][0][_group] = "text" bricks[3][column_1][0][_id] = "m5c2v8xq1w" bricks[3][_group] = "columns" bricks[3][_id] = "a4n6r1ys9d"
Bricks inside bricks are stored inside their brick (column_1 above); ids are unique in the whole field.
A shared structure (translation: fields) stores only the translated values in the other languages, per brick id
(<name>_translations; on a model, a translated attribute without a column):
[viewBag] bricks_translations[k3v9q0x2ma][text] = "Naša zgodba" bricks_translations[p8d1w5rj4c][content] = "<p>Začelo se je v majhni delavnici …</p>"
The pages stay theme files: deploy them as before. Bricks need no cache of their own: Winter caches the parsed YAML definitions by their modification time.
Changelog
All notable changes are documented in the CHANGELOG.
Contributing
Please refer to the CONTRIBUTING guide for details on contributing to this project.
Security
If you discover a security issue, please email info@numencode.com instead of using the issue tracker.
Author
The NumenCode.Bricks plugin is created and maintained by Blaz Orazem. More NumenCode plugins: numencode.com.
For inquiries, contact: info@numencode.com
License
This project is open-sourced software licensed under the MIT license.