Skip to content

v1.5.0

Latest

Choose a tag to compare

@Klehm Klehm released this 20 Oct 08:45
· 64 commits to master since this release
c9a2e82

This release lets a block bring its own CSS and JS, groups the add-block picker by category, tells a block view where and in which language it renders, can put the public CSS in a cascade layer, and lets an editor hold a language back on Publish. Everything is opt-in or additive: existing pages render the same.

A block declares the CSS and JS it needs (klehm/content-blocks)

A block type implementing the optional BlockAssetsInterface returns what its content needs:

public function assets(array $data): BlockAssets
{
    return new BlockAssets(
        stylesheets: ['blocks/counter.css'],
        modules: ($data['animate'] ?? true) ? ['blocks/counter.js'] : [],
    );
}

The page links those files only where such a block renders, each once, after the core's stylesheets. Paths go through asset() when the app has framework.assets. entries names bundler entries: with Webpack Encore installed they are linked with their runtime and shared chunks, and another bundler aliases its own EntrypointResolverInterface. In the builder, a block added or edited in place brings its assets into the preview.

With AssetMapper, declare a block's module entrypoint: true in importmap.php and pass it to no importmap() call, so its imports resolve without running it on every page.

→ CSS and JS a block needs

Picker categories

#[AsContentBlock(category: 'media')] puts a block in a group of the add-block picker; priority still orders it within the group. The built-in groups are text, media, layout, actions, advanced, and other for a block without a category. The kit's blocks come grouped.

content_blocks:
    picker:
        categories: { commerce: 'Shop' }   # added, relabelled or reordered; listed ones first
        blocks: { embed: commerce }        # move a block type to another group

A picker whose blocks all fall in one group keeps the flat grid it showed before. An undeclared category is refused at cache:clear.

→ Picker categories

A block view receives block and context

Beside data and block_id, a view template now gets block (id, type, read-only, never the entity) and context, the RenderContext. A view can tell the builder from the public page (context.preview) and knows its language (context.locale).

If you override render/block.html.twig, pass block: block.view, context: block.context in its include().

→ The view template

The public CSS in a cascade layer

content_blocks:
    css_layer: content-blocks

layout, styling and the kit's kit.css are then served inside @layer content-blocks { … }, so any unlayered rule of yours wins over them, whatever its specificity. It is off by default because turning it on also lets your global CSS (img, a, a reset) start winning: check your pages. The builder chrome is never layered.

→ Overriding the package CSS: cascade layers

Hold a language back on Publish (klehm/content-blocks-i18n)

With a translation pending, the builder's Publish and Revert to published open a dialog listing those languages, all ticked. An unticked language keeps its draft, and its public page stays as it is. The page's own draft always goes, so a translation can be held back but never pushed ahead of its source. With nothing pending, both buttons behave as before.

content_blocks_area_publish and content_blocks_area_discard take an optional body {"locales": [...]}; without it they cover every locale. A bundle keeping its own locale drafts implements LocaleDraftsProviderInterface.

→ Choosing languages in the builder

lang on text shown in another language (klehm/content-blocks-i18n)

content_blocks_i18n:
    fallback_lang: true

On a translated page, a field still in the source language, or read from a fallback locale, says its language, so screen readers and search engines do not read French as German (WCAG 3.1.2). A block nobody translated gets lang on its wrapper. In a partly translated block, the view marks each field with the new cb_lang(object, 'field') Twig function, which every kit view now calls. Off by default.

→ Marking text in another language · in a custom block

Fixed

  • Text typed right after adding a block could be lost: clicking the new block loaded its form a second time, which took focus away mid-typing. A click on the block or section the sidebar already shows now loads nothing.

Upgrading

No database migration. The three packages are tagged together. The kit now requires klehm/content-blocks ^1.5; i18n still accepts ^1.2, without the language dialog.

composer update "klehm/content-blocks*"

Details in each package's changelog: core · kit · i18n