Last active
September 3, 2025 13:09
-
-
Save adnanzameer/74d37ddf3e8d916107acb81fe6030a0c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Linq; | |
| using EPiServer; | |
| using EPiServer.Core; | |
| using EPiServer.DataAbstraction; | |
| using EPiServer.Web; | |
| namespace YourNamespace.Business.Rendering; | |
| public sealed class PreviewContentAreaLoader( | |
| IContentRepository contentRepository, | |
| IContentVersionRepository contentVersionRepository, | |
| IContextModeResolver contextModeResolver) | |
| : IContentAreaLoader | |
| { | |
| public IContentData LoadContent(ContentAreaItem contentAreaItem) | |
| { | |
| if (contentAreaItem == null || ContentReference.IsNullOrEmpty(contentAreaItem.ContentLink)) | |
| return null; | |
| // Draft view if Edit, Preview, or querystring showdrafts=true | |
| var hasQueryFlag = httpContextAccessor.HttpContext?.Request?.Query.TryGetValue("showdrafts", out var val) == true | |
| && val.ToString().Equals("true", StringComparison.OrdinalIgnoreCase); | |
| var isDraftView = mode == ContextMode.Edit | |
| || mode == ContextMode.Preview | |
| || hasQueryFlag; | |
| if (isDraftView) | |
| { | |
| // Prefer the latest saved version (draft if it exists) | |
| var latest = contentVersionRepository.List(contentAreaItem.ContentLink) | |
| .OrderByDescending(v => v.Saved) | |
| .FirstOrDefault(); | |
| if (latest != null && | |
| contentRepository.TryGet<IContentData>(latest.ContentLink, out var latestContent)) | |
| { | |
| return latestContent; | |
| } | |
| } | |
| // View mode or fallback: published | |
| return contentRepository.TryGet<IContentData>(contentAreaItem.ContentLink, out var published) | |
| ? published | |
| : null; | |
| } | |
| [Obsolete("Use LoadContent instead")] | |
| public IContent? Get(ContentAreaItem contentAreaItem) | |
| => LoadContent(contentAreaItem) as IContent; | |
| // Keep simple; if you don’t rely on display options, return null | |
| public DisplayOption LoadDisplayOption(ContentAreaItem contentAreaItem) => null; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment