2016-01-20 52 views
0

我有一个内容MyContainer它有一个Container部分连接到它。如何在Orchard中更改List项目形状的DisplayType?

然后,我有几个MyContainerItem内容有一个Containable部分连接到它,这些被分配到MyContainer

现在,如果内容MyContainer被渲染时,它在内部使用List形状,因此每个MyContainerItem内容呈现与Summary显示类型,这似乎在果园硬编码。

有什么方法可以在不改变核心代码的情况下将MyContainerItem的显示类型更改为Detail

我在我的替代品中尝试了形状转换技术,但无济于事。

回答

1

我以前在其他内容部分有过这个问题(不记得是哪一个),但你可以做的是采取以下步骤:

1创建一个自定义的一部分,并记录附加到具有ContainerPart的内容类型:

public CustomContainerPart : ContentPart<CustomContainerPartRecord> { 
    public string DisplayType { 
     // property setter and getter 
    } 
} 

2创建附加的CustomContainerPart上有ContainerPart的内容类型的处理程序:

public class CustomContainerPartHandler : ContentHandler { 
    private readonly IContentDefinitionManager _contentDefinitionManager; 

    public CustomContainerPartHandler(IContentDefinitionManager contentDefinitionManager) { 
     _contentDefinitionManager = contentDefinitionManager; 
    } 

    protected override void Activating(ActivatingContentContext context) { 
     base.Activating(context); 

     // weld the CustomContainerPart dynamically, if the type has the ContainerPart 
     var contentTypeDefinition = _contentDefinitionManager.GetTypeDefinition(context.ContentType); 
     if (contentTypeDefinition == null) { 
      return; 
     } 

     if (contentTypeDefinition.Parts.Any(part => part.PartDefinition.Name == typeof(ContainerPart).Name)) { 
      context.Builder.Weld<CustomContainerPart>(); 
     } 
    } 
} 

3创建自定义部分驾驶员和使用这种显示方法:

protected override DriverResult Display(CustomContainerPart part, string displayType, dynamic shapeHelper) {  
    return ContentShape("Parts_Container_CustomContained",() => { 
     var containerPart = part.As<ContainerPart>(); 
     // copy code from Orchard.Core/Containers/Drivers/ContainerPartDriver.cs here and tweak it to use the CustomContainerPart display type property 
     // .. 
     var listShape = shapeHelper.List(); 
     listShape.AddRange(pageOfItems.Select(item => _contentManager.BuildDisplay(item, part.DisplayType))); // here use your custom part setting 
     // .. 
    }); 
} 

4最后你r Placement.info:

<Place Parts_Container_Contained="-" /> 
<Match DisplayType="Detail"> 
    <Place Parts_Container_CustomContained="Content:7.5"/> 
</Match> 
+0

不错的想法,但有点太复杂,看我的答案我是如何解决它的。 – ViRuSTriNiTy

0

嗯,我正在接近它错误的方式。

在我看来,替代品是更好的解决方案,因为它不会改变列表行为。

现在我明白了这一点:列表通常应显示每个项目的摘要,当我点击该项目的细节显示,是有道理的。

因此,而不是改变DisplayType只是根据文档用适当的名称创建一个备用:

http://docs.orchardproject.net/Documentation/Accessing-and-rendering-shapes#NamingShapesandTemplates

在我的情况下,替代将是Content.Summary-MyContainerItem.cshtml具有以下内容:

@Model.Content.Items[0].Html 
+0

你确实可以做到这一点,但你实际上并没有使用细节显示类型,你只需调整你的总结显示类型就可以做到这一点。这意味着如果您更改了内容类型,它将不会自动反映在您的摘要视图中(因为它不是详细视图)。 – devqon

+0

也就是说,如果你的要求是简单地以自定义的方式显示它(不是本身的细节显示类型),这将是最好的选择:D – devqon

+0

@devqon是的,我接受你的答案,因为它是解决什么我最初试图去做。我一定会在稍后再谈到。 – ViRuSTriNiTy