从内容搜索器字段

时间:2018-01-09 21:25:54

标签: orchardcms orchardcms-1.10

我有一个名为DayTile的内容类型,我已经设置了一个内容选择器字段,该字段仅限于HotelSection类型。此类型是酒店类型的容器。

当DayTile选中该部分时,我想渲染HotelSection中的所有酒店。这可能吗?

根据我迄今为止所尝试的内容,似乎HotelSection所包含的项目无法作为内容选择器字段进行访问。

2 个答案:

答案 0 :(得分:2)

所选内容项由ContentPickerField s ContentItems属性公开(请参阅Orchard.ContentPicker.Fields.ContentPickerField)。

要从DayTile内容项访问此内容,请使用名为" Content-DayTile.cshtml"的Razor模板,您可以这样做:

@{
    dynamic contentItem = Model.ContentItem; // How to access the content item depends on the context. In this case, we're in a Content shape template.

   // Assuming your picker field is named "HotelSections" and attached to your DayTile content type (which in reality means it's attached to an automatically created part called "DayTile").

   var picker = contentItem.DayTile.HotelSections;
   var hotelSectionContentItems = picker.ContentItems; // Typed as IEnumerable<ContentItem>.
}

<ul>
   @foreach(var hotelSectionContentItem in hotelSectionContentItems)
   {
      // You can now either access individual parts and fields of each hotel section content item and render it, or you can choose to build a shape for each item and display that. Here are examples:  

      <li>@hotelSectionContentItem.TitlePart.Title</li>

      // or: 
      // Build a content shape for the content item.
      var hotelSectionContentShape = BuildDisplay(hotelSectionContentItem, "Summary");

     // Render the shape.
     <li>@Display(hotelSectionContentShape)</li>
}

答案 1 :(得分:1)

如果我理解正确,这绝对是可能的。

您需要覆盖主题/模块中的Fields.ContentPicker视图,并且在foreach循环中而不是显示指向每个HotelSection的链接,您希望显示该HotelSection内容项,而后者将显示所有酒店包含在HotelSection中,如下:

@using Orchard.ContentPicker.Fields
@using Orchard.Utility.Extensions;

@{
    var field = (ContentPickerField) Model.ContentField;
    string name = field.DisplayName;
    var contentItems = field.ContentItems;
}
<p class="content-picker-field content-picker-field-@name.HtmlClassify()">
    <span class="name">@name</span>
    @if(contentItems.Any()) {
        foreach(var contentItem in contentItems) {
            @Display(contentItem.ContentManager.BuildDisplay(contentItem, "Detail"))
        }
    }
    else {
        <span class="value">@T("No content items.")</span>
    }
</p>

如果您想直接从内容选择器字段中访问酒店,您需要在视图中添加更多内容,有点乱,但是这样:

@using Orchard.ContentPicker.Fields
@using Orchard.Utility.Extensions;

@{
    var field = (ContentPickerField) Model.ContentField;
    string name = field.DisplayName;
    var contentItems = field.ContentItems;
}
<p class="content-picker-field content-picker-field-@name.HtmlClassify()">
    <span class="name">@name</span>
    @if(contentItems.Any()) {
        foreach(var contentItem in contentItems) {
            var container = contentItem.As<ContainerPart>();
            if(container == null) {
                continue;
            }

            var hotels = contentItem.ContentManager
                .Query(VersionOptions.Published)
                .Join<CommonPartRecord>().Where(x => x.Container.Id == container.Id)
                .Join<ContainablePartRecord>().OrderByDescending(x => x.Position);

            foreach(var hotel in hotels) {
                // do something
            }
        }
    }
    else {
        <span class="value">@T("No content items.")</span>
    }
</p>

我在顶部错过了一些使用语句,但其中的要点就在那里。