Sitecore 10.1 (SXA) - Decorate all components and forms in one go.

Sitecore 10.1 (SXA) - Decorate all components and forms in one go.

It's a common?scenario where we want to wrap each Sitecore component (all page component) or form to add attributes like identifiers for analytics or any additional details for the tracking, monitoring, etc. So we can see what is exactly happening at the component level.

Suppose if you have so many features or components, and now you got the requirement to add an attribute or identifier to each element, what would we do?

I don't think you would think to update all individual feature or component; otherwise, this small change would be significant.

Sitecore has provided very nice and decorated pipelines to wrap all the components and handle these scenarios.

References - decoraterendering_body

These two essential and simple pipelines are.

  1. Decorate Rendering Pipeline to wrap all the SXA components.

No alt text provided for this image

For example, if we want to wrap each SXA component and add the component name or any unique identifier, it can be done by these simple steps.

? ? <sitecore
? ? ? ? <pipelines>
? ? ? ? ? ? ? ? <processor type="Test.Feature.DecorateComponent.AddNameAttributes, Test.Feature" resolve="true"
? ? ? ? ? ? ? ? ? ? ? ? ? ?patch:after="processor[@type='Sitecore.XA.Foundation.Presentation.Pipelines.DecorateRendering.GetRenderingCssClasses']"/>
? ? ? ? ? ? </decorateRendering>
? ? ? ? </pipelines>
? ? </sitecore>>
        

The sample structure

using Sitecore.XA.Foundation.MarkupDecorator.Pipelines.DecorateRendering;

namespace Test.Feature.DecorateComponent
{


? ? public class AddNameAttributes : RenderingDecorator
? ? {
? ? ? ? public override void Process(RenderingDecoratorArgs args)
? ? ? ? {
? ? ? ? ? ? var rendering = args.Rendering;
? ? ? ? ? ? if (rendering == null)
? ? ? ? ? ? ? ? return;


? ? ? ? ? ? args.AddAttribute("you class name", args.Rendering.Name + "_" + rendering?.UniqueId?.ToShortID());
? ? ? ? ? ??
? ? ? ? }
? ? }
}t        

and that's it, so simple, right?

Check all your pages and verify the SXA component. You will see this new attribute now.

2. Wrap the form component - Sitecore.ExperienceForms.Mvc.Pipelines.RenderForm.SetHtmlHelperSettings

No alt text provided for this image

Configuration for the registration

? ?<sitecore
? ? ? ? <pipelines>
? ? ? ? ? ? <forms.renderForm>
? ? ? ? ? ? ? ? <processor patch:after="*[3]" type="Test.Feature.DecorateComponent.AddFormNameAttributes, Test.Feature" resolve="true"/>
? ? ? ? ? ? </forms.renderForm>
? ? ? ? </pipelines>
? ? </sitecore>>        

Sample structure: I hope this article will help decorate the SXA components and forms. Let me know if you have any questions or suggestions.

using Sitecore.ExperienceForms.Mvc.Pipelines.RenderFor
using Sitecore.Mvc.Pipelines;
namespace Test.Feature.DecorateComponent
{


? ? public class AddFormNameAttributes : MvcPipelineProcessor<RenderFormEventArgs>
? ? {


? ? ? ? public override void Process(RenderFormEventArgs args)
? ? ? ? {
? ? ? ? ? ? args?.Attributes?.Add("you class name"? args.ViewModel?.Name);
? ? ? ? }
? ? }
};
        

要查看或添加评论,请登录

Jitendra Soni的更多文章

社区洞察

其他会员也浏览了