Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SiteSettings for custom settings and to get settings by name #16220

Merged
merged 8 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ public override Task GetContentItemAspectAsync(ContentItemAspectContext context)
{
// This handlers provides defaults, either from the Seo Meta Settings, or ensures values by default. (title etc)
_contentManager ??= _httpContextAccessor.HttpContext.RequestServices.GetRequiredService<IContentManager>();

var siteSettings = await _siteService.GetSiteSettingsAsync();
var metaSettings = siteSettings.As<ContentItem>("SocialMetaSettings");

Expand Down
17 changes: 17 additions & 0 deletions src/OrchardCore/OrchardCore.Contents.Core/SiteServiceExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Threading.Tasks;
using OrchardCore.ContentManagement;
using OrchardCore.Entities;

namespace OrchardCore.Settings;

public static class SiteServiceExtensions
{
/// <summary>
/// Gets an instance of the specified custom settings if it exists.
/// </summary>
/// <param name="siteService">The site service.</param>
/// <param name="name">The entry name.</param>
/// <returns>A <see cref="ContentItem" /> instance that matches the given name, if one exists.</returns>
public static async Task<ContentItem> GetCustomSettingsAsync(this ISiteService siteService, string name)
=> (await siteService.GetSiteSettingsAsync()).As<ContentItem>(name);
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ public static class EntityExtensions
/// <returns>A new instance of the requested type if the property was not found.</returns>
public static T As<T>(this IEntity entity, string name) where T : new()
{
JsonNode value;
if (entity.Properties.TryGetPropertyValue(name, out value))
ArgumentNullException.ThrowIfNull(name);

if (entity.Properties.TryGetPropertyValue(name, out var value))
{
return value.Deserialize<T>(JOptions.Default);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Threading.Tasks;
using OrchardCore.Entities;

namespace OrchardCore.Settings;

Expand All @@ -12,4 +13,14 @@ public static class SiteServiceExtensions
/// <returns>An instance of the given type if one exists.</returns>
public static async Task<T> GetSettingsAsync<T>(this ISiteService siteService) where T : new()
=> (await siteService.GetSiteSettingsAsync()).As<T>();

/// <summary>
/// Gets an instance of the specified settings if it exists.
/// </summary>
/// <typeparam name="T">The type of the settings to attempt to get.</typeparam>
/// <param name="siteService">The site service.</param>
/// <param name="name">The entry name.</param>
/// <returns>An instance of the given type if one exists.</returns>
public static async Task<T> GetSettingsAsync<T>(this ISiteService siteService, string name) where T : new()
=> (await siteService.GetSiteSettingsAsync()).As<T>(name);
}
8 changes: 8 additions & 0 deletions src/docs/reference/modules/CustomSettings/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,16 @@ public class MyController : Controller
}
public async Task<IActionResult> Index()
{
/*
For convenience, we have added an extension method named 'GetCustomSettingsAsync' to easily access the custom settings.
This method is available in the OrchardCore.Contents.Core package.

Alternatively, you can access the custom settings without using the 'GetCustomSettingsAsync' extension method as follow:
var siteSettings = await _siteService.GetSiteSettingsAsync();
var blogSettings = siteSettings.As<ContentItem>("BlogSettings");
*/

ContentItem blogSettings = await _siteService.GetCustomSettingsAsync("BlogSettings");
var blogHtml = blogSettings.As<HtmlBodyPart>();

return View();
Expand Down
15 changes: 14 additions & 1 deletion src/docs/releases/2.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ Prior to this release, if you had a module with multiple features, you had to us

### Site Settings

A new extension method named `GetSettingsAsync<T>()` has been added to the `ISiteService` interface. This method allows you to retrieve specific settings with a single line of code. For example, to get the `LoginSettings`, you can now use:
- New extension methods named `GetSettingsAsync<T>()` and `GetSettingsAsync<T>("")` were added to the `ISiteService` interface. These methods allow you to retrieve specific settings with a single line of code. For example, to get the `LoginSettings`, you can now use:

```csharp
await _siteService.GetSettingsAsync<LoginSettings>();
Expand All @@ -528,6 +528,19 @@ Previously, achieving the same result required more code:

```csharp
(await _siteService.GetSiteSettingsAsync()).As<LoginSettings>();
```

- A new extension method named `GetCustomSettingsAsync()` was added to the `ISiteService` interface. This method allows you to retrieve [custom settings](../reference/modules/CustomSettings/README.md). For example, to get custom settings of type 'BlogSettings', you can now use:

```csharp
ContentItem blogSettings = await _siteService.GetCustomSettingsAsync("BlogSettings");
```

Previously, achieving the same result required more code:

```csharp
var siteSettings = await _siteService.GetSiteSettingsAsync();
var blogSettings = siteSettings.As<ContentItem>("BlogSettings");
```

### Content Fields
Expand Down