Skip to content
Merged
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
@@ -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,
},
],
},
];
Original file line number Diff line number Diff line change
@@ -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 };
Loading