惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 叶小钗
Microsoft Azure Blog
Microsoft Azure Blog
Stack Overflow Blog
Stack Overflow Blog
Jina AI
Jina AI
Vercel News
Vercel News
H
Help Net Security
Martin Fowler
Martin Fowler
美团技术团队
云风的 BLOG
云风的 BLOG
Y
Y Combinator Blog
阮一峰的网络日志
阮一峰的网络日志
MyScale Blog
MyScale Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 三生石上(FineUI控件)
博客园 - 司徒正美
人人都是产品经理
人人都是产品经理
Engineering at Meta
Engineering at Meta
G
Google Developers Blog
Blog — PlanetScale
Blog — PlanetScale
MongoDB | Blog
MongoDB | Blog
宝玉的分享
宝玉的分享
小众软件
小众软件
T
Tailwind CSS Blog
WordPress大学
WordPress大学

博客园 - 飞翔的天空

sp.net core部署到iis中出现 HTTP Error 502.5 - Process Failure 的解决办法 aspnet zero Swagger 不出现Authorize按钮的解决办法 Js获取当前日期时间及其它操作 js阿拉伯数字转中文大写 php学习点滴 excel合并单元格内容 EXCEL公式以指定分隔符从右往左截取字符 drupal学习FAQ drupal模块简介 js中三目运算及readonly的解决办法 Zxing中文乱码的简单解决办法 easyUI MVC3一些注意的东东(4) orchard模块编写的错误及其解决办法 orchard文档之-orchard工作原理 orchard文档之-搜索和索引 orchard文档之-更新站点到新的orchard版本 orchard文档之-创建自定义表单 准备把orchard的自己认为重要的文档翻译一遍 orchard文档之-理解数据访问
orchard文档之-理解内容处理器
飞翔的天空 · 2013-05-31 · via 博客园 - 飞翔的天空

理解内容处理器

A content handler defines what happens with a content part in response to specific events, such as when the part is activated. The content handler enables you to perform actions at particular moments in the lifecycle of the content item. It also enables you to set up data repositories and manipulate the data model prior to rendering the content item.

Typically, you define a handler for a content part by creating a class that inherits from ContentHandler. The ContentHandler class is a base class that provides the methods and properties you will commonly need when defining your own content handler. Alternately, you can also create your own content handler by creating a class that implements IContentHandler.

Defining Data Repository and Adding Filters

When working with a content part that persists data, add a constructor for the handler that accepts an IRepository parameter for objects of the type you defined for records in the part. The following code shows a basic implementation of a content handler. MapRecord is a class defined in a separate file.

using Map.Models;
using Orchard.ContentManagement.Handlers;
using Orchard.Data;

namespace Map.Handlers {
    public class MapHandler : ContentHandler {
        public MapHandler(IRepository<MapRecord> repository) {
            Filters.Add(StorageFilter.For(repository));
        }
    }
}

You can add other types of filters to the content handler. For example, you can add an ActivatingFilter to the Filters collection to define how the part is added to a type.

Built-in filter types

  • StorageFilter class - Takes care of persisting the data from repository object to the database. Its usage is shown in the example above.
  • ActivatingFilter class - Attaches a part to a content type from code. As opposed to attaching parts via migrations, parts attached using this filter will neither be displayed in the Dashboard, nor users will be able to remove them from types. It's a legitimate way of attaching parts that should always exist on a given content type.

Lifecycle Events

In addition to defining the repository, you can add code for handling events. You use the following methods to add the code that is executed for the event:

  • OnActivated
  • OnCreated
  • OnCreating
  • OnIndexed
  • OnIndexing
  • OnInitializing
  • OnLoaded
  • OnLoading
  • OnPublished
  • OnPublishing
  • OnRemoved
  • OnRemoving
  • OnUnpublished
  • OnUnpublishing
  • OnVersioned
  • OnVersioning

For example, the TagPartHandler class contains code to take action for the Removed and Indexing events, as shown in the following example:

public class TagsPartHandler : ContentHandler {
    public TagsPartHandler(IRepository<TagsPartRecord> repository, ITagService tagService) {
        Filters.Add(StorageFilter.For(repository));

        OnRemoved<TagsPart>((context, tags) => 
            tagService.RemoveTagsForContentItem(context.ContentItem));

        OnIndexing<TagsPart>((context, tagsPart) => 
            context.DocumentIndex.Add("tags", String.Join(", ", tagsPart.CurrentTags.Select(t => t.TagName))).Analyze());
    }
}

Data Manipulation

You can override the following methods to perform actions related to the state of the data:

  • GetItemMetadata
  • BuildDisplayShape
  • BuildEditorShape
  • UpdateEditorShape

For example, the BlogPostPartHandler class overrides the GetItemMetadata method to add route values using code like the following:

protected override void GetItemMetadata(GetContentItemMetadataContext context) {
    var blogPost = context.ContentItem.As<BlogPostPart>();

    if (blogPost == null)
        return;

    context.Metadata.CreateRouteValues = new RouteValueDictionary {
        {"Area", "Orchard.Blogs"},
        {"Controller", "BlogPostAdmin"},
        {"Action", "Create"},
        {"blogId", blogPost.BlogPart.Id}
    };
    context.Metadata.EditorRouteValues = new RouteValueDictionary {
        {"Area", "Orchard.Blogs"},
        {"Controller", "BlogPostAdmin"},
        {"Action", "Edit"},
        {"postId", context.ContentItem.Id},
        {"blogId", blogPost.BlogPart.Id}
    };
    context.Metadata.RemoveRouteValues = new RouteValueDictionary {
        {"Area", "Orchard.Blogs"},
        {"Controller", "BlogPostAdmin"},
        {"Action", "Delete"},
        {"postId", context.ContentItem.Id},
        {"blogSlug", blogPost.BlogPart.As<RoutePart>().Slug}
    };
}