Passed
Push — trunk ( 024f41...cfd10b )
by Christian
15:08 queued 13s
created

placeholder.mixin.ts ➔ placeholder   C

Complexity

Conditions 9

Size

Total Lines 48
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 31
dl 0
loc 48
rs 6.6666
c 0
b 0
f 0
1
/**
2
 * @package admin
3
 */
4
5
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
6
7
/* @private */
8
export {};
9
10
/**
11
 * @deprecated tag:v6.6.0 - Will be private
12
 */
13
Shopware.Mixin.register('placeholder', {
14
    methods: {
15
        placeholder<EntityName extends keyof EntitySchema.Entities>(
16
            entity: EntitySchema.Entity<EntityName>,
17
            field: keyof EntitySchema.Entity<EntityName>,
18
            fallbackSnippet: string,
19
        ) {
20
            if (!entity) {
21
                return fallbackSnippet;
22
            }
23
24
            // @ts-expect-error - we know it is a string because we check it beforehand
25
            if (Shopware.Utils.types.isString(entity[field]) && entity[field].length > 0) {
26
                return entity[field];
27
            }
28
29
            // TODO: Refactor with NEXT-3304
30
            // Return the field from parent translation if set
31
            const parentLanguageId = Shopware.Context.api.language ? Shopware.Context.api.language.parentId : null;
32
            // @ts-expect-error - we just check if translations exists
33
            if (parentLanguageId && parentLanguageId.length > 0 && entity.translations) {
34
                // @ts-expect-error - we had checked if translations exists
35
                const translation = (entity.translations as unknown as {
36
                    [key: string]: string,
37
                }[]).find((entry) => {
38
                    return entry.id === `${entity.id}-${parentLanguageId}`;
39
                });
40
41
                // @ts-expect-error - we check if the field exists
42
                if (translation?.[field] && translation[field].length > 0) {
43
                    // @ts-expect-error - we check if the field exists beforehand
44
                    // eslint-disable-next-line @typescript-eslint/no-unsafe-return
45
                    return translation[field];
46
                }
47
            }
48
49
            // @ts-expect-error - we check if the field exists
50
            // Return the field from translated if set
51
            // eslint-disable-next-line @typescript-eslint/no-unsafe-call
52
            if (entity.translated != null && entity.translated.hasOwnProperty(field)) {
53
                // @ts-expect-error - we check if the field exists beforehand
54
                if (entity.translated[field] !== null) {
55
                    // @ts-expect-error - we check if the field exists beforehand
56
                    // eslint-disable-next-line @typescript-eslint/no-unsafe-return
57
                    return entity.translated[field];
58
                }
59
            }
60
61
            // Return the placeholder snippet
62
            return fallbackSnippet;
63
        },
64
    },
65
});
66