diff --git a/src/Umbraco.Web.UI.Client/examples/workspace-context-initial-name/README.md b/src/Umbraco.Web.UI.Client/examples/workspace-context-initial-name/README.md new file mode 100644 index 000000000000..3c679efbc9a0 --- /dev/null +++ b/src/Umbraco.Web.UI.Client/examples/workspace-context-initial-name/README.md @@ -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. diff --git a/src/Umbraco.Web.UI.Client/examples/workspace-context-initial-name/index.ts b/src/Umbraco.Web.UI.Client/examples/workspace-context-initial-name/index.ts new file mode 100644 index 000000000000..bd81e0ae4ee2 --- /dev/null +++ b/src/Umbraco.Web.UI.Client/examples/workspace-context-initial-name/index.ts @@ -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 = [ + { + 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, + }, + ], + }, +]; diff --git a/src/Umbraco.Web.UI.Client/examples/workspace-context-initial-name/workspace-context.ts b/src/Umbraco.Web.UI.Client/examples/workspace-context-initial-name/workspace-context.ts new file mode 100644 index 000000000000..91e30d19871a --- /dev/null +++ b/src/Umbraco.Web.UI.Client/examples/workspace-context-initial-name/workspace-context.ts @@ -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 };