Skip to content

Commit 283e6b2

Browse files
Entity type + Entity Unique conditions (#19614)
* Add entity-type and entity-unique condition support Introduces new condition types for entity-type and entity-unique, including their constants, types, condition implementations, and manifests. Updates exports in core entity modules to include these new features, enabling more granular extension conditions based on entity type and uniqueness. * register conditions * add support for oneOf * fix self imports * Update manifests.ts * remove unused
1 parent cfbbfa0 commit 283e6b2

File tree

12 files changed

+133
-1
lines changed

12 files changed

+133
-1
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
export * from './contexts/ancestors/constants.js';
22
export * from './contexts/parent/constants.js';
3+
export * from './entity-type/constants.js';
4+
export * from './entity-unique/constants.js';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const UMB_ENTITY_TYPE_CONDITION_ALIAS = 'Umb.Condition.Entity.Type';
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { UMB_ENTITY_CONTEXT } from '../entity.context-token.js';
2+
import type { UmbEntityTypeConditionConfig } from './types.js';
3+
import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
4+
import type { UmbConditionControllerArguments, UmbExtensionCondition } from '@umbraco-cms/backoffice/extension-api';
5+
import { UmbConditionBase } from '@umbraco-cms/backoffice/extension-registry';
6+
7+
export class UmbEntityTypeCondition
8+
extends UmbConditionBase<UmbEntityTypeConditionConfig>
9+
implements UmbExtensionCondition
10+
{
11+
constructor(host: UmbControllerHost, args: UmbConditionControllerArguments<UmbEntityTypeConditionConfig>) {
12+
super(host, args);
13+
14+
this.consumeContext(UMB_ENTITY_CONTEXT, (context) => {
15+
this.observe(context?.entityType, (entityType) => this.#check(entityType), 'umbEntityTypeObserver');
16+
});
17+
}
18+
19+
#check(value: string | undefined) {
20+
if (!value) {
21+
this.permitted = false;
22+
return;
23+
}
24+
25+
// if the config has a match, we only check that
26+
if (this.config.match) {
27+
this.permitted = value === this.config.match;
28+
return;
29+
}
30+
31+
this.permitted = this.config.oneOf?.some((configValue) => configValue === value) ?? false;
32+
}
33+
}
34+
35+
export { UmbEntityTypeCondition as api };
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { UMB_ENTITY_TYPE_CONDITION_ALIAS } from './constants.js';
2+
3+
export const manifests: Array<UmbExtensionManifest> = [
4+
{
5+
type: 'condition',
6+
name: 'Umbraco Entity Type Condition',
7+
alias: UMB_ENTITY_TYPE_CONDITION_ALIAS,
8+
api: () => import('./entity-type.condition.js'),
9+
},
10+
];
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import type { UMB_ENTITY_TYPE_CONDITION_ALIAS } from './constants.js';
2+
import type { UmbConditionConfigBase } from '@umbraco-cms/backoffice/extension-api';
3+
4+
export type UmbEntityTypeConditionConfig = UmbConditionConfigBase<typeof UMB_ENTITY_TYPE_CONDITION_ALIAS> & {
5+
match: string;
6+
oneOf?: Array<string>;
7+
};
8+
9+
declare global {
10+
interface UmbExtensionConditionConfigMap {
11+
UmbEntityTypeConditionConfig: UmbEntityTypeConditionConfig;
12+
}
13+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const UMB_ENTITY_UNIQUE_CONDITION_ALIAS = 'Umb.Condition.Entity.Unique';
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { UMB_ENTITY_CONTEXT } from '../entity.context-token.js';
2+
import type { UmbEntityUnique } from '../types.js';
3+
import type { UmbEntityUniqueConditionConfig } from './types.js';
4+
import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
5+
import type { UmbConditionControllerArguments, UmbExtensionCondition } from '@umbraco-cms/backoffice/extension-api';
6+
import { UmbConditionBase } from '@umbraco-cms/backoffice/extension-registry';
7+
8+
export class UmbEntityUniqueCondition
9+
extends UmbConditionBase<UmbEntityUniqueConditionConfig>
10+
implements UmbExtensionCondition
11+
{
12+
constructor(host: UmbControllerHost, args: UmbConditionControllerArguments<UmbEntityUniqueConditionConfig>) {
13+
super(host, args);
14+
15+
this.consumeContext(UMB_ENTITY_CONTEXT, (context) => {
16+
this.observe(context?.unique, (unique) => this.#check(unique), 'umbEntityUniqueObserver');
17+
});
18+
}
19+
20+
#check(value: UmbEntityUnique | undefined) {
21+
if (value === undefined) {
22+
this.permitted = false;
23+
return;
24+
}
25+
26+
// if the config has a match, we only check that
27+
if (this.config.match !== undefined) {
28+
this.permitted = value === this.config.match;
29+
return;
30+
}
31+
32+
this.permitted = this.config.oneOf?.some((configValue) => configValue === value) ?? false;
33+
}
34+
}
35+
36+
export { UmbEntityUniqueCondition as api };
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { UMB_ENTITY_UNIQUE_CONDITION_ALIAS } from './constants.js';
2+
3+
export const manifests: Array<UmbExtensionManifest> = [
4+
{
5+
type: 'condition',
6+
name: 'Umbraco Entity Unique Condition',
7+
alias: UMB_ENTITY_UNIQUE_CONDITION_ALIAS,
8+
api: () => import('./entity-unique.condition.js'),
9+
},
10+
];
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import type { UMB_ENTITY_UNIQUE_CONDITION_ALIAS } from './constants.js';
2+
import type { UmbConditionConfigBase } from '@umbraco-cms/backoffice/extension-api';
3+
4+
export type UmbEntityUnique = string | null;
5+
6+
export type UmbEntityUniqueConditionConfig = UmbConditionConfigBase<typeof UMB_ENTITY_UNIQUE_CONDITION_ALIAS> & {
7+
match?: UmbEntityUnique;
8+
oneOf?: Array<UmbEntityUnique>;
9+
};
10+
11+
declare global {
12+
interface UmbExtensionConditionConfigMap {
13+
UmbEntityUniqueConditionConfig: UmbEntityUniqueConditionConfig;
14+
}
15+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { manifests as entityTypeManifests } from './entity-type/manifests.js';
2+
import { manifests as entityUniqueManifests } from './entity-unique/manifests.js';
3+
4+
export const manifests: Array<UmbExtensionManifest> = [...entityTypeManifests, ...entityUniqueManifests];

0 commit comments

Comments
 (0)