From what I can tell, it is currently impossible in Umbraco to get the grid content of an ancestor if the child node already has a grid property of the same name. This is because the recursive GetProperty method still sees the empty grid as having a value.
For example, I have a "Page" document type with a grid editor named "sidebarGrid". In the Content section, I create one root "Page" node with several "Page" children. Only the root node has content in the "sidebarGrid" property, while all children leave this property empty.
One would think that the following call would fetch the root node's "sidebarGrid" content for all child pages:
However, this just returns an empty MvcHtmlString, since the recursive GetProperty method still sees the empty JSON in the grid object as having a value.
My question is: is there an existing workaround for this that I'm somehow missing?
My quick and dirty workaround for this is to create an Html extension method that scrubs the grid property's JSON, checking to see if there is any section or row content.
If there isn't a built-in method to accomplish the same thing, can anyone help make this better? My code is making a lot of assumptions about the grid's JSON object that I don't feel comfortable with.
public static MvcHtmlString GetRecursiveGrid(this HtmlHelper html, IPublishedContent content, string propertyAlias, string framework)
{
while (content != null)
{
var serializer = new JavaScriptSerializer();
dynamic grid = serializer.Deserialize<object>(content.GetPropertyValue<string>("sidebar"));
if (grid == null || grid["sections"] == null)
{
content = content.Parent;
continue;
}
var sections = grid["sections"][0];
var rows = sections["rows"];
if (rows == null ||
rows.Length == 0 ||
rows[0]["areas"][0]["controls"] == null ||
rows[0]["areas"][0]["controls"].Length == 0)
{
content = content.Parent;
continue;
}
return html.GetGridHtml(content.GetProperty("sidebar"), "basicstrata");
}
return null;
}
Html Helper to Get Recursive Grid Content
From what I can tell, it is currently impossible in Umbraco to get the grid content of an ancestor if the child node already has a grid property of the same name. This is because the recursive GetProperty method still sees the empty grid as having a value.
For example, I have a "Page" document type with a grid editor named "sidebarGrid". In the Content section, I create one root "Page" node with several "Page" children. Only the root node has content in the "sidebarGrid" property, while all children leave this property empty.
One would think that the following call would fetch the root node's "sidebarGrid" content for all child pages:
However, this just returns an empty MvcHtmlString, since the recursive GetProperty method still sees the empty JSON in the grid object as having a value.
My question is: is there an existing workaround for this that I'm somehow missing?
My quick and dirty workaround for this is to create an Html extension method that scrubs the grid property's JSON, checking to see if there is any section or row content.
If there isn't a built-in method to accomplish the same thing, can anyone help make this better? My code is making a lot of assumptions about the grid's JSON object that I don't feel comfortable with.
is working on a reply...