Skip to content

Commit

Permalink
Add logic for sitemap collections (#184)
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickdemooij9 authored Apr 27, 2023
1 parent 6308cc1 commit f2df0d8
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using SeoToolkit.Umbraco.Sitemap.Core.Interfaces;
using SeoToolkit.Umbraco.Sitemap.Core.Models.Business;

namespace SeoToolkit.Umbraco.Site.ExampleCode
{
public class ExampleSitemapCollection : ISitemapCollectionProvider
{
public SitemapNodeItem[] GetItems()
{
return new SitemapNodeItem[]
{
new SitemapNodeItem("https://google.nl?test=true")
};
}
}
}
14 changes: 14 additions & 0 deletions src/SeoToolkit.Umbraco.Site/ExampleCode/StartupComposer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using SeoToolkit.Umbraco.Sitemap.Core.Collections;
using Umbraco.Cms.Core.Composing;
using Umbraco.Cms.Core.DependencyInjection;

namespace SeoToolkit.Umbraco.Site.ExampleCode
{
public class StartupComposer : IComposer
{
public void Compose(IUmbracoBuilder builder)
{
builder.SitemapCollections().Append<ExampleSitemapCollection>();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using SeoToolkit.Umbraco.Sitemap.Core.Interfaces;
using System;
using System.Collections.Generic;
using Umbraco.Cms.Core.Composing;

namespace SeoToolkit.Umbraco.Sitemap.Core.Collections
{
public class SitemapCollectionProviderCollection : BuilderCollectionBase<ISitemapCollectionProvider>
{
public SitemapCollectionProviderCollection(Func<IEnumerable<ISitemapCollectionProvider>> items) : base(items)
{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using SeoToolkit.Umbraco.Sitemap.Core.Interfaces;
using Umbraco.Cms.Core.Composing;

namespace SeoToolkit.Umbraco.Sitemap.Core.Collections
{
public class SitemapCollectionProviderCollectionBuilder : OrderedCollectionBuilderBase<SitemapCollectionProviderCollectionBuilder, SitemapCollectionProviderCollection, ISitemapCollectionProvider>
{
protected override SitemapCollectionProviderCollectionBuilder This => this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Umbraco.Cms.Core.DependencyInjection;

namespace SeoToolkit.Umbraco.Sitemap.Core.Collections
{
public static class SitmapCollectionProviderExtensions
{
public static SitemapCollectionProviderCollectionBuilder SitemapCollections(this IUmbracoBuilder builder)
=> builder.WithCollectionBuilder<SitemapCollectionProviderCollectionBuilder>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
using SeoToolkit.Umbraco.Sitemap.Core.Services.SitemapService;
using Umbraco.Cms.Core.Events;
using Umbraco.Cms.Core.Services;
using SeoToolkit.Umbraco.Sitemap.Core.Collections;
using SeoToolkit.Umbraco.Sitemap.Core.Interfaces;

namespace SeoToolkit.Umbraco.Sitemap.Core.Common.SitemapGenerators
{
Expand All @@ -23,6 +25,7 @@ public class SitemapGenerator : ISitemapGenerator
private readonly IEventAggregator _eventAggregator;
private readonly SitemapConfig _settings;
private readonly IVariationContextAccessor _variationContextAccessor;
private readonly SitemapCollectionProviderCollection _sitemapCollectionProviders;

private List<string> _validAlternateCultures;
private Dictionary<int, SitemapPageSettings> _pageTypeSettings; //Used to cache the types for the generation
Expand All @@ -35,14 +38,16 @@ public SitemapGenerator(IUmbracoContextFactory umbracoContextFactory,
ISitemapService sitemapService,
IPublicAccessService publicAccessService,
IEventAggregator eventAggregator,
IVariationContextAccessor variationContextAccessor)
IVariationContextAccessor variationContextAccessor,
SitemapCollectionProviderCollection sitemapCollectionProviders = null)
{
_umbracoContextFactory = umbracoContextFactory;
_sitemapService = sitemapService;
_publicAccessService = publicAccessService;
_eventAggregator = eventAggregator;
_variationContextAccessor = variationContextAccessor;
_settings = settingsService.GetSettings();
_sitemapCollectionProviders = sitemapCollectionProviders;

_pageTypeSettings = new Dictionary<int, SitemapPageSettings>();
}
Expand Down Expand Up @@ -77,6 +82,14 @@ public XDocument Generate(SitemapGeneratorOptions options)

_eventAggregator.Publish(new GenerateSitemapNotification(items));

if (_sitemapCollectionProviders != null)
{
foreach (var collection in _sitemapCollectionProviders)
{
items.AddRange(collection.GetItems());
}
}

rootNamespace.Add(ToXmlElements(items));

if (_settings.ShowAlternatePages)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using SeoToolkit.Umbraco.Sitemap.Core.Models.Business;

namespace SeoToolkit.Umbraco.Sitemap.Core.Interfaces
{
public interface ISitemapCollectionProvider
{
SitemapNodeItem[] GetItems();
}
}

0 comments on commit f2df0d8

Please sign in to comment.