Skip to content

Instantly share code, notes, and snippets.

@ronaldbarendse
Created September 4, 2020 08:14
Show Gist options
  • Save ronaldbarendse/82f49fe19021b2ca34e57df50cf4509e to your computer and use it in GitHub Desktop.
Save ronaldbarendse/82f49fe19021b2ca34e57df50cf4509e to your computer and use it in GitHub Desktop.
Umbraco Forms IPageService implementations
using System.Collections;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.Forms.Core.Services;
using Umbraco.Web;
public class PublishedContentPageService : IPageService
{
protected readonly IUmbracoContextAccessor umbracoContextAccessor;
public PublishedContentPageService(IUmbracoContextAccessor umbracoContextAccessor)
{
this.umbracoContextAccessor = umbracoContextAccessor;
}
public virtual Hashtable GetPageElements() => this.GetPageElements(this.umbracoContextAccessor.UmbracoContext?.PublishedRequest?.PublishedContent);
public virtual Hashtable GetPageElements(int contentId) => this.GetPageElements(this.umbracoContextAccessor.UmbracoContext?.Content?.GetById(contentId));
protected Hashtable GetPageElements(IPublishedContent content)
{
var pageElements = new Hashtable();
if (content != null)
{
pageElements.Add("pageID", content.Id);
pageElements.Add("parentID", content.Parent?.Id ?? -1);
pageElements.Add("pageName", content.Name);
pageElements.Add("nodeType", content.ContentType.Id);
pageElements.Add("nodeTypeAlias", content.ContentType.Alias);
pageElements.Add("writerName", content.WriterName);
pageElements.Add("creatorName", content.CreatorName);
pageElements.Add("createDate", content.CreateDate);
pageElements.Add("updateDate", content.UpdateDate);
pageElements.Add("path", content.Path);
pageElements.Add("splitpath", content.Path.Split(new char[] { ',' }));
if (content.TemplateId is int templateId)
{
pageElements.Add("template", templateId);
}
foreach (var property in content.Properties)
{
if (!pageElements.ContainsKey(property.Alias))
{
pageElements.Add(property.Alias, property.GetSourceValue());
}
}
}
return pageElements;
}
}
using System.Collections;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.Forms.Core.Services;
using Umbraco.Web;
public class PublishedContentPreviewPageService : PublishedContentPageService
{
public PublishedContentPreviewPageService(IUmbracoContextAccessor umbracoContextAccessor)
: base(umbracoContextAccessor)
{ }
public override Hashtable GetPageElements() => this.GetPageElements(this.umbracoContextAccessor.UmbracoContext?.PublishedRequest?.PublishedContent?.Id ?? -1);
public override Hashtable GetPageElements(int contentId) => this.GetPageElements(this.umbracoContextAccessor.UmbracoContext?.Content?.GetById(true, contentId));
}
using Umbraco.Core;
using Umbraco.Core.Composing;
using Umbraco.Forms.Core.Components;
using Umbraco.Forms.Core.Services;
[ComposeAfter(typeof(UmbracoFormsComposer))]
public class UmbracoFormsPageServiceComposer : IUserComposer
{
public void Compose(Composition composition)
{
composition.RegisterUnique<IPageService, PublishedContentPageService>();
// composition.RegisterUnique<IPageService, PublishedContentPreviewPageService>();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment