4

Sitecore 10.4.

Rendering parameters are only being applied to the primary language (English) and not falling back for secondary languages (ex: es-mx). I'm using a "helper" class to get the rendering params and I tried forcing it to get from the English version but it had no effect

public CssBase(RenderingParameters parameters)
{
    using (new LanguageSwitcher("en"))
    {
        CssStyles = parameters["Css Styles"];
        CssClasses = parameters["Css Classes"];
...

There are many more params but you get the idea. I get the correct values for English pages but not when I'm testing another language.

I'm guessing it may be because I need to get the English version before that point? This is the spot in the view where I get the params and pass them to the class:

...
@model RenderingModel
@{
    var rendering = RenderingContext.Current.Rendering;
    var parameters = rendering.Parameters;
    var css = new CssBase(parameters);
...

But css is coming out with all empty strings.

Even when I try to get the rendering params directly in the view, I still get nothing where there should be something:

...
@model RenderingModel
@{
    var rendering = RenderingContext.Current.Rendering;
    var parameters = rendering.Parameters;
    var cssClasses = parameters["Css Classes"];
...

I checked the Enable Field Level Fallback on the /sitecore/templates/System/Templates/Sections/Layout/Layout/__Final Renderings field.

2 Answers 2

1

Since rendering parameters in Sitecore are stored within the item's layout definition, they don't support the language fallback feature out of the box. To access rendering parameters set in the default language (such as English) when working in other languages, you can transfer the values from the final layout to the shared layout. By doing this, the parameters become accessible across all languages.

One practical way to automate this process is with a PowerShell script. The following example script copies the CssClass rendering parameter, which is configured in the English version, into the shared layout so that other languages can access it as well.

# Load the Sitecore PowerShell Extensions module
Import-Module -Name SitecorePowerShellExtensions

# Define the item path and rendering ID
$itemPath = "/sitecore/content/YourItemPath"
$renderingId = "{Rendering-GUID}"

# Get the item in the English language
$item = Get-Item -Path $itemPath -Language "en"

# Ensure the item exists
if ($item -ne $null) {
    # Get the final layout definition in the English version
    $finalLayout = [Sitecore.Layouts.LayoutDefinition]::Parse($item["__Final Renderings"])

    # Find the rendering by ID and get the CssClass parameter value
    $rendering = $finalLayout.GetRendering($renderingId)
    $cssClassValue = $rendering?.Parameters["CssClass"]

    if ($cssClassValue -ne $null) {
        # Update the shared layout definition with the CssClass parameter
        $sharedLayout = [Sitecore.Layouts.LayoutDefinition]::Parse($item["__Renderings"])
        $sharedRendering = $sharedLayout.GetRendering($renderingId)

        if ($sharedRendering -eq $null) {
            # If rendering doesn't exist in shared layout, create it
            $sharedRendering = New-Object Sitecore.Layouts.RenderingDefinition
            $sharedRendering.UniqueId = $renderingId
            $sharedLayout.InsertRendering($sharedRendering)
        }

        # Set the CssClass parameter value
        $sharedRendering.Parameters["CssClass"] = $cssClassValue

        # Save the shared layout back to the item
        $item.Editing.BeginEdit()
        $item["__Renderings"] = $sharedLayout.ToXml()
        $item.Editing.EndEdit()

        Write-Host "CssClass parameter value copied to shared layout."
    } else {
        Write-Host "CssClass parameter not found in the English final layout."
    }
} else {
    Write-Host "Item not found at the specified path."
}

Another approach could be to customize the fallback Logic in Code. Modify your code to check the current language for rendering parameters and fall back to the primary language if parameters aren’t available.

public string GetRenderingParameterWithFallback(string parameterName, RenderingParameters parameters)
{
    string value = parameters[parameterName];
    if (string.IsNullOrEmpty(value))
    {
        using (new LanguageSwitcher("en"))
        {
            value = parameters[parameterName];
        }
    }
    return value;
}

Hope this helps you!

0

Since rendering parameters inside Sitecore are fetched from the item layout definition, therefore the fallback mechanism would not meet the criteria.

In order to read the rendering parameters set in the primary language(English), in the other languages, you will need to copy the rendering parameter value set in the final layout to the shared layout so that it can be read from other languages.

One of the way is to achieve this is using powershell snippet. In the below code, CssClass rendering parameter value set inside the English language is copied to the shared layout, in order to make this available for other languages.

$item = Get-Item -Path "master:" -ID {F7E2820D-AB92-4CD8-8A2A-48FED1635C36}

$device = Get-LayoutDevice -Default
$finalLayout = Get-Rendering -Item $item -Device $device -FinalLayout -Language "en"

$finalLayout | ForEach-Object {
    $renderingItem = Get-Item -Path "master:" -ID $_.ItemID
    if($renderingItem.Name -eq "Gallery"){
        $renderingParam = Get-RenderingParameter -Rendering $_
        $parameters = [ordered]@{"CssClass"= $renderingParam["CssClass"]}
        Set-RenderingParameter -Rendering $_ -Parameter $parameters | Set-Rendering -Item $item -Device $device
    }
}

Using above way, we can make sure that the rendering parameters set in the primary language are available in the other languages as well.

Once above changes are made, you should be able to read rendering parameters using RenderingContext.Current.Rendering.Parameters in other languages.

Hope this helps!!! Let me know in case you have any queries.

10
  • I manually copied the final rendering value from English to the final rendering value of alternate language version and I'm still not seeing the rendering values properly. Is this different than the suggestion you made? When you reference "final layout", I took it to mean final rendering, please correct me if I am wrong.
    – Michael
    Commented Oct 14 at 20:53
  • yes @Michael by final layout I meant copying to final rendering for the language. Did you removed the language switcher code and cleared the cache as well?
    – ckhanna
    Commented Oct 14 at 21:00
  • @Michael Can you also check inside the value you copied do you see rendering parameter value inside the s:par attribute in the raw value of the final rendering. Let me know in case you have any queries.
    – ckhanna
    Commented Oct 14 at 21:10
  • Yes, I do see that, 1 ex: s:par="Text Color=%7b6AF85472-D6F7-4812-9C7E-C194FE2F2AA2%7d&Text Alignment=%7b42951C7D-6050-4316-B71B-2986E2241653%7d&Css Classes&Margin Style&Column Size=%7b2C38AC36-89FE-445B-9DE8-EB858804371F%7d&Background Color=%7b1D84B801-D392-4086-BB80-4A8E4F308845%7d&Padding Style=%7bE46032FB-25EF-490C-BFAE-46E3479D97C1%7d&Link Hover Color&Link Focus Color&Css Styles&Background Hover Color&Background Image&...
    – Michael
    Commented Oct 15 at 12:06
  • @Michael Can you check if those parameters are shared fields or in the shared layout?
    – ckhanna
    Commented Oct 15 at 12:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.