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

Make sure to set the variationContext for sitemap #166

Merged
merged 3 commits into from
Mar 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@patrickdemooij9 Should this culture default to null, same as the interface?

Suggested change
public void Delete(int nodeId, string fieldAlias, string culture)
public void Delete(int nodeId, string fieldAlias, string culture = null)

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Not quite sure why it wasn't complaining about it but I changed it to the default null now

{
_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,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<GenerateSitemapNotification>
{
private readonly IMetaFieldsValueService _metaFieldsValueService;

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

public void Handle(GenerateSitemapNotification notification)
{
notification.Nodes.Add(new SitemapNodeItem("https://google.nl"));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> _validAlternateCultures;
private Dictionary<int, SitemapPageSettings> _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<SitemapConfig> 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<int, SitemapPageSettings>();
}
Expand All @@ -49,7 +53,12 @@ public XDocument Generate(SitemapGeneratorOptions options)
_validAlternateCultures = new List<string>();
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<IPublishedContent>();
if (options.StartingNode != null)
Expand Down