Skip to content

Commit 43cbb9d

Browse files
authored
Merge pull request #12 from contentstack/feat/editable-tags-fn-support
feat: Editable tags function added
2 parents b57ff58 + ef246e6 commit 43cbb9d

3 files changed

Lines changed: 99 additions & 0 deletions

File tree

.talismanrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
fileignoreconfig:
22
- filename: Contentstack.Utils/Models/Options.cs
33
checksum: 3dc51f0de02429ef9a43b66e666ac4dbde41195e245f8ecc0094548ca8603245
4+
- filename: Contentstack.Utils/Utils.cs
5+
checksum: dfcfa04bedf8e5f9e74f68d02a9f8cb4e35de399e272da015453c6bc4c481e3f
46
version: ""

Contentstack.Utils/Interfaces/IEmbeddedObject.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ string Title
2424
}
2525
}
2626

27+
public interface EditableEntry: IEmbeddedEntry
28+
{
29+
30+
string Locale { get; set; }
31+
32+
object this[string key] { get; set; }
33+
}
34+
2735
public interface IEmbeddedAsset: IEmbeddedObject
2836
{
2937
string Title

Contentstack.Utils/Utils.cs

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,5 +193,94 @@ private static IEmbeddedObject findEmbedded(Metadata metadata, List<IEmbeddedObj
193193
}
194194
return null;
195195
}
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+
}
196285
}
197286
}

0 commit comments

Comments
 (0)