Sitecore bug 502996 - Checkbox Rendering Parameter is taking value from standard values if not checked
The Problem
In a recent project based on Sitecore 10.1.4, I encountered an intriguing issue with Sitecore’s rendering parameters. Specifically, I was working with a rendering parameter template that included Checkbox fields. The standard values for these fields were set to “checked”
Observed Behavior
When reading these values for a page rendering, I noticed the following behavior:
- Unchecking the Checkbox: The value remained “1”.
- Unchecking the Checkbox and adding another rendering parameter with a value: The checkbox value became null (as expected).
This led me to question why the checkbox value remained “1” when unchecked, unless a value was added to the one of the rendering parameter field.
The Analysis
Upon further investigation and raising a support ticket with Sitecore, it was confirmed that this behavior is indeed a bug in the current Sitecore version. The checkbox rendering parameter appears to read from the standard value if unchecked, which is not the expected behavior.
Sitecore’s Response and Workaround
Sitecore Support provided the following insights and workaround:
Issue Definition: The false value for a boolean rendering parameter is retrieved as null.
Workaround: Consider the parameter value as true if it is present and equals “1”.
Sitecore has acknowledged the issue and registered a bug for the current version. To track the future status of this bug report, you can use reference number 502996.
Conclusion
While this bug is being addressed, the suggested workaround is to treat the checkbox parameter value as true if it is present and equals “1”. This ensures that your rendering logic remains consistent until a permanent fix is released. I created a reusable utility in my solution to fetch checkbox type rendering parameter field values from the query parameter passed by the rendering. This method takes in the field name for the checkbox type rendering parameter field and returns it value -
public static bool GetCheckboxValueFromRawRenderingParameters(string fieldName)
{
var rawRenderingParameters = RenderingContext.Current.Rendering?.Properties?.Where(i => i.Key.Equals("Parameters")).FirstOrDefault().Value?.Split('&');
if (rawRenderingParameters.Any() && rawRenderingParameters.Contains(fieldName))
{
if (rawRenderingParameters.Contains($"{fieldName}=1"))
{
return true;
}
else
{
return false;
}
}
else
{
var standardValue = RenderingContext.Current.Rendering.GetStandardValue();
if (standardValue != null
&& standardValue.Fields[fieldName] != null)
{
return standardValue.Fields[fieldName].Value.ToBool();
}
}
return false;
}
If you encounter similar issues or have further questions, consider reaching out to Sitecore Support for assistance.
Comments
Post a Comment