Make a custom block editable in the Visual Builder
On this page
Take your own Block Grid block from content-only to fully styled in the Visual Builder: element type, opt-in, wrapped view and a hint.
Goal: a "Quote" block of your own that editors can place, edit in place and style (spacing, background, border, per device) in the Visual Builder, with its accent toggle clickable on the canvas. You write no styling code: the Visual Builder's wrapper applies the styles.
Before you start #
- Visual Builder installed and turned on.
- A Block Grid data type for your pages, here called "Page Grid".
- ModelsBuilder in
SourceCodeManualorSourceCodeAutomode, so models compile with the site. (WithInMemoryAuto, skip the model step and use the generatedQuoteBlockmodel the same way.)
Step by step #
- 1Create the element typeIn Settings › Document Types, create an element type Quote block (alias
quoteBlock) with:- Quote (
quote, Textarea) - Author (
author, Textstring) - Accent (
accent, Toggle)
In Settings › Data types › Page Grid, add Quote block to the allowed blocks. Don't give it a settings type; the opt-in adds one.
- Quote (
- 2Opt the grid in to stylesAdd a composer to the website project:1using ClientCraft.VisualBuilder.Umbraco.Schema;2using Umbraco.Cms.Core.Composing;3using Umbraco.Cms.Core.DependencyInjection;45namespace MySite.VisualBuilder;67public sealed class VisualBuilderSetup : IComposer8{9 public void Compose(IUmbracoBuilder builder) => builder.AddVisualBuilderSite(site => site.Styles("Page Grid"));10}
Start the site once. The Quote block now has the shared Element appearance settings type, and every other block of Page Grid gets its style field too. Look for "Visual Builder site setup" in the log if something is off.
- 3Generate the modelsGenerate ModelsBuilder models (Settings › Models Builder › Generate models) and build the site, so
QuoteBlockexists. - 4Register the tag helpersIn
Views/_ViewImports.cshtml:1@addTagHelper *, ClientCraft.VisualBuilder.Umbraco - 5Write the block viewCreate
Views/Partials/blockgrid/Components/quoteBlock.cshtml:1@inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage<Umbraco.Cms.Core.Models.Blocks.BlockGridItem<QuoteBlock>>2@{3 var quote = Model.Content;4}5<ccvb-block block="Model" tag="figure">6 <blockquote class="quote@(quote.Accent ? " quote--accent" : "")" data-property="accent">7 <p>@quote.Quote</p>8 </blockquote>9 @if (!string.IsNullOrWhiteSpace(quote.Author))10 {11 <figcaption class="quote-author">@quote.Author</figcaption>12 }13</ccvb-block><ccvb-block>renders thefigurewith the styles editors set.data-property="accent"lets editors click the blockquote to get the Accent toggle on the block's bar.- The text fields need no hint: the builder finds them on the page.
- 6Render the styles in the layoutIn your layout's
<head>, once:1<ccvb-assets /> - 7Try itBuild and run the site, open a page in the builder and insert Quote block. Type the quote in place, select the blockquote to flip Accent, then open Style and set padding and a background colour. Switch to Phone and reduce the padding there.
- 8PublishSelect Save and publish and check the public page: the
figurecarries the styles and your markup inside is unchanged.
Result #
Editors place, edit and style the Quote block like any built-in block, per device, and the accent toggle is one click away on the canvas. Your view stays focused on content.