Skip to content

Commit

Permalink
Add elements ids to nested List elements (#26965)
Browse files Browse the repository at this point in the history
* Add element ids to nested list elements

* ensure we don't drop other fields in items when enhancing elements
  • Loading branch information
alinaboghiu authored Mar 13, 2024
1 parent 13bf14f commit 0e91890
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion common/app/model/dotcomrendering/ElementsEnhancer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,20 @@ object ElementsEnhancer {
// Also look for "03feb394-a17d-4430-8384-edd1891e0d01"

def enhanceElement(element: JsValue): JsValue = {
element.as[JsObject] ++ Json.obj("elementId" -> java.util.UUID.randomUUID.toString)
val elementWithId = element.as[JsObject] ++ Json.obj("elementId" -> java.util.UUID.randomUUID.toString)
val elementType = elementWithId.value("_type").as[String]
val elementIsList = elementType == "model.dotcomrendering.pageElements.ListBlockElement"

if (elementIsList) {
val listItems = elementWithId.value("items").as[JsArray]
val listItemsWithIds = listItems.value.map { item =>
val obj = item.as[JsObject]
obj ++ Json.obj("elements" -> enhanceElements(obj.value("elements")))
}
elementWithId ++ Json.obj("items" -> listItemsWithIds)
} else {
elementWithId
}
}

def enhanceElements(elements: JsValue): IndexedSeq[JsValue] = {
Expand Down

0 comments on commit 0e91890

Please sign in to comment.