Skip to content

Commit bea12b7

Browse files
authored
UFM: ContentName, adds support for Media Picker (#17635)
1 parent 334cb47 commit bea12b7

File tree

1 file changed

+48
-22
lines changed

1 file changed

+48
-22
lines changed

src/Umbraco.Web.UI.Client/src/packages/ufm/components/content-name/content-name.element.ts

Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { UmbUfmElementBase } from '../ufm-element-base.js';
22
import { UMB_UFM_RENDER_CONTEXT } from '../ufm-render/ufm-render.context.js';
33
import { customElement, property } from '@umbraco-cms/backoffice/external/lit';
4-
import { UmbDocumentItemRepository } from '@umbraco-cms/backoffice/document';
5-
import { UmbMediaItemRepository } from '@umbraco-cms/backoffice/media';
6-
import { UmbMemberItemRepository } from '@umbraco-cms/backoffice/member';
4+
import { UmbDocumentItemRepository, UMB_DOCUMENT_ENTITY_TYPE } from '@umbraco-cms/backoffice/document';
5+
import { UmbId } from '@umbraco-cms/backoffice/id';
6+
import { UmbMediaItemRepository, UMB_MEDIA_ENTITY_TYPE } from '@umbraco-cms/backoffice/media';
7+
import { UmbMemberItemRepository, UMB_MEMBER_ENTITY_TYPE } from '@umbraco-cms/backoffice/member';
78

89
const elementName = 'ufm-content-name';
910

@@ -25,39 +26,64 @@ export class UmbUfmContentNameElement extends UmbUfmElementBase {
2526
async (value) => {
2627
const temp =
2728
this.alias && typeof value === 'object'
28-
? ((value as Record<string, unknown>)[this.alias] as string)
29-
: (value as string);
30-
31-
const entityType = Array.isArray(temp) && temp.length > 0 ? temp[0].type : null;
32-
const uniques = Array.isArray(temp) ? temp.map((x) => x.unique) : temp ? [temp] : [];
33-
34-
if (uniques?.length) {
35-
const repository = this.#getRepository(entityType);
36-
if (repository) {
37-
const { data } = await repository.requestItems(uniques);
38-
this.value = data ? data.map((item) => item.name).join(', ') : '';
39-
return;
40-
}
41-
}
42-
43-
this.value = '';
29+
? (value as Record<string, unknown>)[this.alias]
30+
: (value as unknown);
31+
32+
if (!temp) return;
33+
34+
const entityType = this.#getEntityType(temp);
35+
const uniques = this.#getUniques(temp);
36+
37+
this.value = await this.#getNames(entityType, uniques);
4438
},
4539
'observeValue',
4640
);
4741
});
4842
}
4943

44+
#getEntityType(value: unknown) {
45+
if (Array.isArray(value) && value.length > 0) {
46+
const item = value[0];
47+
if (item.type) return item.type;
48+
if (item.mediaKey) return UMB_MEDIA_ENTITY_TYPE;
49+
}
50+
51+
return null;
52+
}
53+
54+
#getUniques(value: unknown) {
55+
if (Array.isArray(value)) {
56+
return value.map((x) => x.unique ?? x.mediaKey ?? x).filter((x) => UmbId.validate(x));
57+
}
58+
59+
return typeof value === 'string' && UmbId.validate(value) ? [value] : [];
60+
}
61+
62+
async #getNames(entityType: string, uniques?: Array<string>) {
63+
if (uniques?.length) {
64+
const repository = this.#getRepository(entityType);
65+
if (repository) {
66+
const { data } = await repository.requestItems(uniques);
67+
if (Array.isArray(data) && data.length > 0) {
68+
return data.map((item) => item.name).join(', ');
69+
}
70+
}
71+
}
72+
73+
return '';
74+
}
75+
5076
#getRepository(entityType?: string | null) {
5177
switch (entityType) {
52-
case 'media':
78+
case UMB_MEDIA_ENTITY_TYPE:
5379
if (!this.#mediaRepository) this.#mediaRepository = new UmbMediaItemRepository(this);
5480
return this.#mediaRepository;
5581

56-
case 'member':
82+
case UMB_MEMBER_ENTITY_TYPE:
5783
if (!this.#memberRepository) this.#memberRepository = new UmbMemberItemRepository(this);
5884
return this.#memberRepository;
5985

60-
case 'document':
86+
case UMB_DOCUMENT_ENTITY_TYPE:
6187
default:
6288
if (!this.#documentRepository) this.#documentRepository = new UmbDocumentItemRepository(this);
6389
return this.#documentRepository;

0 commit comments

Comments
 (0)