Last active
November 8, 2018 12:48
-
-
Save aweber1/fa707595482ba7cbe03ab2107d80ad6a to your computer and use it in GitHub Desktop.
JSS TP4 ComponentName workaround
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
using Sitecore.JavaScriptServices.ViewEngine.LayoutService.Serialization; | |
using Sitecore.LayoutService.ItemRendering; | |
using RenderedJsonRendering = Sitecore.JavaScriptServices.ViewEngine.ItemRendering.RenderedJsonRendering; | |
namespace MyNamespace.JavaScriptServices.ViewEngine.LayoutService.Serialization | |
{ | |
public class CustomPlaceholderTransformer : PlaceholderTransformer | |
{ | |
public override object TransformPlaceholderElement(RenderedPlaceholderElement element) | |
{ | |
var transformedElement = (dynamic)base.TransformPlaceholderElement(element); | |
// This check is a bit redundant because the base method does the same check, but the type cast is necessary. | |
if (!(element is RenderedJsonRendering renderingElement)) | |
{ | |
return element; | |
} | |
// This is the important part, changing the erroneous base `componentName` property to the value | |
// provided in the `Component Name` field. | |
transformedElement.componentName = renderingElement.ComponentName; | |
return transformedElement; | |
} | |
} | |
} |
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
// Make sure this assembly is registered with the container _after_ Sitecore.JavaScriptServices.ViewEngine.dll | |
// e.g. | |
/* | |
<services> | |
... | |
<configurator type="Sitecore.JavaScriptServices.ViewEngine.RegisterDependencies, Sitecore.JavaScriptServices.ViewEngine" /> | |
<configurator type="MyNamespace.JavaScriptServices.ViewEngine.RegisterDependencies, MyNamespace.JavaScriptServices.ViewEngine" /> | |
... | |
</services> | |
*/ | |
using Microsoft.Extensions.DependencyInjection; | |
using Sitecore.DependencyInjection; | |
using Sitecore.JavaScriptServices.ViewEngine.LayoutService; | |
using MyNamespace.JavaScriptServices.ViewEngine.LayoutService.Serialization; | |
namespace MyNamespace.JavaScriptServices.ViewEngine | |
{ | |
public class RegisterDependencies : IServicesConfigurator | |
{ | |
public void Configure(IServiceCollection serviceCollection) | |
{ | |
serviceCollection.AddTransient<IPlaceholderTransformer, CustomPlaceholderTransformer>(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment