Skip to content

Commit

Permalink
Make sure to set the variationContext for sitemap (#166)
Browse files Browse the repository at this point in the history
* Make sure to set the variationContext for sitemap

* Some fixes

* Last fix
  • Loading branch information
patrickdemooij9 authored Mar 6, 2023
1 parent 4fe9929 commit e2d4911
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,21 @@ namespace SeoToolkit.Umbraco.MetaFields.Core.Interfaces.Services
{
public interface IMetaFieldsValueService
{
Dictionary<string, object> GetUserValues(int nodeId);
void AddValues(int nodeId, Dictionary<string, object> values);
void Delete(int nodeId, string fieldAlias);
/// <summary>
/// Get the values set by the user. If culture is NULL, the variation context culture will be used.
/// </summary>
/// <param name="nodeId"></param>
/// <param name="culture"></param>
/// <returns></returns>
Dictionary<string, object> GetUserValues(int nodeId, string culture = null);

/// <summary>
/// Set the values by the user. If culture is NULL, the variation context culture will be used.
/// </summary>
/// <param name="nodeId"></param>
/// <param name="values"></param>
/// <param name="culture"></param>
void AddValues(int nodeId, Dictionary<string, object> values, string culture = null);
void Delete(int nodeId, string fieldAlias, string culture = null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,30 @@ public MetaFieldsValueService(IMetaFieldsValueRepository repository, IVariationC
_cache = appCaches.RuntimeCache;
}

public Dictionary<string, object> GetUserValues(int nodeId)
public Dictionary<string, object> GetUserValues(int nodeId, string culture = null)
{
var culture = GetCulture();
return _cache.GetCacheItem($"{BaseCacheKey}{nodeId}_{culture}", () => _repository.GetAllValues(nodeId, culture), TimeSpan.FromMinutes(30));
var foundCulture = culture.IfNullOrWhiteSpace(GetCulture());
return _cache.GetCacheItem($"{BaseCacheKey}{nodeId}_{foundCulture}", () => _repository.GetAllValues(nodeId, foundCulture), TimeSpan.FromMinutes(30));
}

public void AddValues(int nodeId, Dictionary<string, object> values)
public void AddValues(int nodeId, Dictionary<string, object> values, string culture = null)
{
var foundCulture = culture.IfNullOrWhiteSpace(GetCulture());
foreach (var (key, value) in values)
{
if (_repository.Exists(nodeId, key, GetCulture()))
_repository.Update(nodeId, key, GetCulture(), value);
if (_repository.Exists(nodeId, key, foundCulture))
_repository.Update(nodeId, key, foundCulture, value);
else
{
_repository.Add(nodeId, key, GetCulture(), value);
_repository.Add(nodeId, key, foundCulture, value);
}
}
ClearCache(nodeId);
}

public void Delete(int nodeId, string fieldAlias)
public void Delete(int nodeId, string fieldAlias, string culture = null)
{
_repository.Delete(nodeId, fieldAlias, GetCulture());
_repository.Delete(nodeId, fieldAlias, culture.IfNullOrWhiteSpace(GetCulture()));
}

private string GetCulture()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
using SeoToolkit.Umbraco.Sitemap.Core.Models.Business;
using SeoToolkit.Umbraco.MetaFields.Core.Interfaces.Services;
using SeoToolkit.Umbraco.Sitemap.Core.Models.Business;
using SeoToolkit.Umbraco.Sitemap.Core.Notifications;
using System.Linq;
using Umbraco.Cms.Core.Events;

namespace SeoToolkit.Umbraco.Site.ExampleCode
{
public class ExampleSitemapNotification : INotificationHandler<GenerateSitemapNotification>
{
private readonly IMetaFieldsValueService _metaFieldsValueService;

public ExampleSitemapNotification(IMetaFieldsValueService metaFieldsValueService)
{
_metaFieldsValueService = metaFieldsValueService;
}

public void Handle(GenerateSitemapNotification notification)
{
notification.Nodes.Add(new SitemapNodeItem("https://google.nl"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class SitemapGenerator : ISitemapGenerator
private readonly IPublicAccessService _publicAccessService;
private readonly IEventAggregator _eventAggregator;
private readonly SitemapConfig _settings;
private readonly IVariationContextAccessor _variationContextAccessor;

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

_pageTypeSettings = new Dictionary<int, SitemapPageSettings>();
Expand All @@ -49,6 +52,11 @@ public XDocument Generate(SitemapGeneratorOptions options)
_validAlternateCultures = new List<string>();
var rootNamespace = new XElement(_namespace + "urlset", _settings.ShowAlternatePages ? new XAttribute(XNamespace.Xmlns + "xhtml", _xHtmlNamespace) : null);

if (!string.IsNullOrWhiteSpace(options.Culture))
{
_variationContextAccessor.VariationContext = new VariationContext(options.Culture);
}

using (var ctx = _umbracoContextFactory.EnsureUmbracoContext())
{
var startingNodes = new List<IPublishedContent>();
Expand Down

0 comments on commit e2d4911

Please sign in to comment.