Browse Source

cms: Add conditional widget adding to CmsKitContentWidgetOptions

pull/24110/head
EngincanV 3 months ago
parent
commit
4bae80156e
  1. 21
      docs/en/modules/cms-kit/dynamic-widget.md
  2. 23
      modules/cms-kit/src/Volo.CmsKit.Common.Web/Pages/CmsKit/Components/Contents/CmsKitContentWidgetOptions.cs

21
docs/en/modules/cms-kit/dynamic-widget.md

@ -124,21 +124,26 @@ In this image, after choosing your widget (on the other case, it changes automat
You can edit this output manually if do any wrong coding for that (wrong value or typo) you won't see the widget, even so, your page will be viewed successfully.
## Options
To configure the widget, you should define the below code in YourModule.cs
## Options
To add content widgets, you should configure the `CmsKitContentWidgetOptions` in your module's `ConfigureServices` method:
```csharp
Configure<CmsKitContentWidgetOptions>(options =>
{
options.AddWidget(widgetType: "Today", widgetName: "CmsToday", parameterWidgetName: "Format");
// Alternatively, you can add a widget conditionally based on a global feature being enabled
options.AddWidgetIfFeatureEnabled(typeof(PagesFeature), "Today", "CmsToday", "Format");
});
```
Let's look at these parameters in detail
* `widgetType` is used for end-user and more readable names. The following bold word represents widgetType.
[Widget Type="**Today**" Format="yyyy-dd-mm HH:mm:ss"].
The `CmsKitContentWidgetOptions` provides two methods for registering widgets:
* `widgetName` is used for your widget name used in code for the name of the `ViewComponent`.
- **AddWidget:** Registers a widget that will be available in the content editor. It accepts the following parameters:
- `widgetType` (required): A user-friendly name for the widget that appears in the widget selection dropdown and is used in content markup. For example, in `[Widget Type="Today"]`, `"Today"` is the `widgetType`.
- `widgetName` (required): The name of the `ViewComponent` that will be rendered. This must match the `Name` attribute of your `ViewComponent` (e.g., `[ViewComponent(Name = "CmsToday")]`).
- `parameterWidgetName` (optional): The name of the parameter widget that will be displayed in the "Add Widget" modal to collect parameter values from users. This is only required when your widget needs parameters.
* `parameterWidgetName` is used the for editor component side to see on the `Add Widget` modal.
After choosing the widget type from listbox (now just defined `Format`) and renders this widget automatically. It's required only to see UI once using parameters
- **AddWidgetIfFeatureEnabled:** Registers a widget conditionally, only if a specified [global feature](../../framework/infrastructure/global-features.md) is enabled. It accepts the same parameters as `AddWidget`, plus an additional first parameter:
- `featureType` (required): The type of the global feature that must be enabled for the widget to be available (e.g., `typeof(PagesFeature)`).

23
modules/cms-kit/src/Volo.CmsKit.Common.Web/Pages/CmsKit/Components/Contents/CmsKitContentWidgetOptions.cs

@ -1,4 +1,8 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Reflection;
using Volo.Abp;
using Volo.Abp.GlobalFeatures;
namespace Volo.CmsKit.Web.Contents;
@ -16,4 +20,21 @@ public class CmsKitContentWidgetOptions
var config = new ContentWidgetConfig(widgetName, parameterWidgetName);
WidgetConfigs.Add(widgetType, config);
}
public void AddWidgetIfFeatureEnabled(Type globalFeatureType, string widgetType, string widgetName, string parameterWidgetName = null)
{
Check.NotNull(globalFeatureType, nameof(globalFeatureType));
if(globalFeatureType.GetCustomAttribute<GlobalFeatureNameAttribute>() == null)
{
throw new ArgumentException($"The type {globalFeatureType.Name} must have a {nameof(GlobalFeatureNameAttribute)} attribute.", nameof(globalFeatureType));
}
if(!GlobalFeatureManager.Instance.IsEnabled(globalFeatureType))
{
return;
}
AddWidget(widgetType, widgetName, parameterWidgetName);
}
}
Loading…
Cancel
Save