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

Look for presets in base theme if current preset specified in current theme #389

Merged
merged 13 commits into from
Mar 18, 2020
41 changes: 29 additions & 12 deletions VirtoCommerce.LiquidThemeEngine/ShopifyLiquidThemeEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -358,13 +358,26 @@ public IDictionary<string, object> GetSettings(string defaultValue = null)

JObject result;
var baseThemeSettings = new JObject();
var currentThemeSettings = result = GetCurrentSettingsPreset(InnerGetAllSettings(_themeBlobProvider, CurrentThemeSettingPath));
var allCurrentThemeSettings = InnerGetAllSettings(_themeBlobProvider, CurrentThemeSettingPath);
var currentThemeSettings = result = null;
try
{
currentThemeSettings = result = GetCurrentSettingsPreset(allCurrentThemeSettings);
asvishnyakov marked this conversation as resolved.
Show resolved Hide resolved
}
catch (StorefrontException) when (_options.MergeBaseSettings)
{
// Do not throw exception of missed presets or current preset if we merge settings
}

//Try to load settings from base theme path and merge them with resources for local theme
if ((_options.MergeBaseSettings || currentThemeSettings == null) && !string.IsNullOrEmpty(BaseThemeSettingPath))
{
cacheItem.AddExpirationToken(new CompositeChangeToken(new[] { ThemeEngineCacheRegion.CreateChangeToken(), _themeBlobProvider.Watch(BaseThemeSettingPath) }));
result = baseThemeSettings = GetCurrentSettingsPreset(InnerGetAllSettings(_themeBlobProvider, BaseThemeSettingPath));
var allBaseThemeSettings = InnerGetAllSettings(_themeBlobProvider, BaseThemeSettingPath);
result = baseThemeSettings = GetCurrentSettingsPreset(allBaseThemeSettings,
_options.MergeBaseSettings && currentThemeSettings == null
? allCurrentThemeSettings
: null);
}

if (_options.MergeBaseSettings)
Expand Down Expand Up @@ -480,23 +493,27 @@ private static JObject InnerGetAllSettings(IContentBlobProvider themeBlobProvide
/// Get actual preset from config
/// </summary>
/// <returns></returns>
private static JObject GetCurrentSettingsPreset(JObject allSettings)
private static JObject GetCurrentSettingsPreset(JObject allSettings, JObject baseSettings = null)
{
var result = allSettings;
var currentPreset = allSettings.GetValue("current");
var currentPreset = (baseSettings ?? allSettings).GetValue("current");
if (currentPreset is JValue currentPresetValue)
{
var currentPresetName = currentPresetValue.Value.ToString();
if (!(allSettings.GetValue("presets") is JObject presets) || !presets.Children().Any())
if (!string.IsNullOrEmpty(currentPresetName))
{
throw new StorefrontException("Setting presets not defined");
}
if (!(allSettings.GetValue("presets") is JObject presets) ||
!presets.Children().Any())
{
throw new StorefrontException("Setting presets not defined");
}

IList<JProperty> allPresets = presets.Children().Cast<JProperty>().ToList();
result = allPresets.FirstOrDefault(p => p.Name == currentPresetName)?.Value as JObject;
if (result == null)
{
throw new StorefrontException($"Setting preset with name '{currentPresetName}' not found");
IList<JProperty> allPresets = presets.Children().Cast<JProperty>().ToList();
result = allPresets.FirstOrDefault(p => p.Name == currentPresetName)?.Value as JObject;
if (result == null)
{
throw new StorefrontException($"Setting preset with name '{currentPresetName}' not found");
}
}
}
if (currentPreset is JObject preset)
Expand Down