Skip to content

Instantly share code, notes, and snippets.

@AThraen
Last active November 3, 2023 09:31
Show Gist options
  • Select an option

  • Save AThraen/97c2cfe22fb7185e4f03bc6a48f1bdc3 to your computer and use it in GitHub Desktop.

Select an option

Save AThraen/97c2cfe22fb7185e4f03bc6a48f1bdc3 to your computer and use it in GitHub Desktop.
Updated Suggested Types Rules for CMS 12
using EPiServer.Cms.Shell.UI.Rest;
using EPiServer.Core;
using EPiServer.DataAbstraction;
using EPiServer.ServiceLocation;
using EPiServer;
using System.Collections.Generic;
using CodeArt.Web.Models.Pages;
namespace CodeArt.Web.Business.SuggestedTypes
{
[ServiceConfiguration(typeof(IContentTypeAdvisor))]
public class CustomContentRecommendations : IContentTypeAdvisor
{
public IContentRepository repo { get; set; }
public CustomContentRecommendations(IContentRepository repo)
{
this.repo = repo;
}
//Add custom recommendations according to rules
public IEnumerable<int> GetSuggestions(IContent parent, bool contentFolder, IEnumerable<string> requestedTypes)
{
var startpage=repo.Get<StartPage>(ContentReference.StartPage);
if (startpage.SuggestedTypes != null)
{
foreach(var r in startpage.SuggestedTypes)
{
if(r.ParentContentType == parent.ContentTypeID.ToString() || r.ParentContent.CompareToIgnoreWorkID(parent.ContentLink))
{
if (r.ForThisContentFolder == contentFolder)
{
var types = r.TypesToSuggest.Split(',');
foreach (var t in types)
{
yield return int.Parse(t);
}
}
}
}
}
}
}
}
public class StartPage : ListPage
{
...
//In this example the suggested types rules are added on the Site Settings tab of the StartPage - but it could also be on a custom Settings element.
[Display(Name = "Suggested Types Rules" , GroupName = Globals.TAB_SITESETTINGS, Order = 110)]
public virtual IList<SuggestedTypesBlock> SuggestedTypes { get; set;}
...
}
using System.ComponentModel.DataAnnotations;
using EPiServer.Core;
using EPiServer.DataAbstraction;
using EPiServer.DataAnnotations;
using EPiServer.Shell.ObjectEditing;
using System.Collections.Generic;
using EPiServer.ServiceLocation;
using System.Linq;
namespace CodeArt.Web.Models.Blocks
{
[ContentType(DisplayName = "Suggested Types Rule", GUID = "AE86837F-65BD-4FEA-B007-3B2224DAD53E", Description = "", AvailableInEditMode = false)]
public class SuggestedTypesBlock : BlockData
{
[Display(Order = 10, Name ="Parent Content Type")]
[SelectOne(SelectionFactoryType = typeof(ContentTypesSelectionFactory))]
public virtual string ParentContentType { get; set; }
[Display(Order =20, Name = "Parent Content")]
public virtual ContentReference ParentContent { get; set; }
[Display(Order = 30, Description = "", Name = "In 'For this Content' folder?")]
public virtual bool ForThisContentFolder { get; set; }
[Display(Order = 40, Description = "", Name ="Types to suggest")]
[Required]
[SelectMany(SelectionFactoryType = typeof(ContentTypesSelectionFactory))]
public virtual string TypesToSuggest { get; set; }
}
public class ContentTypesSelectionFactory : ISelectionFactory
{
public IEnumerable<ISelectItem> GetSelections(ExtendedMetadata metadata)
{
var typerepo = ServiceLocator.Current.GetInstance<IContentTypeRepository>();
var lst = typerepo.List().Where(t => t.IsAvailable).Select(t => new SelectItem() { Text = t.LocalizedName ?? t.DisplayName ?? t.Name, Value = t.ID }).ToArray();
return lst;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment