0

Is there a way to limit the max number of composite items in an SXA composite component like Carousel? For example the carousel has no more then 8 slides. It sounds like SXA does not have anything built in and I'd need to implement some sort of validation. If that is the case can anyone share an example of how to achieve that?

This is on SXA 10.2

1

1 Answer 1

2

You can do it by adding the validation through OnItemCreated event. Where you can specify your template name (for example Carousel) and if its child items exceed the limit then you can show the message and delete the item. The code will look like this.

public void OnItemCreated(object sender, EventArgs args)
{
    var createdArgs = Event.ExtractParameter(args, 0) as ItemCreatedEventArgs;

    if (createdArgs != null)
    {
        if (createdArgs.Item != null)
        {
            var item = createdArgs.Item;

            if (item.Parent != null && 
                item.Parent.TemplateName == "carousel" &&
                item.Parent.Children.Count() > 8)
            {
                SheerResponse.Alert(
                    String.Format("Sorry, you cannot add more than 8 slides to {0}.",
                                      item.Parent.Name), new string[0]);
               
               // write here your logic to delete the item. 
            }
        }
    }
}

This works when you create the item from the content editor and experience editor.

Once you are done with your method then register it using a patch config like this.

<event name="item:created">
    <handler type="MyProject.Sc.Extensions.Events.ItemCreatedEvent, MyProject.Sc.Extensions" method="OnItemCreated" />
</event>

It will look like this.

enter image description here

Hope this helps.

1
  • Thanks. I suspected I had to implement that custom event if SXA didn't have any sort of built in validation
    – Diego
    Dec 21, 2022 at 13:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.