2013-02-15 31 views
6

Tôi gặp sự cố với mô-đun xuất nhập khẩu trong Orchard 1.6:
Tôi muốn xuất loại tùy chỉnh với một phần có thuộc tính trong đó. XML xuất chứa dữ liệu từ TitlePart, CommonPart, BodyPart và AutoroutePart, tuy nhiên dữ liệu từ phần của riêng tôi không có ở đó.Thuộc tính phần tùy chỉnh bị thiếu trong xuất khẩu Orchard 1.6

Có điều gì tôi nên làm như triển khai giao diện hoặc ghi đè điều gì đó trên phần của mình để nó được chứa trong XML xuất không? (Nếu có) các điểm mở rộng của mô-đun xuất khẩu là gì? Tôi có nguồn của mô-đun nhưng không thể tìm thấy nó.

Các module.txt của module xuất khẩu đặc biệt là:
Tên: Xuất nhập khẩu
Đường dẫn: ImportExport
AntiForgery: kích hoạt
Tác giả: The Orchard Đội
Website: http://orchardproject.net
Version: 1.6
OrchardVersion: 1.4
Mô tả: Cung cấp khả năng nhập và xuất dữ liệu mục nội dung.
FeatureDescription: Nhập khẩu và xuất khẩu dữ liệu nội dung mục
Thể loại: Nội dung

Cảm ơn trước :)

Trả lời

7

Bạn cần phải ghi đè theExporting/Nhập khẩu phương pháp trong trình điều khiển phần nội dung của bạn. Dưới đây là một ví dụ đơn giản từ Orchard.Core.Title.Driver.TitlePartDriver:

protected override void Importing(TitlePart part, ImportContentContext context) { 
    var title = context.Attribute(part.PartDefinition.Name, "Title"); 
    if (title != null) { 
     part.Title = title; 
    } 
} 

protected override void Exporting(TitlePart part, ExportContentContext context) { 
    context.Element(part.PartDefinition.Name).SetAttributeValue("Title", part.Title); 
} 

Lớp ImportExportContext cung cấp quyền truy cập vào các cấu trúc XML cơ bản sử dụng để tạo ra các tài liệu đầu ra, vì vậy nếu bạn đang sử dụng để sử dụng System.Xml.Linq, XDocument vv sau đó nó tất cả sẽ có vẻ quen thuộc.

Có một số ví dụ khác về cách sử dụng trong Orchard.Core.Common.Drivers.CommonPartDriver, Orchard.Users.Drivers.UserPartDriverOrchard.Comments.Drivers.CommentPartDriver.

+0

Cảm ơn @mdm. Điều này hoạt động hoàn hảo và chính xác là những gì tôi cần – Ytrog

0

Bạn có thể thử sử dụng phản ánh:

 string[] notRequiredExportProperties = new string[] 
      { 
       "Record", 
       "ContentItem", 
       "Zones", 
       "Id", 
       "TypeDefinition", 
       "TypePartDefinition", 
       "PartDefinition", 
       "Settings", 
       "Fields" 
      }; 

     protected override void Importing(ContactPart part, Orchard.ContentManagement.Handlers.ImportContentContext context) 
     { 
      var contactRecordType = part.Record.GetType(); 
      var allProps = contactRecordType.GetProperties(); 
      foreach (PropertyInfo p in allProps) 
      { 
       if (Array.FindIndex(notRequiredExportProperties, i => i == p.Name) > -1) 
        continue; 

       var importValue = context.Attribute(part.PartDefinition.Name, p.Name); 
       var import = Convert.ChangeType(importValue, p.PropertyType); 
       if (p.PropertyType.IsSubclassOf(typeof(Enum))) 
        continue; 
       p.SetValue(part.Record, import, null); 
      } 
     } 

     protected override void Exporting(ContactPart part, Orchard.ContentManagement.Handlers.ExportContentContext context) 
     { 
      var contactPartType = part.GetType(); 
      var allProps = contactPartType.GetProperties(); 
      foreach(PropertyInfo p in allProps) 
      { 
       if (Array.FindIndex(notRequiredExportProperties, i => i == p.Name) > -1) 
        continue; 

       var propVal = p.GetValue(part, null); 
       context.Element(part.PartDefinition.Name).SetAttributeValue(p.Name, propVal); 
      } 
     } 

Bạn có thể phải làm thêm một chút để làm cho nó hỗ trợ Enums, vv này có lẽ nên được trong một lớp helper của một số loại.

Các vấn đề liên quan