Skip to content
New issue

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

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

Already on GitHub? # to your account

OrchardHelper content APIs should be accessible without namespace #17189

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
@@ -1,10 +1,10 @@
using Microsoft.Extensions.DependencyInjection;
using OrchardCore.ContentManagement;
using OrchardCore.ContentManagement.Records;
using YesSql;

namespace OrchardCore;

[Obsolete("This class has been deprecated, please use ContentOrchardHelperExtensions instead.")]
public static class ContentRazorHelperExtensions
{
/// <summary>
Expand All @@ -16,10 +16,7 @@ public static class ContentRazorHelperExtensions
/// <example>GetContentItemIdByHandleAsync("slug:myblog/my-blog-post").</example>
/// <returns>A content item id or <c>null</c> if it was not found.</returns>
public static Task<string> GetContentItemIdByHandleAsync(this IOrchardHelper orchardHelper, string handle)
{
var contentHandleManager = orchardHelper.HttpContext.RequestServices.GetService<IContentHandleManager>();
return contentHandleManager.GetContentItemIdAsync(handle);
}
=> ContentOrchardHelperExtensions.GetContentItemIdByHandleAsync(orchardHelper, handle);

/// <summary>
/// Loads a content item by its handle.
Expand All @@ -29,11 +26,7 @@ public static Task<string> GetContentItemIdByHandleAsync(this IOrchardHelper orc
/// <param name="option">A specific version to load or the default version.</param>
/// <returns>A content item with the specific name, or <c>null</c> if it doesn't exist.</returns>
public static async Task<ContentItem> GetContentItemByHandleAsync(this IOrchardHelper orchardHelper, string handle, VersionOptions option = null)
{
var contentItemId = await GetContentItemIdByHandleAsync(orchardHelper, handle);
var contentManager = orchardHelper.HttpContext.RequestServices.GetService<IContentManager>();
return await contentManager.GetAsync(contentItemId, option);
}
=> await ContentOrchardHelperExtensions.GetContentItemByHandleAsync(orchardHelper, handle, option);

/// <summary>
/// Loads a content item by its id.
Expand All @@ -43,11 +36,7 @@ public static async Task<ContentItem> GetContentItemByHandleAsync(this IOrchardH
/// <param name="option">A specific version to load or the default version.</param>
/// <returns>A content item with the specific id, or <c>null</c> if it doesn't exist.</returns>
public static Task<ContentItem> GetContentItemByIdAsync(this IOrchardHelper orchardHelper, string contentItemId, VersionOptions option = null)
{
var contentManager = orchardHelper.HttpContext.RequestServices.GetService<IContentManager>();

return contentManager.GetAsync(contentItemId, option);
}
=> ContentOrchardHelperExtensions.GetContentItemByIdAsync(orchardHelper, contentItemId, option);

/// <summary>
/// Loads a list of content items by their ids.
Expand All @@ -57,11 +46,7 @@ public static Task<ContentItem> GetContentItemByIdAsync(this IOrchardHelper orch
/// <param name="option">A specific version to load or the default version.</param>
/// <returns>A list of content items with the specific ids.</returns>
public static Task<IEnumerable<ContentItem>> GetContentItemsByIdAsync(this IOrchardHelper orchardHelper, IEnumerable<string> contentItemIds, VersionOptions option = null)
{
var contentManager = orchardHelper.HttpContext.RequestServices.GetService<IContentManager>();

return contentManager.GetAsync(contentItemIds, option);
}
=> ContentOrchardHelperExtensions.GetContentItemsByIdAsync(orchardHelper, contentItemIds, option);

/// <summary>
/// Loads a content item by its version id.
Expand All @@ -70,24 +55,13 @@ public static Task<IEnumerable<ContentItem>> GetContentItemsByIdAsync(this IOrch
/// <param name="contentItemVersionId">The content item version id to load.</param>
/// <returns>A content item with the specific version id, or <c>null</c> if it doesn't exist.</returns>
public static Task<ContentItem> GetContentItemByVersionIdAsync(this IOrchardHelper orchardHelper, string contentItemVersionId)
{
var contentManager = orchardHelper.HttpContext.RequestServices.GetService<IContentManager>();

return contentManager.GetVersionAsync(contentItemVersionId);
}
=> ContentOrchardHelperExtensions.GetContentItemByVersionIdAsync(orchardHelper, contentItemVersionId);

/// <summary>
/// Query content items.
/// </summary>
public static async Task<IEnumerable<ContentItem>> QueryContentItemsAsync(this IOrchardHelper orchardHelper, Func<IQuery<ContentItem, ContentItemIndex>, IQuery<ContentItem>> query)
{
var contentManager = orchardHelper.HttpContext.RequestServices.GetService<IContentManager>();
var session = orchardHelper.HttpContext.RequestServices.GetService<ISession>();

var contentItems = await query(session.Query<ContentItem, ContentItemIndex>()).ListAsync();

return await contentManager.LoadAsync(contentItems);
}
=> await ContentOrchardHelperExtensions.QueryContentItemsAsync(orchardHelper, query);

/// <summary>
/// Loads content items of a specific type.
Expand All @@ -96,9 +70,5 @@ public static async Task<IEnumerable<ContentItem>> QueryContentItemsAsync(this I
/// <param name="contentType">The content type to load.</param>
/// <param name="maxContentItems">The maximum content items to return.</param>
public static Task<IEnumerable<ContentItem>> GetRecentContentItemsByContentTypeAsync(this IOrchardHelper orchardHelper, string contentType, int maxContentItems = 10)
{
return orchardHelper.QueryContentItemsAsync(query => query.Where(x => x.ContentType == contentType && x.Published == true)
.OrderByDescending(x => x.CreatedUtc)
.Take(maxContentItems));
}
=> ContentOrchardHelperExtensions.GetRecentContentItemsByContentTypeAsync(orchardHelper, contentType, maxContentItems);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
using Microsoft.Extensions.DependencyInjection;
using OrchardCore;
using OrchardCore.ContentManagement;
using OrchardCore.ContentManagement.Records;
using YesSql;

#pragma warning disable CA1050 // Declare types in namespaces
public static class ContentOrchardHelperExtensions
#pragma warning restore CA1050 // Declare types in namespaces
{
/// <summary>
/// Returns a content item id by its handle.
/// </summary>
/// <param name="orchardHelper">The <see cref="IOrchardHelper"/>.</param>
/// <param name="handle">The handle.</param>
/// <example>GetContentItemIdByHandleAsync("alias:carousel").</example>
/// <example>GetContentItemIdByHandleAsync("slug:myblog/my-blog-post").</example>
/// <returns>A content item id or <c>null</c> if it was not found.</returns>
public static async Task<string> GetContentItemIdByHandleAsync(this IOrchardHelper orchardHelper, string handle)
{
var contentHandleManager = orchardHelper.HttpContext.RequestServices.GetService<IContentHandleManager>();

return await contentHandleManager.GetContentItemIdAsync(handle);
}

/// <summary>
/// Loads a content item by its handle.
/// </summary>
/// <param name="orchardHelper">The <see cref="IOrchardHelper"/>.</param>
/// <param name="handle">The handle to load.</param>
/// <param name="option">A specific version to load or the default version.</param>
/// <returns>A content item with the specific name, or <c>null</c> if it doesn't exist.</returns>
public static async Task<ContentItem> GetContentItemByHandleAsync(this IOrchardHelper orchardHelper, string handle, VersionOptions option = null)
{
var contentManager = orchardHelper.HttpContext.RequestServices.GetService<IContentManager>();

var contentItemId = await orchardHelper.GetContentItemIdByHandleAsync(handle);

return await contentManager.GetAsync(contentItemId, option);
}

/// <summary>
/// Loads a content item by its id.
/// </summary>
/// <param name="orchardHelper">The <see cref="IOrchardHelper"/>.</param>
/// <param name="contentItemId">The content item id to load.</param>
/// <param name="option">A specific version to load or the default version.</param>
/// <returns>A content item with the specific id, or <c>null</c> if it doesn't exist.</returns>
public static async Task<ContentItem> GetContentItemByIdAsync(this IOrchardHelper orchardHelper, string contentItemId, VersionOptions option = null)
{
var contentManager = orchardHelper.HttpContext.RequestServices.GetService<IContentManager>();

return await contentManager.GetAsync(contentItemId, option);
}

/// <summary>
/// Loads a list of content items by their ids.
/// </summary>
/// <param name="orchardHelper">The <see cref="IOrchardHelper"/>.</param>
/// <param name="contentItemIds">The content item ids to load.</param>
/// <param name="option">A specific version to load or the default version.</param>
/// <returns>A list of content items with the specific ids.</returns>
public static async Task<IEnumerable<ContentItem>> GetContentItemsByIdAsync(this IOrchardHelper orchardHelper, IEnumerable<string> contentItemIds, VersionOptions option = null)
{
var contentManager = orchardHelper.HttpContext.RequestServices.GetService<IContentManager>();

return await contentManager.GetAsync(contentItemIds, option);
}

/// <summary>
/// Loads a content item by its version id.
/// </summary>
/// <param name="orchardHelper">The <see cref="IOrchardHelper"/>.</param>
/// <param name="contentItemVersionId">The content item version id to load.</param>
/// <returns>A content item with the specific version id, or <c>null</c> if it doesn't exist.</returns>
public static async Task<ContentItem> GetContentItemByVersionIdAsync(this IOrchardHelper orchardHelper, string contentItemVersionId)
{
var contentManager = orchardHelper.HttpContext.RequestServices.GetService<IContentManager>();

return await contentManager.GetVersionAsync(contentItemVersionId);
}

/// <summary>
/// Query content items.
/// </summary>
public static async Task<IEnumerable<ContentItem>> QueryContentItemsAsync(this IOrchardHelper orchardHelper, Func<IQuery<ContentItem, ContentItemIndex>, IQuery<ContentItem>> query)
{
var contentManager = orchardHelper.HttpContext.RequestServices.GetService<IContentManager>();
var session = orchardHelper.HttpContext.RequestServices.GetService<ISession>();

var contentItems = await query(session.Query<ContentItem, ContentItemIndex>()).ListAsync();

return await contentManager.LoadAsync(contentItems);
}

/// <summary>
/// Loads content items of a specific type.
/// </summary>
/// <param name="orchardHelper">The <see cref="IOrchardHelper"/>.</param>
/// <param name="contentType">The content type to load.</param>
/// <param name="maxContentItems">The maximum content items to return.</param>
public static async Task<IEnumerable<ContentItem>> GetRecentContentItemsByContentTypeAsync(this IOrchardHelper orchardHelper, string contentType, int maxContentItems = 10)
=> await orchardHelper.QueryContentItemsAsync(query => query.Where(x => x.ContentType == contentType && x.Published == true)
.OrderByDescending(i => i.CreatedUtc)
.Take(maxContentItems));
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\OrchardCore.ContentManagement.Abstractions\OrchardCore.ContentManagement.Abstractions.csproj" />
<ProjectReference Include="..\OrchardCore.ContentManagement\OrchardCore.ContentManagement.csproj" />
<ProjectReference Include="..\OrchardCore.DisplayManagement.Abstractions\OrchardCore.DisplayManagement.Abstractions.csproj" />
<ProjectReference Include="..\OrchardCore.Infrastructure.Abstractions\OrchardCore.Infrastructure.Abstractions.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="YesSql.Filters.Query"/>
<PackageReference Include="YesSql.Filters.Query" />
</ItemGroup>

</Project>
6 changes: 6 additions & 0 deletions src/docs/releases/3.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ Before upgrading from version 2 to v3, it is important to first compile your pro

## Breaking Changes

### Content Module

In the previous versions you need to use `OrhardCore` namespace in Razor pages or views whenever you want to access the content helpers that provided by `IOrchardHelper` which is unlike the other services that you can access directly without the need to use the namespace. In this version, we have removed the need to use the `OrchardCore` namespace in Razor pages or views to access the content helpers. You can now access the content helpers directly without the need to use the `OrchardCore` namespace.

The `ContentRazorHelperExtensions` has been replaced by `ContentOrchardHelperExtensions`.

### Email Module

Previously, emails sent from Orchard Core could have either a plain text body, or an HTML body, but not both. Now, they can have both. This also brings some code-level API changes, see below.
Expand Down