Created
September 3, 2025 13:05
-
-
Save adnanzameer/e94fb30dc7826c5db389eb12716b03c3 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
| public class PreviewPageHelper( | |
| IContentRepository repo, | |
| IContentVersionRepository versions, | |
| IContextModeResolver modes, | |
| IHttpContextAccessor http) | |
| { | |
| private bool IsDraftView | |
| { | |
| get | |
| { | |
| var ctx = http.HttpContext; | |
| var hasParam = ctx?.Request?.Query.TryGetValue("showdrafts", out var val) == true | |
| && val.ToString().Equals("true", System.StringComparison.OrdinalIgnoreCase); | |
| //var user = ctx?.User; | |
| //var isEditor = user?.IsInRole("CmsEditors") == true || user?.IsInRole("CmsAdmins") == true; | |
| return (hasParam | |
| //&& isEditor | |
| ) | |
| || modes.CurrentMode == ContextMode.Edit | |
| || modes.CurrentMode == ContextMode.Preview; | |
| } | |
| } | |
| public T GetPage<T>(ContentReference pageRef) where T : PageData | |
| { | |
| if (ContentReference.IsNullOrEmpty(pageRef)) return null; | |
| if (IsDraftView) | |
| { | |
| var latest = versions.List(pageRef) | |
| .OrderByDescending(v => v.Saved) | |
| .FirstOrDefault(); | |
| if (latest != null && repo.TryGet<T>(latest.ContentLink, out var latestPage)) | |
| { | |
| return latestPage; | |
| } | |
| } | |
| return repo.TryGet<T>(pageRef, out var publishedPage) ? publishedPage : null; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment