Orchard查询按内容类型BlogPost和某些BlogId进行过滤

时间:2012-08-21 23:42:20

标签: asp.net-mvc orchardcms

我安装了两个博客的Orchard,其中一个用于公司新闻,另一个用于成员出版物。

我想创建一个小部件,显示公司新闻博客中的一部分帖子。

Orchard是否可以创建一个按ContentType BlogPost和BlogId过滤的查询? 我按内容类型找到了一个过滤器,但我没有找到BlogId的过滤器。

2 个答案:

答案 0 :(得分:4)

这将是一个非常简单的过滤器,可以在模块中添加。这是一个用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编号可配置,但这应该让你走上正轨。

答案 1 :(得分:1)

我玩了一些过滤器,这就是我所做的。 此过滤器允许您根据站点中的某个博客过滤BlogPost。如果您有多个博客并且想要显示一两个最近的帖子,这将非常有用。

它对我有用。

任何评论都将不胜感激。

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);

        }
    }
}