Skip to content

Instantly share code, notes, and snippets.

@hfloyd
Last active May 20, 2021 21:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hfloyd/a6f3772420b7c1386175ae0797812300 to your computer and use it in GitHub Desktop.
Save hfloyd/a6f3772420b7c1386175ae0797812300 to your computer and use it in GitHub Desktop.
Contentment Custom IDataListSource: Umbraco Page Types
namespace YourCode
{
using System;
using System.Collections.Generic;
using System.Linq;
using Umbraco.Community.Contentment.DataEditors;
using Umbraco.Core.Logging;
using Umbraco.Core.PropertyEditors;
using Umbraco.Core.Services;
public class PageTypesDataSource : IDataListSource
{
#region Properties
public string Name => "Page Types";
public string Description => "All Content Types which are not Elements and have Templates";
public string Icon => "icon-blueprint";
public OverlaySize OverlaySize => OverlaySize.Small;
public Dictionary<string, object> DefaultValues => default;
public IEnumerable<ConfigurationField> Fields => default;
#endregion
#region Dependency Injection - Let's get some Umbraco goodies available here!
private readonly IContentTypeService _contentTypeService;
private readonly ILogger _logger;
public PageTypesDataSource(IContentTypeService contentTypeService, ILogger logger)
{
_contentTypeService = contentTypeService;
_logger = logger;
}
#endregion
public IEnumerable<DataListItem> GetItems(Dictionary<string, object> config)
{
var items = new List<DataListItem>();
try
{
foreach (var ct in _contentTypeService.GetAll().Where(n => !n.IsElement).OrderBy(n => n.Name))
{
if (ct.AllowedTemplates.Any())
{
var item = new DataListItem();
item.Name = ct.Name;
item.Value = ct.Alias;
item.Icon = ct.Icon;
item.Description = ct.Description;
items.Add(item);
}
}
}
catch (Exception e)
{
_logger.Error(typeof(PageTypesDataSource), e);
}
return items;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment