Feedback
 
Did this article resolve your question/issue?

   

Your feedback is appreciated.

Please tell us how we can make this article more useful. Please provide us a way to contact you, should we need clarification on the feedback provided or if you need further assistance.

Characters Remaining: 1025

 


Article

How to make a Related Media field to roll back to the default culture

« Go Back

Information

 
Article Number000094355
EnvironmentProduct: Progress Sitefinity CMS
Version: All versions
OS: All supported OS versions
Database: All supported Microsoft SQL Server versions
Question/Problem Description

Current setup:
1) Multilingual site
2) Dynamic Module that contains a Related Media field (Images). 
3) The Images uploaded to the system are only in English (or default) culture. 
4) The Dynamic items created in different cultures don't define a Related Media for each culture, but only for the English (or default) culture. So the Related Media field is NULL for all the cultures except for English. 

Purpose:
Have a built-in MVC widget able to show always the English version of the Related Media field regardless the current language in the frontend, 

By default the MVC widget will throw a NullReferenceException when attempting to render in any culture (except for English) since the Related Media field is NULL for all the cultures except for English.

Steps to Reproduce
Clarifying Information
Error MessageException occured while executing the controller.
Defect/Enhancement Number
Cause
This is expected behavior. 

 
Resolution
To overcome this behavior consider the following: 

For the List View template of the MVC widget: 
- Change the culture only to grab the English version of the Related Media field. E.g: 
 
@model Telerik.Sitefinity.Frontend.DynamicContent.Mvc.Models.DynamicContentListViewModel

@using Telerik.Sitefinity.Frontend.DynamicContent.WidgetTemplates.Fields.Helpers;
@using Telerik.Sitefinity;
@using Telerik.Sitefinity.Data.ContentLinks;
@using Telerik.Sitefinity.Frontend.Mvc.Helpers;
@using Telerik.Sitefinity.Frontend.Mvc.Models;
@using Telerik.Sitefinity.Web.DataResolving;
@using Telerik.Sitefinity.Model.ContentLinks;
@using Telerik.Sitefinity.Modules.Pages;

@using System.Globalization;
@using System.Threading;



<div class="@Model.CssClass slides swiper-container">
<div class="swiper-wrapper">



@foreach (var item in Model.Items)
{
var navigateUrl = HyperLinkHelpers.GetDetailPageUrl(item, ViewBag.DetailsPageId, ViewBag.OpenInSamePage, Model.UrlKeyPrefix);

var uic = Thread.CurrentThread.CurrentUICulture; 
var cc = Thread.CurrentThread.CurrentCulture;

Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("en"); 
Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("en");

var the_image = @item.Fields.Background_Image.Fields.MediaUrl;


Thread.CurrentThread.CurrentUICulture = uic; 
Thread.CurrentThread.CurrentCulture = cc;


<div style='background-image:url(@the_image);' class='slide swiper-slide' @Html.InlineEditingAttributes(Model.ProviderName, Model.ContentType.FullName, (Guid)item.Fields.Id)>

...
...
...
...


}


</div>

</div>


For the Detail View template (MVC):
- The approach followed above doesn't apply for the Details View. It is possible to get the different versions of the items from the List of items however once the Item is fetched (which is what happens in the Detail view) it doesn't make sense to switch the current cultures, since there is only one current item in one culture which Related Media field is not defined, it is NULL.

So the approach in this case is to  use the ContentLinksManager to get the Image of the Original item. E.g: 
 
@model Telerik.Sitefinity.Frontend.Mvc.Models.ContentDetailsViewModel 

@using System.Collections; 
@using System.Linq; 
@using Telerik.Sitefinity.Frontend.DynamicContent.WidgetTemplates.Fields.Helpers; 
@using Telerik.Sitefinity; 
@using Telerik.Sitefinity.Data.ContentLinks; 
@using Telerik.Sitefinity.Frontend.Mvc.Helpers; 
@using Telerik.Sitefinity.Libraries.Model; 
@using Telerik.Sitefinity.Localization; 
@using Telerik.Sitefinity.Model; 
@using Telerik.Sitefinity.Model.ContentLinks; 
@using Telerik.Sitefinity.Modules.Pages; 
@using Telerik.Sitefinity.Pages.Model; 

@{var contentLinkManager = Telerik.Sitefinity.Data.ContentLinks.ContentLinksManager.GetManager(); 
var the_image = contentLinkManager.GetContentLink(Model.Item.Fields.OriginalContentId, "Telerik.Sitefinity.DynamicTypes.Model.Slides.Slide", "Background_Image").ChildItemAdditionalInfo; 
the_image = the_image.Remove(the_image.IndexOf(".tmb-"),5); 
}


<div style='background-image:url(@the_image);' class="slide swiper-slide" @Html.InlineEditingAttributes(Model.ProviderName, Model.ContentType.FullName, (Guid)Model.Item.Fields.Id)> 

...
...
...



</div> 

</div>














 
Workaround
Notes
Attachment 
Last Modified Date2/11/2019 4:33 PM