Can I limit the use of a widget based on a specific page type?

2024/03/26 6:36 AM

In making my pages fully support the PageBuilder I am creating many different widgets. The list is growinig and I fear it will at some point not be very friendly anymore. Is there a way to hide widgets from selection if I am on a certain Page Type? Or only allow widgets to a specified page type?


Environment

  • Xperience by Kentico version: [28.4.0]
  • .NET version: [8]
  • Deployment environment: [Local | Azure]
  • Link to Example of hero widget

Answers

2024/03/26 9:06 AM
Answer

I don't think you can directly restrict a widget to a specific Content Type, but you can restrict them in the View used for the Page Type's template. I believe there's a couple ways of approaching it:

You can restrict widgets for editable areas:

<div>
    @{
        var optionsLimited = new EditableAreaOptions
        {
            AllowedWidgets = new[] { "LearningKit.Widgets.NumberWidget",
                                     "LearningKit.Widgets.SomeOtherWidget" },
            AllowedSections = AllowedComponents.ALL
        };
    }
    @await Html.Kentico().EditableAreaAsync("limitedArea", optionsLimited)
</div>

You can restrict widgets for widget zones:

<div>
    @{
        var widgetsAllowlist = new[] { Kentico.Content.Web.Mvc.SystemComponentIdentifiers.FORM_WIDGET_IDENTIFIER,
                                       "LearningKit.Widgets.NumberWidget",
                                       "LearningKit.Widgets.SomeOtherWidget" };
    }
    @await Html.Kentico().WidgetZoneAsync(allowedWidgets: widgetsAllowlist)
</div>
2024/03/26 12:11 PM

@Danny-Paul

Liam's correct - those two APIs are the only way to limit which Widgets show up in the "add widget" dialog in the Page Builder, so anything you do to limit them will have to start there.

I wrote a blog post about the different ways you can use those APIs to manage which locations a growing number of Widgets are allowed in 👍.

2024/03/26 12:48 PM

Thank you both very much for your help!

2024/03/26 2:10 PM

Ohh! I like the injected service method, I normally don't like injecting services into views but this feels like a really neat way of setting the widgets

To answer this question, you have to login first.