@@ -193,5 +193,94 @@ private static IEmbeddedObject findEmbedded(Metadata metadata, List<IEmbeddedObj
193
193
}
194
194
return null ;
195
195
}
196
+
197
+ public static void addEditableTags ( EditableEntry entry , string contentTypeUid , bool tagsAsObject , string locale = "en-us" )
198
+ {
199
+ if ( entry != null )
200
+ entry [ "$" ] = GetTag ( entry , $ "{ contentTypeUid } .{ entry . Uid } .{ locale } ", tagsAsObject , locale ) ;
201
+ }
202
+
203
+ private static Dictionary < string , object > GetTag ( object content , string prefix , bool tagsAsObject , string locale )
204
+ {
205
+ var tags = new Dictionary < string , object > ( ) ;
206
+ foreach ( var property in ( Dictionary < string , object > ) content )
207
+ {
208
+ var key = property . Key ;
209
+ var value = property . Value ;
210
+
211
+ if ( key == "$" )
212
+ continue ;
213
+
214
+ switch ( value )
215
+ {
216
+ case object obj when obj is object [ ] array :
217
+ for ( int index = 0 ; index < array . Length ; index ++ )
218
+ {
219
+ object objValue = array [ index ] ;
220
+ string childKey = $ "{ key } __{ index } ";
221
+ string parentKey = $ "{ key } __parent";
222
+
223
+ tags [ childKey ] = GetTagsValue ( $ "{ prefix } .{ key } .{ index } ", tagsAsObject ) ;
224
+ tags [ parentKey ] = GetParentTagsValue ( $ "{ prefix } .{ key } ", tagsAsObject ) ;
225
+
226
+ if ( objValue != null &&
227
+ objValue . GetType ( ) . GetProperty ( "_content_type_uid" ) != null &&
228
+ objValue . GetType ( ) . GetProperty ( "Uid" ) != null )
229
+ {
230
+ var typedObj = ( EditableEntry ) objValue ;
231
+ string locale_ = Convert . ToString ( typedObj . GetType ( ) . GetProperty ( "locale" ) . GetValue ( typedObj ) ) ;
232
+ string ctUid = Convert . ToString ( typedObj . GetType ( ) . GetProperty ( "_content_type_uid" ) . GetValue ( typedObj ) ) ;
233
+ string uid = Convert . ToString ( typedObj . GetType ( ) . GetProperty ( "uid" ) . GetValue ( typedObj ) ) ;
234
+ string localeStr = "" ;
235
+ if ( locale_ != null )
236
+ {
237
+ localeStr = locale_ ;
238
+ } else
239
+ {
240
+ localeStr = locale ;
241
+ }
242
+ typedObj [ "$" ] = GetTag ( typedObj , $ "{ ctUid } .{ uid } .{ localeStr } ", tagsAsObject , locale ) ;
243
+ }
244
+ else if ( value is object )
245
+ {
246
+ ( ( EditableEntry ) value ) [ "$" ] = GetTag ( value , $ "{ prefix } .{ key } .{ index } ", tagsAsObject , locale ) ;
247
+ }
248
+ }
249
+ tags [ key ] = GetTagsValue ( $ "{ prefix } .{ key } ", tagsAsObject ) ;
250
+ break ;
251
+ case object obj when obj != null :
252
+ if ( value != null )
253
+ {
254
+ ( ( EditableEntry ) value ) [ "$" ] = GetTag ( value , $ "{ prefix } .{ key } ", tagsAsObject , locale ) ;
255
+ }
256
+ break ;
257
+ }
258
+ }
259
+ return tags ;
260
+ }
261
+
262
+ private static object GetTagsValue ( string dataValue , bool tagsAsObject )
263
+ {
264
+ if ( tagsAsObject )
265
+ {
266
+ return new Dictionary < string , object > { { "data-cslp" , dataValue } } ;
267
+ }
268
+ else
269
+ {
270
+ return $ "data-cslp={ dataValue } ";
271
+ }
272
+ }
273
+
274
+ private static object GetParentTagsValue ( string dataValue , bool tagsAsObject )
275
+ {
276
+ if ( tagsAsObject )
277
+ {
278
+ return new Dictionary < string , object > { { "data-cslp-parent-field" , dataValue } } ;
279
+ }
280
+ else
281
+ {
282
+ return $ "data-cslp-parent-field={ dataValue } ";
283
+ }
284
+ }
196
285
}
197
286
}
0 commit comments