@@ -8,27 +8,91 @@ export function addTags(entry: EntryModel, contentTypeUid: string, tagsAsObject:
88function getTag ( content : object , prefix : string , tagsAsObject : boolean , locale : string ) : object {
99 const tags : any = { }
1010 Object . entries ( content ) . forEach ( ( [ key , value ] ) => {
11+ if ( key === '$' ) return
12+
1113 switch ( typeof value ) {
1214 case "object" :
1315 if ( Array . isArray ( value ) ) {
1416 value . forEach ( ( obj , index ) => {
17+ const childKey = `${ key } __${ index } `
18+ const parentKey = `${ key } __parent`
19+ /**
20+ * Indexes of array are handled here
21+ * {
22+ * "array": ["hello", "world"],
23+ * "$": {
24+ * "array": {"data-cslp": "content_type_uid.entry_uid.locale.array"}
25+ * "array__0": {"data-cslp": "content_type_uid.entry_uid.locale.array.0"}
26+ * "array__1": {"data-cslp": "content_type_uid.entry_uid.locale.array.1"}
27+ * }
28+ * }
29+ */
30+ tags [ childKey ] = getTagsValue ( `${ prefix } .${ key } .${ index } ` , tagsAsObject )
31+ tags [ parentKey ] = getParentTagsValue ( `${ prefix } .${ key } ` , tagsAsObject )
1532 if ( typeof obj !== 'undefined' && obj !== null && obj . _content_type_uid !== undefined && obj . uid !== undefined ) {
33+ /**
34+ * References are handled here
35+ * {
36+ * "reference": [{
37+ * "title": "title",
38+ * "uid": "ref_uid",
39+ * "_content_type_uid": "ref_content_type_uid",
40+ * "$": {"title": {"data-cslp": "ref_content_type_uid.ref_uid.locale.title"}}
41+ * }]
42+ * }
43+ */
1644 value [ index ] . $ = getTag ( obj , `${ obj . _content_type_uid } .${ obj . uid } .${ obj . locale || locale } ` , tagsAsObject , locale )
17- } else {
18- if ( typeof obj === "object" ) {
19- obj . $ = getTag ( obj , `${ prefix } .${ key } .${ index } ` , tagsAsObject , locale )
20- } else {
21- tags [ key ] = getTagsValue ( `${ prefix } .${ key } ` , tagsAsObject )
22- }
45+ } else if ( typeof obj === "object" ) {
46+ /**
47+ * Objects inside Array like modular blocks are handled here
48+ * {
49+ * "array": [{
50+ * "title": "title",
51+ * "$": {"title": {"data-cslp": "content_type_uid.entry_uid.locale.array.0.title"}}
52+ * }]
53+ * }
54+ */
55+ obj . $ = getTag ( obj , `${ prefix } .${ key } .${ index } ` , tagsAsObject , locale )
2356 }
2457 } )
2558 } else {
2659 if ( value ) {
60+ /**
61+ * Objects are handled here
62+ * {
63+ * "object": {
64+ * "title": "title",
65+ * "$": {
66+ * "title": {"data-cslp": "content_type_uid.entry_uid.locale.object.title"}
67+ * }
68+ * },
69+ * }
70+ */
2771 value . $ = getTag ( value , `${ prefix } .${ key } ` , tagsAsObject , locale )
2872 }
2973 }
74+ /**
75+ * {
76+ * "object": {
77+ * "title": "title",
78+ * },
79+ * "array": ["hello", "world"]
80+ * "$": {
81+ * "object": {"data-cslp": "content_type_uid.entry_uid.locale.object"},
82+ * "array": {"data-cslp": "content_type_uid.entry_uid.locale.array"}
83+ * }
84+ * }
85+ */
86+ tags [ key ] = getTagsValue ( `${ prefix } .${ key } ` , tagsAsObject )
3087 break ;
3188 default :
89+ /**
90+ * All primitive values are handled here
91+ * {
92+ * "title": "title",
93+ * "$": {title: {"data-cslp": "content_type_uid.entry_uid.locale.title"}}
94+ * }
95+ */
3296 tags [ key ] = getTagsValue ( `${ prefix } .${ key } ` , tagsAsObject )
3397 }
3498 } )
@@ -41,4 +105,12 @@ function getTagsValue (dataValue:string, tagsAsObject: boolean): any {
41105 } else {
42106 return `data-cslp=${ dataValue } ` ;
43107 }
108+ }
109+
110+ function getParentTagsValue ( dataValue :string , tagsAsObject : boolean ) : any {
111+ if ( tagsAsObject ) {
112+ return { "data-cslp-parent-field" : dataValue } ;
113+ } else {
114+ return `data-cslp-parent-field=${ dataValue } ` ;
115+ }
44116}
0 commit comments