Skip to content

Commit ea840bc

Browse files
authored
Front-end Example: Initial name example (#20899)
initial name example
1 parent 1261194 commit ea840bc

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Workspace Context for setting an Initial Name
2+
3+
This example of a Workspace Context demonstrates how to manipulate the name of the workspaces entity.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import {
2+
UMB_WORKSPACE_CONDITION_ALIAS,
3+
UMB_WORKSPACE_ENTITY_IS_NEW_CONDITION_ALIAS,
4+
} from '@umbraco-cms/backoffice/workspace';
5+
6+
export const manifests: Array<UmbExtensionManifest> = [
7+
{
8+
type: 'workspaceContext',
9+
name: 'Example Name Manipulation Workspace Context',
10+
alias: 'example.workspaceContext.nameManipulation',
11+
api: () => import('./workspace-context.js'),
12+
conditions: [
13+
{
14+
alias: UMB_WORKSPACE_CONDITION_ALIAS,
15+
match: 'Umb.Workspace.Document',
16+
},
17+
{
18+
alias: UMB_WORKSPACE_ENTITY_IS_NEW_CONDITION_ALIAS,
19+
},
20+
],
21+
},
22+
];
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { UmbControllerBase } from '@umbraco-cms/backoffice/class-api';
2+
import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
3+
import { UMB_CONTENT_WORKSPACE_CONTEXT } from '@umbraco-cms/backoffice/content';
4+
import { UmbVariantId } from '@umbraco-cms/backoffice/variant';
5+
6+
// The Example Workspace Context Controller:
7+
export class ExampleWorkspaceContextNameManipulation extends UmbControllerBase {
8+
constructor(host: UmbControllerHost) {
9+
super(host);
10+
11+
this.consumeContext(UMB_CONTENT_WORKSPACE_CONTEXT, async (workspace) => {
12+
if (!workspace) return;
13+
await workspace.isLoaded();
14+
// Set the name if it's already empty (We do not want to overwrite if it's a Blueprint)
15+
// 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.
16+
const variantId = UmbVariantId.CreateInvariant();
17+
const name = workspace.getName(variantId);
18+
if (name === undefined) {
19+
const manipulatedName = `New Document - ${new Date().toLocaleDateString('en-GB')}`;
20+
workspace.setName(manipulatedName, variantId);
21+
}
22+
});
23+
}
24+
}
25+
26+
// Declare a api export, so Extension Registry can initialize this class:
27+
export { ExampleWorkspaceContextNameManipulation as api };

0 commit comments

Comments
 (0)