From 2a981d2b301665c187daae6ff39d33bef45d5f47 Mon Sep 17 00:00:00 2001 From: Lee Kelleher Date: Mon, 28 Oct 2024 10:24:50 +0000 Subject: [PATCH] Bugfix: Prevent delete a template when has children (#2479) * Adds "Template Has No Children Condition" to prevent the "delete" action being displayed for templates that have child templates. * Removed condition config type it didn't have any configurable properties. Lazy-loaded the manifest api. * Renamed "Template Has No Children" condition to "Template Allow Delete Action" condition. --- .../conditions/allow-delete/const.ts | 1 + .../conditions/allow-delete/manifest.ts | 8 +++++++ .../template-allow-delete-action.condition.ts | 22 +++++++++++++++++++ .../templates/conditions/manifests.ts | 3 +++ .../templates/entity-actions/manifests.ts | 2 ++ .../templating/templates/manifests.ts | 2 ++ 6 files changed, 38 insertions(+) create mode 100644 src/packages/templating/templates/conditions/allow-delete/const.ts create mode 100644 src/packages/templating/templates/conditions/allow-delete/manifest.ts create mode 100644 src/packages/templating/templates/conditions/allow-delete/template-allow-delete-action.condition.ts create mode 100644 src/packages/templating/templates/conditions/manifests.ts diff --git a/src/packages/templating/templates/conditions/allow-delete/const.ts b/src/packages/templating/templates/conditions/allow-delete/const.ts new file mode 100644 index 0000000000..b986c98f18 --- /dev/null +++ b/src/packages/templating/templates/conditions/allow-delete/const.ts @@ -0,0 +1 @@ +export const UMB_TEMPLATE_ALLOW_DELETE_ACTION_CONDITION_ALIAS = 'Umb.Condition.Template.AllowDeleteAction'; diff --git a/src/packages/templating/templates/conditions/allow-delete/manifest.ts b/src/packages/templating/templates/conditions/allow-delete/manifest.ts new file mode 100644 index 0000000000..37a4793a92 --- /dev/null +++ b/src/packages/templating/templates/conditions/allow-delete/manifest.ts @@ -0,0 +1,8 @@ +import { UMB_TEMPLATE_ALLOW_DELETE_ACTION_CONDITION_ALIAS } from './const.js'; + +export const manifest: UmbExtensionManifest = { + type: 'condition', + name: 'Template Allow Delete Action Condition', + alias: UMB_TEMPLATE_ALLOW_DELETE_ACTION_CONDITION_ALIAS, + api: () => import('./template-allow-delete-action.condition.js'), +}; diff --git a/src/packages/templating/templates/conditions/allow-delete/template-allow-delete-action.condition.ts b/src/packages/templating/templates/conditions/allow-delete/template-allow-delete-action.condition.ts new file mode 100644 index 0000000000..c1550dab72 --- /dev/null +++ b/src/packages/templating/templates/conditions/allow-delete/template-allow-delete-action.condition.ts @@ -0,0 +1,22 @@ +import { UmbConditionBase } from '@umbraco-cms/backoffice/extension-registry'; +import { UMB_TREE_ITEM_CONTEXT } from '@umbraco-cms/backoffice/tree'; +import type { UmbConditionControllerArguments, UmbExtensionCondition } from '@umbraco-cms/backoffice/extension-api'; +import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api'; + +export class UmbTemplateAllowDeleteActionCondition extends UmbConditionBase implements UmbExtensionCondition { + constructor(host: UmbControllerHost, args: UmbConditionControllerArguments) { + super(host, args); + + this.consumeContext(UMB_TREE_ITEM_CONTEXT, (context) => { + this.observe( + context.hasChildren, + (hasChildren) => { + this.permitted = hasChildren === false; + }, + '_templateAllowDeleteActionCondition', + ); + }); + } +} + +export { UmbTemplateAllowDeleteActionCondition as api }; diff --git a/src/packages/templating/templates/conditions/manifests.ts b/src/packages/templating/templates/conditions/manifests.ts new file mode 100644 index 0000000000..731fde0ccd --- /dev/null +++ b/src/packages/templating/templates/conditions/manifests.ts @@ -0,0 +1,3 @@ +import { manifest as templateAllowDeleteActionCondition } from './allow-delete/manifest.js'; + +export const manifests: Array = [templateAllowDeleteActionCondition]; diff --git a/src/packages/templating/templates/entity-actions/manifests.ts b/src/packages/templating/templates/entity-actions/manifests.ts index fb548f4166..7912a433e2 100644 --- a/src/packages/templating/templates/entity-actions/manifests.ts +++ b/src/packages/templating/templates/entity-actions/manifests.ts @@ -1,5 +1,6 @@ import { UMB_TEMPLATE_DETAIL_REPOSITORY_ALIAS, UMB_TEMPLATE_ITEM_REPOSITORY_ALIAS } from '../repository/index.js'; import { UMB_TEMPLATE_ENTITY_TYPE, UMB_TEMPLATE_ROOT_ENTITY_TYPE } from '../entity.js'; +import { UMB_TEMPLATE_ALLOW_DELETE_ACTION_CONDITION_ALIAS } from '../conditions/allow-delete/const.js'; export const manifests: Array = [ { @@ -26,5 +27,6 @@ export const manifests: Array = [ detailRepositoryAlias: UMB_TEMPLATE_DETAIL_REPOSITORY_ALIAS, itemRepositoryAlias: UMB_TEMPLATE_ITEM_REPOSITORY_ALIAS, }, + conditions: [{ alias: UMB_TEMPLATE_ALLOW_DELETE_ACTION_CONDITION_ALIAS }], }, ]; diff --git a/src/packages/templating/templates/manifests.ts b/src/packages/templating/templates/manifests.ts index eb8a2594d5..da057a5daa 100644 --- a/src/packages/templating/templates/manifests.ts +++ b/src/packages/templating/templates/manifests.ts @@ -1,3 +1,4 @@ +import { manifests as conditionsManifests } from './conditions/manifests.js'; import { manifests as entityActionsManifests } from './entity-actions/manifests.js'; import { manifests as menuManifests } from './menu/manifests.js'; import { manifests as modalManifests } from './modals/manifests.js'; @@ -7,6 +8,7 @@ import { manifests as treeManifests } from './tree/manifests.js'; import { manifests as workspaceManifests } from './workspace/manifests.js'; export const manifests: Array = [ + ...conditionsManifests, ...entityActionsManifests, ...menuManifests, ...modalManifests,