Instantly share code, notes, and snippets.
Created
May 24, 2023 12:18
-
Save warrenbuckley/ad2b9caea6b3ecbb5f8cac99ae0db7bf to your computer and use it in GitHub Desktop.
Umbraco V12: Extending Content Delivery API
This file contains 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 Umbraco.Cms.Core; | |
using Umbraco.Cms.Core.DeliveryApi; | |
using Umbraco.Cms.Core.Models; | |
using Umbraco.Cms.Core.Web; | |
namespace ExampleProject.ContentDeliveryApi | |
{ | |
public class AuthorContentIndexer : IContentIndexHandler | |
{ | |
private readonly IUmbracoContextFactory _umbracoContextFactory; | |
public AuthorContentIndexer(IUmbracoContextFactory umbracoContextFactory) | |
{ | |
_umbracoContextFactory = umbracoContextFactory; | |
} | |
public IEnumerable<IndexField> GetFields() | |
{ | |
return new[] | |
{ | |
new IndexField | |
{ | |
FieldName = "author", | |
FieldType = FieldType.StringSortable, | |
VariesByCulture = false | |
} | |
}; | |
} | |
public IEnumerable<IndexFieldValue> GetFieldValues(IContent content, string? culture) | |
{ | |
var authorId = content.GetValue<GuidUdi>("author"); | |
if (authorId == null) | |
{ | |
return Enumerable.Empty<IndexFieldValue>(); | |
} | |
var ctxRef = _umbracoContextFactory.EnsureUmbracoContext(); | |
if (ctxRef != null) | |
{ | |
var pickedAuthorNode = ctxRef?.UmbracoContext?.Content?.GetById(authorId); | |
if (pickedAuthorNode != null) | |
{ | |
return new[] | |
{ | |
new IndexFieldValue | |
{ | |
FieldName = "author", | |
Values = new object[] { pickedAuthorNode.Name } | |
} | |
}; | |
} | |
} | |
return Enumerable.Empty<IndexFieldValue>(); | |
} | |
} | |
} | |
This file contains 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 Umbraco.Cms.Core.DeliveryApi; | |
namespace ExampleProject.ContentDeliveryApi | |
{ | |
public class AuthorFilter : IFilterHandler | |
{ | |
private const string FilterType = "author:"; | |
public FilterOption BuildFilterOption(string filter) | |
{ | |
var authorFilterValue = filter.Substring(FilterType.Length); | |
return new FilterOption | |
{ | |
FieldName = "author", | |
Operator = FilterOperation.Is, | |
Values = new[] { authorFilterValue } | |
}; | |
} | |
public bool CanHandle(string query) | |
{ | |
return string.IsNullOrWhiteSpace(query) ? false : query.StartsWith(FilterType, StringComparison.OrdinalIgnoreCase); | |
} | |
} | |
} |
This file contains 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 Umbraco.Cms.Core; | |
using Umbraco.Cms.Core.DeliveryApi; | |
namespace ExampleProject.ContentDeliveryApi | |
{ | |
public class AuthorSort : ISortHandler | |
{ | |
private const string SortType = "author:"; | |
public SortOption BuildSortOption(string sort) | |
{ | |
var sortDirection = sort.Substring(SortType.Length); | |
return new SortOption | |
{ | |
Direction = sortDirection.StartsWith("asc", StringComparison.OrdinalIgnoreCase) ? Direction.Ascending : Direction.Descending, | |
FieldName = "author" | |
}; | |
} | |
public bool CanHandle(string query) | |
{ | |
return string.IsNullOrWhiteSpace(query) ? false : query.StartsWith(SortType, StringComparison.OrdinalIgnoreCase); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment