From a461c01b97e3ec3cb5731f8db7f5b6933ee7257a Mon Sep 17 00:00:00 2001 From: Patrick de Mooij Date: Sun, 5 Mar 2023 22:52:47 +0100 Subject: [PATCH 1/3] Make sure to set the variationContext for sitemap --- .../Services/IMetaFieldsValueService.cs | 19 ++++++++++++++++--- .../MetaFieldsValueService.cs | 19 ++++++++++--------- .../ExampleCode/ExampleSitemapNotification.cs | 13 +++++++++++-- .../SitemapGenerators/SitemapGenerator.cs | 17 +++++++++++++---- 4 files changed, 50 insertions(+), 18 deletions(-) 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..d9d5e984 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) { - _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..0b2c96f5 100644 --- a/src/SeoToolkit.Umbraco.Site/ExampleCode/ExampleSitemapNotification.cs +++ b/src/SeoToolkit.Umbraco.Site/ExampleCode/ExampleSitemapNotification.cs @@ -1,14 +1,23 @@ -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..0287d4f1 100644 --- a/src/SeoToolkit.Umbraco.Sitemap.Core/Common/SitemapGenerators/SitemapGenerator.cs +++ b/src/SeoToolkit.Umbraco.Sitemap.Core/Common/SitemapGenerators/SitemapGenerator.cs @@ -22,24 +22,28 @@ 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 private XNamespace _namespace => XNamespace.Get("http://www.sitemaps.org/schemas/sitemap/0.9"); - private XNamespace _xHtmlNamespace = XNamespace.Get("http://www.w3.org/1999/xhtml"); + + private XNamespace _xHtmlNamespace = XNamespace.Get("http://www.w3.org/1999/xhtml"); public SitemapGenerator(IUmbracoContextFactory umbracoContextFactory, ISettingsService settingsService, ISitemapService sitemapService, IPublicAccessService publicAccessService, - IEventAggregator eventAggregator) + IEventAggregator eventAggregator, + IVariationContextAccessor variationContextAccessor) { _umbracoContextFactory = umbracoContextFactory; _sitemapService = sitemapService; _publicAccessService = publicAccessService; _eventAggregator = eventAggregator; - _settings = settingsService.GetSettings(); + _variationContextAccessor = variationContextAccessor; + _settings = settingsService.GetSettings(); _pageTypeSettings = new Dictionary(); } @@ -49,7 +53,12 @@ public XDocument Generate(SitemapGeneratorOptions options) _validAlternateCultures = new List(); var rootNamespace = new XElement(_namespace + "urlset", _settings.ShowAlternatePages ? new XAttribute(XNamespace.Xmlns + "xhtml", _xHtmlNamespace) : null); - using (var ctx = _umbracoContextFactory.EnsureUmbracoContext()) + if (!string.IsNullOrWhiteSpace(options.Culture)) + { + _variationContextAccessor.VariationContext = new VariationContext(options.Culture); + } + + using (var ctx = _umbracoContextFactory.EnsureUmbracoContext()) { var startingNodes = new List(); if (options.StartingNode != null) From 346974b652852e63e08b53e0c22144873a5c4794 Mon Sep 17 00:00:00 2001 From: Patrick de Mooij Date: Mon, 6 Mar 2023 17:36:01 +0100 Subject: [PATCH 2/3] Some fixes --- .../MetaFieldsValueService.cs | 2 +- .../ExampleCode/ExampleSitemapNotification.cs | 8 ++++---- .../Common/SitemapGenerators/SitemapGenerator.cs | 13 ++++++------- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/SeoToolkit.Umbraco.MetaFields.Core/Services/MetaFieldsValueService/MetaFieldsValueService.cs b/src/SeoToolkit.Umbraco.MetaFields.Core/Services/MetaFieldsValueService/MetaFieldsValueService.cs index d9d5e984..048559f3 100644 --- a/src/SeoToolkit.Umbraco.MetaFields.Core/Services/MetaFieldsValueService/MetaFieldsValueService.cs +++ b/src/SeoToolkit.Umbraco.MetaFields.Core/Services/MetaFieldsValueService/MetaFieldsValueService.cs @@ -44,7 +44,7 @@ public void AddValues(int nodeId, Dictionary values, string cult ClearCache(nodeId); } - public void Delete(int nodeId, string fieldAlias, string culture) + public void Delete(int nodeId, string fieldAlias, string culture = null) { _repository.Delete(nodeId, fieldAlias, culture.IfNullOrWhiteSpace(GetCulture())); } diff --git a/src/SeoToolkit.Umbraco.Site/ExampleCode/ExampleSitemapNotification.cs b/src/SeoToolkit.Umbraco.Site/ExampleCode/ExampleSitemapNotification.cs index 0b2c96f5..0ad284da 100644 --- a/src/SeoToolkit.Umbraco.Site/ExampleCode/ExampleSitemapNotification.cs +++ b/src/SeoToolkit.Umbraco.Site/ExampleCode/ExampleSitemapNotification.cs @@ -8,9 +8,9 @@ namespace SeoToolkit.Umbraco.Site.ExampleCode { public class ExampleSitemapNotification : INotificationHandler { - private readonly IMetaFieldsValueService _metaFieldsValueService; - - public ExampleSitemapNotification(IMetaFieldsValueService metaFieldsValueService) + private readonly IMetaFieldsValueService _metaFieldsValueService; + + public ExampleSitemapNotification(IMetaFieldsValueService metaFieldsValueService) { _metaFieldsValueService = metaFieldsValueService; } @@ -18,6 +18,6 @@ public ExampleSitemapNotification(IMetaFieldsValueService 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 0287d4f1..b3db1ee4 100644 --- a/src/SeoToolkit.Umbraco.Sitemap.Core/Common/SitemapGenerators/SitemapGenerator.cs +++ b/src/SeoToolkit.Umbraco.Sitemap.Core/Common/SitemapGenerators/SitemapGenerator.cs @@ -28,8 +28,7 @@ public class SitemapGenerator : ISitemapGenerator private Dictionary _pageTypeSettings; //Used to cache the types for the generation private XNamespace _namespace => XNamespace.Get("http://www.sitemaps.org/schemas/sitemap/0.9"); - - private XNamespace _xHtmlNamespace = XNamespace.Get("http://www.w3.org/1999/xhtml"); + private XNamespace _xHtmlNamespace = XNamespace.Get("http://www.w3.org/1999/xhtml"); public SitemapGenerator(IUmbracoContextFactory umbracoContextFactory, ISettingsService settingsService, @@ -42,8 +41,8 @@ public SitemapGenerator(IUmbracoContextFactory umbracoContextFactory, _sitemapService = sitemapService; _publicAccessService = publicAccessService; _eventAggregator = eventAggregator; - _variationContextAccessor = variationContextAccessor; - _settings = settingsService.GetSettings(); + _variationContextAccessor = variationContextAccessor; + _settings = settingsService.GetSettings(); _pageTypeSettings = new Dictionary(); } @@ -56,9 +55,9 @@ public XDocument Generate(SitemapGeneratorOptions options) if (!string.IsNullOrWhiteSpace(options.Culture)) { _variationContextAccessor.VariationContext = new VariationContext(options.Culture); - } - - using (var ctx = _umbracoContextFactory.EnsureUmbracoContext()) + } + + using (var ctx = _umbracoContextFactory.EnsureUmbracoContext()) { var startingNodes = new List(); if (options.StartingNode != null) From 6dd6d751de10d49e352cd0bebb64ad265611b09f Mon Sep 17 00:00:00 2001 From: Patrick de Mooij Date: Mon, 6 Mar 2023 17:37:21 +0100 Subject: [PATCH 3/3] Last fix --- .../ExampleCode/ExampleSitemapNotification.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/SeoToolkit.Umbraco.Site/ExampleCode/ExampleSitemapNotification.cs b/src/SeoToolkit.Umbraco.Site/ExampleCode/ExampleSitemapNotification.cs index 0ad284da..16988f67 100644 --- a/src/SeoToolkit.Umbraco.Site/ExampleCode/ExampleSitemapNotification.cs +++ b/src/SeoToolkit.Umbraco.Site/ExampleCode/ExampleSitemapNotification.cs @@ -12,8 +12,8 @@ public class ExampleSitemapNotification : INotificationHandler