我有一个带有两个博客的 Orchard 安装,其中一个用于公司新闻,另一个用于成员出版物。
我想创建一个小部件来显示来自公司新闻博客的帖子子集。
是否可以在 Orchard 中创建一个按 ContentType BlogPost 和 BlogId 过滤的查询?我找到了按内容类型的过滤器,但没有找到按 BlogId 的过滤器。
我有一个带有两个博客的 Orchard 安装,其中一个用于公司新闻,另一个用于成员出版物。
我想创建一个小部件来显示来自公司新闻博客的帖子子集。
是否可以在 Orchard 中创建一个按 ContentType BlogPost 和 BlogId 过滤的查询?我找到了按内容类型的过滤器,但没有找到按 BlogId 的过滤器。
这将是一个在模块中添加的非常简单的过滤器。这是一个按 id 过滤的硬编码示例...
public class ContentIdFilter : IFilterProvider {
private const int HardCodedId = 99;
public ContentIdFilter() {
T = NullLocalizer.Instance;
}
public Localizer T { get; set; }
public void Describe(DescribeFilterContext describe) {
describe.For("Content", T("Content"), T("Content"))
.Element("ContentId", T("Content Id"), T("Content w/ Id: " + HardCodedId.ToString()),
ApplyFilter,
DisplayFilter,
null);
}
public void ApplyFilter(dynamic context) {
var query = (IHqlQuery)context.Query;
context.Query = query.Where(x => x.ContentItem(), x => x.Eq("Id", HardCodedId));
}
public LocalizedString DisplayFilter(dynamic context) {
return T("Content w/ Id: " + HardCodedId.ToString());
}
}
要使 ID 号可配置,还有更多工作要做,但这应该让您走上正确的轨道。
I have played a bit with filter and this is what I did. This filter allows you to filter BlogPost based on one of the Blog you have in the site. It is useful if you have more than one blog and you want to show recent posts from just one or two.
It works for me.
Any comment will be appreciated.
using System;
using System.Linq;
using System.Web.Mvc;
using Orchard.Blogs.Models;
using Orchard.ContentManagement;
using Orchard.Core.Common.Models;
using Orchard.DisplayManagement;
using Orchard.Forms.Services;
using Orchard.Localization;
using Orchard.Projections.Descriptors.Filter;
using IFilterProvider = Orchard.Projections.Services.IFilterProvider;
namespace Orchard.Projections.Providers.Filters
{
public class BlogPostFilter : IFilterProvider
{
private readonly IContentManager _contentManager;
public BlogPostFilter(
IContentManager contentManager)
{
_contentManager = contentManager;
T = NullLocalizer.Instance;
}
public Localizer T { get; set; }
public void Describe(DescribeFilterContext describe)
{
describe.For("Blogs", T("Blogs"), T("Blogs"))
.Element("Blogs", T("Blogs"), T("Posts in blogs"),
ApplyFilter,
DisplayFilter,
"BlogPostFilter");
}
public void ApplyFilter(dynamic context)
{
var blogIds = (string)context.State.Blogs;
if (!String.IsNullOrEmpty(blogIds)) {
var ids = blogIds.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
var query = (IHqlQuery)context.Query;
context.Query = query.Where(x => x.ContentPartRecord<CommonPartRecord>(), x => x.In("Container.Id", ids));
}
}
public LocalizedString DisplayFilter(dynamic context)
{
var blogIds = (string)context.State.Blogs;
if (!String.IsNullOrEmpty(blogIds)) {
var ids = Array.ConvertAll(blogIds.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries), s => int.Parse(s));;
var names = _contentManager.GetMany<BlogPart>(ids, VersionOptions.Published, QueryHints.Empty);
return T("Posts in blogs: " + string.Join(", ", names.Select(x => x.Name)));
}
return T("Posts in blogs");
}
}
public class BlogPostFilterForms : IFormProvider
{
private readonly IContentManager _contentManager;
protected dynamic Shape { get; set; }
public Localizer T { get; set; }
public BlogPostFilterForms(
IShapeFactory shapeFactory,
IContentManager contentManager)
{
_contentManager = contentManager;
Shape = shapeFactory;
T = NullLocalizer.Instance;
}
public void Describe(DescribeContext context)
{
Func<IShapeFactory, object> form =
shape => {
var f = Shape.Form(
Id: "AnyOfBlogPart",
_Parts: Shape.SelectList(
Id: "blogs", Name: "Blogs",
Title: T("Blogs"),
Description: T("Select some blogs."),
Size: 10,
Multiple: true
)
);
f._Parts.Add(new SelectListItem { Value = "", Text = T("Any").Text });
foreach (var blog in _contentManager.Query<BlogPart>().List().OrderBy(x => x.Name)) {
f._Parts.Add(new SelectListItem { Value = blog.Id.ToString(), Text = blog.Name });
}
return f;
};
context.Form("BlogPostFilter", form);
}
}
}