Front-end Example: Initial name example (#20899)

initial name example
This commit is contained in:
Niels Lyngsø
2025-11-24 13:37:43 +01:00
committed by GitHub
parent 12611942ff
commit ea840bcfde
3 changed files with 52 additions and 0 deletions

View File

@@ -0,0 +1,3 @@
# Workspace Context for setting an Initial Name
This example of a Workspace Context demonstrates how to manipulate the name of the workspaces entity.

View File

@@ -0,0 +1,22 @@
import {
UMB_WORKSPACE_CONDITION_ALIAS,
UMB_WORKSPACE_ENTITY_IS_NEW_CONDITION_ALIAS,
} from '@umbraco-cms/backoffice/workspace';
export const manifests: Array<UmbExtensionManifest> = [
{
type: 'workspaceContext',
name: 'Example Name Manipulation Workspace Context',
alias: 'example.workspaceContext.nameManipulation',
api: () => import('./workspace-context.js'),
conditions: [
{
alias: UMB_WORKSPACE_CONDITION_ALIAS,
match: 'Umb.Workspace.Document',
},
{
alias: UMB_WORKSPACE_ENTITY_IS_NEW_CONDITION_ALIAS,
},
],
},
];

View File

@@ -0,0 +1,27 @@
import { UmbControllerBase } from '@umbraco-cms/backoffice/class-api';
import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
import { UMB_CONTENT_WORKSPACE_CONTEXT } from '@umbraco-cms/backoffice/content';
import { UmbVariantId } from '@umbraco-cms/backoffice/variant';
// The Example Workspace Context Controller:
export class ExampleWorkspaceContextNameManipulation extends UmbControllerBase {
constructor(host: UmbControllerHost) {
super(host);
this.consumeContext(UMB_CONTENT_WORKSPACE_CONTEXT, async (workspace) => {
if (!workspace) return;
await workspace.isLoaded();
// Set the name if it's already empty (We do not want to overwrite if it's a Blueprint)
// Notice we need to provide a Variant-ID to getName, as Document names are variant specific. Here we get the Invariant name — this will need to be extended if you are looking to support multiple variants.
const variantId = UmbVariantId.CreateInvariant();
const name = workspace.getName(variantId);
if (name === undefined) {
const manipulatedName = `New Document - ${new Date().toLocaleDateString('en-GB')}`;
workspace.setName(manipulatedName, variantId);
}
});
}
}
// Declare a api export, so Extension Registry can initialize this class:
export { ExampleWorkspaceContextNameManipulation as api };