Last active
January 17, 2020 15:56
-
-
Save joostbroekhuizen/8ee387678ef7ac2e702f461d4af664ba to your computer and use it in GitHub Desktop.
Custom Sitecore JSS Layout Processor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Extends the LayoutService context with localized errors. | |
/// </summary> | |
public class ErrorsLayoutServiceContextProcessor : JssGetLayoutServiceContextProcessor | |
{ | |
public ErrorsLayoutServiceContextProcessor(IConfigurationResolver configurationResolver) : base(configurationResolver) | |
{ | |
} | |
protected override void DoProcess(GetLayoutServiceContextArgs args, AppConfiguration application) | |
{ | |
if(args.RenderedItem == null || application == null) | |
{ | |
return; | |
} | |
var errorItems = Sitecore.Context.Database?.GetItem(application.Attributes["generalErrorsRoot"])?.Children; | |
if (errorItems == null) | |
{ | |
return; | |
} | |
var errors = new Dictionary<string, ErrorDTO>(); | |
foreach (Item item in errorItems) | |
{ | |
var code = item.Fields[Templates.Error.Fields.Code]?.Value; | |
if (string.IsNullOrEmpty(code)) | |
{ | |
continue; | |
} | |
errors.Add(code, new ErrorDTO() | |
{ | |
Description = item.Fields[Templates.Error.Fields.Description]?.Value, | |
Message = item.Fields[Templates.Error.Fields.Message]?.Value, | |
Type = item.Fields[Templates.Error.Fields.Type]?.Value | |
}); | |
} | |
args.ContextData.Add("errors", errors); | |
} | |
private class ErrorDTO | |
{ | |
public string Description { get; set; } | |
public string Message { get; set; } | |
public string Type { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment