0

我想根据其标签值为内容项创建替代项。

例如,我想创建一个名为List-ProjectionPage-tags-special

搜索网络指示我实施一个新的ShapeDisplayEvents

因此,我有

public class TagAlternatesFactory : ShapeDisplayEvents
{
    public TagAlternatesFactory()
    {
    }

    public override void Displaying(ShapeDisplayingContext context)
    {
    }
}

在该Displaying方法中,我相信我需要检查 context.Shape 中的 contentItem 并基于它创建一个备用名称(假设它已将 TagsPart 添加到内容项中)。

但是,那我该怎么办呢?如何添加候补名称?这就是创建新的替代类型所需的全部内容吗?果园会知道找List-ProjectionPage-tags-special吗?

4

1 回答 1

2

I took a cue from Bertrand's comment and looked at some Orchard source for direction.

Here's my implementation:

public class TagAlternatesFactory : ShapeDisplayEvents
{
    public override void Displaying(ShapeDisplayingContext context)
    {
        context.ShapeMetadata.OnDisplaying(displayedContext =>
        {
            var contentItem = displayedContext.Shape.ContentItem;
            var contentType = contentItem.ContentType;

            var parts = contentItem.Parts as IEnumerable<ContentPart>;
            if (parts == null) return;

            var tagsPart = parts.FirstOrDefault(part => part is TagsPart) as TagsPart;
            if (tagsPart == null) return;

            foreach (var tag in tagsPart.CurrentTags)
            {
                displayedContext.ShapeMetadata.Alternates.Add(
                                    String.Format("{0}__{1}__{2}__{3}", 
displayedContext.ShapeMetadata.Type, (string)contentType, "tag", tag.TagName)); //See update
            }
        });
    }
}

This allows an alternate view based on a tag value. So, if you have a project page that you want to apply a specific style to, you can simply create your alternate view with the name ProjectionPage_tag_special and anytime you want a projection page to use it, just add the special tag to it.

Update I added the displayedContext.ShapeMetadata.Type to the alternate name so specific shapes could be overridden (like the List-ProjectionPage)

于 2013-06-21T15:40:29.627 回答