A fragment changes a supported widget's presentation while keeping its data flow.
This example gives the existing Site Totals widget a new presentation. It reuses the widget's existing props. You do not need another API endpoint.
Define the fragment metadata
Create Widgets/Fragments/MyCustomTotalsFragmentMeta.php.
<?php
namespace MyCustom\Catalog\Widgets\Fragments;
use Vanilla\Widgets\Fragments\SiteTotalsFragmentMeta;
class MyCustomTotalsFragmentMeta extends SiteTotalsFragmentMeta
{
public const FRAGMENT_TYPE = "MyCustomTotalsFragment";
public static function getFragmentType(): string
{
return self::FRAGMENT_TYPE;
}
public static function getName(): string
{
return "My Custom Site Totals";
}
}
This example inherits the core Site Totals props schema. It changes the fragment type and display name.
If you add new props, define and validate them explicitly. Registering fragment metadata does not itself make the widget fetch new data or expose new administrator controls.
Register it for the widget
Add this to your addon's CatalogContainerRules::configureContainer():
// Imports:
// Vanilla\Widgets\WidgetFragmentService
// Vanilla\Forum\Widgets\SiteTotalsWidget
// Vanilla\Widgets\Fragments\SiteTotalsFragmentMeta
// MyCustom\Catalog\Widgets\Fragments\MyCustomTotalsFragmentMeta
$container
->rule(WidgetFragmentService::class)
->addCall("registerFragmentForWidget", [
SiteTotalsWidget::class,
MyCustomTotalsFragmentMeta::class,
SiteTotalsFragmentMeta::FRAGMENT_TYPE,
]);
The third argument groups your fragment under the core Site Totals fragment type. That allows its presentation to be selected in the same widget fragment group.
Keep the plugin enabled. Metadata registration and frontend fragment discovery are both required.
Add the frontend files
Create these files under src/scripts/widget-fragments:
MyCustomTotalsFragment.injectable.tsx
MyCustomTotalsFragment.template.tsx
MyCustomTotalsFragment.template.css
MyCustomTotalsFragment.previewData.ts
Their shared basename matches the MyCustomTotalsFragment type.
The inspected registry discovers these file patterns, including plugin paths. This is a release-dependent capability. Verify it on your target LTS rather than assuming an older installation has the same discovery behavior.
Expose the props contract
Create MyCustomTotalsFragment.injectable.tsx:
import SiteTotalsFragment from "@vanilla/injectables/SiteTotalsFragment";
const MyCustomTotalsFragment = { ...SiteTotalsFragment };
namespace MyCustomTotalsFragment {
export type Props = SiteTotalsFragment.Props;
}
export default MyCustomTotalsFragment;
The injectable gives template code a supported import and a typed props contract. This example keeps the core props unchanged.
Ordinary plugin source can import internal modules where appropriate. A fragment template should use the injectable interfaces made available to the Widget Builder runtime.
Write the template
Create MyCustomTotalsFragment.template.tsx:
import MyCustomTotals from "@vanilla/injectables/MyCustomTotalsFragment";
import Components from "@vanilla/injectables/Components";
import Utils from "@vanilla/injectables/Utils";
export default function MyCustomTotalsFragment(props: MyCustomTotals.Props) {
return (
<Components.LayoutWidget className="myCustomTotals">
<ul className="myCustomTotals__list">
{props.totals.map((total) => (
<li key={total.recordType}>
<strong>
{total.isCalculating
? "—"
: props.formatNumbers
? Utils.formatNumber(total.count)
: String(total.count)}
</strong>
<span>{total.label}</span>
</li>
))}
</ul>
</Components.LayoutWidget>
);
}
The template uses the provided data. It preserves the calculating state and number-format option.
This compact example omits the core widget's container appearance options and icons. Add those back when the design requires them. A custom template owns the presentation features it chooses to support.
Create MyCustomTotalsFragment.template.css:
.myCustomTotals__list {
display: flex;
flex-wrap: wrap;
gap: 1rem;
list-style: none;
padding: 0;
}
.myCustomTotals__list li {
display: grid;
gap: 0.25rem;
}
Use classes owned by your fragment. Avoid reaching into another component's generated styles.
Add preview data
Create MyCustomTotalsFragment.previewData.ts.
The following abbreviated fixture supplies one preview case:
import type { IFragmentPreviewData } from "@library/utility/fragmentsRegistry";
import type MyCustomTotals from "./MyCustomTotalsFragment.injectable";
const previews: IFragmentPreviewData<MyCustomTotals.Props>[] = [
{
previewDataUUID: "f1fd4697-c47c-4dae-a409-856337458d80",
name: "One total",
description: "A simple preview of the member count.",
data: {
totals: [{
recordType: "user",
label: "Members",
iconName: "meta-users",
count: 250,
isCalculating: false,
isFiltered: false,
}],
formatNumbers: true,
},
},
];
export default previews;
Add cases with several totals, zero counts, long labels and calculating values. Preview data belongs in the plugin. It should not require a live service.
Build and select the fragment
Refresh addon discovery after adding PHP classes. Rebuild frontend assets after adding the fragment files.
In Layout Editor, edit a Site Totals widget. Use its fragment selection control to find My Custom Site Totals.
If you create an editable implementation in Widget Builder, commit/publish that implementation before selecting its active version on the layout. Save the layout and open a page assigned to it.