diff --git a/src/SeoToolkit.Umbraco.MetaFields.Core/Interfaces/Services/IMetaFieldsValueService.cs b/src/SeoToolkit.Umbraco.MetaFields.Core/Interfaces/Services/IMetaFieldsValueService.cs index 43450e8e..14b22cd2 100644 --- a/src/SeoToolkit.Umbraco.MetaFields.Core/Interfaces/Services/IMetaFieldsValueService.cs +++ b/src/SeoToolkit.Umbraco.MetaFields.Core/Interfaces/Services/IMetaFieldsValueService.cs @@ -6,8 +6,21 @@ namespace SeoToolkit.Umbraco.MetaFields.Core.Interfaces.Services { public interface IMetaFieldsValueService { - Dictionary GetUserValues(int nodeId); - void AddValues(int nodeId, Dictionary values); - void Delete(int nodeId, string fieldAlias); + /// + /// Get the values set by the user. If culture is NULL, the variation context culture will be used. + /// + /// + /// + /// + Dictionary GetUserValues(int nodeId, string culture = null); + + /// + /// Set the values by the user. If culture is NULL, the variation context culture will be used. + /// + /// + /// + /// + void AddValues(int nodeId, Dictionary values, string culture = null); + void Delete(int nodeId, string fieldAlias, string culture = null); } } diff --git a/src/SeoToolkit.Umbraco.MetaFields.Core/Services/MetaFieldsValueService/MetaFieldsValueService.cs b/src/SeoToolkit.Umbraco.MetaFields.Core/Services/MetaFieldsValueService/MetaFieldsValueService.cs index 5e33e0a4..048559f3 100644 --- a/src/SeoToolkit.Umbraco.MetaFields.Core/Services/MetaFieldsValueService/MetaFieldsValueService.cs +++ b/src/SeoToolkit.Umbraco.MetaFields.Core/Services/MetaFieldsValueService/MetaFieldsValueService.cs @@ -23,29 +23,30 @@ public MetaFieldsValueService(IMetaFieldsValueRepository repository, IVariationC _cache = appCaches.RuntimeCache; } - public Dictionary GetUserValues(int nodeId) + public Dictionary 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 values) + public void AddValues(int nodeId, Dictionary 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() diff --git a/src/SeoToolkit.Umbraco.Site/ExampleCode/ExampleSitemapNotification.cs b/src/SeoToolkit.Umbraco.Site/ExampleCode/ExampleSitemapNotification.cs index 17f4532d..16988f67 100644 --- a/src/SeoToolkit.Umbraco.Site/ExampleCode/ExampleSitemapNotification.cs +++ b/src/SeoToolkit.Umbraco.Site/ExampleCode/ExampleSitemapNotification.cs @@ -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 { + private readonly IMetaFieldsValueService _metaFieldsValueService; + + public ExampleSitemapNotification(IMetaFieldsValueService metaFieldsValueService) + { + _metaFieldsValueService = metaFieldsValueService; + } + public void Handle(GenerateSitemapNotification notification) { notification.Nodes.Add(new SitemapNodeItem("https://google.nl")); diff --git a/src/SeoToolkit.Umbraco.Sitemap.Core/Common/SitemapGenerators/SitemapGenerator.cs b/src/SeoToolkit.Umbraco.Sitemap.Core/Common/SitemapGenerators/SitemapGenerator.cs index fb923677..b3db1ee4 100644 --- a/src/SeoToolkit.Umbraco.Sitemap.Core/Common/SitemapGenerators/SitemapGenerator.cs +++ b/src/SeoToolkit.Umbraco.Sitemap.Core/Common/SitemapGenerators/SitemapGenerator.cs @@ -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 _validAlternateCultures; private Dictionary _pageTypeSettings; //Used to cache the types for the generation @@ -33,12 +34,14 @@ public SitemapGenerator(IUmbracoContextFactory umbracoContextFactory, ISettingsService 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(); @@ -49,6 +52,11 @@ public XDocument Generate(SitemapGeneratorOptions options) _validAlternateCultures = new List(); 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();