Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add logic for sitemap collections #184

Merged
merged 1 commit into from
Apr 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}