2017-05-04 57 views
1

這是Content.ThumbnailSummary.cshtml的標記,它是我用來將ContentItems呈現爲可點擊縮略圖的自定義DisplayType,其內容絕對位於它們之上。Orchard是否可以在渲染之前動態更改形狀中的HTML?

@using Orchard.Utility.Extensions; 
@{ 
    var contentTypeClassName = ((string)Model.ContentItem.ContentType).HtmlClassify(); 
} 
<a class="content-item @contentTypeClassName thumbnail-summary"> 
    @Display(Model.Header) 
    <div class="thumbnail-summary-inner"> 
     @Display(Model.Content) 
    </div> 
    @Display(Model.Footer) 
</a> 

的問題是,開箱即用的大部分地區和領域得到呈現爲鏈接或包含鏈接的段落,並嵌套<a>標籤搞亂DOM渲染在大多數瀏覽器相當嚴重。 ThumbnailSummary應該永不包含任何鏈接。

我可以爲每個字段和部分創建替代字段,或者我可以在放置時刪除所有內容,並且只根據需要爲特定案例添加規則。但是這將非常繁瑣,並且失去了很多放置的好處,所以我希望我能夠以某種方式去掉或替換代碼中的所有<a>標籤,僅用於具有此DisplayType的形狀。

我一直在尋找在這個方向上,但我不知道這是否是可行的:

public class Shapes : IShapeTableProvider 
{ 
    public void Discover(ShapeTableBuilder builder) 
    { 
     builder.Describe("Content") 
      .OnDisplaying(displaying => 
      { 
       if (displaying.ShapeMetadata.DisplayType == "ThumbnailSummary") 
       {    
        // Do something here??? 
       } 
      }); 
    } 
} 

回答

1

您是差不多吧,而不是供應商的加入,從Orchard.DisplayManagement.Implementation.ShapeDisplayEvents繼承的類或實現IShapeDisplayEvents自己。

我這樣做是爲了從管理區域刪除某些功能,而這些功能無法通過功能或權限禁用。

的代碼看起來應該是這樣

public class MyShapeDisplayEvents : Orchard.DisplayManagement.Implementation.ShapeDisplayEvents 
{ 
    public override void Displayed(Orchard.DisplayManagement.Implementation.ShapeDisplayedContext context) 
    { 
    if (context.Shape is Orchard.DisplayManagement.Shapes.Shape) 
    { 
     Orchard.DisplayManagement.Shapes.Shape lShape = (Orchard.DisplayManagement.Shapes.Shape)context.Shape; 

     if (lShape.Metadata.Type == "Layout") 
     { 
     string lChildContent = context.ChildContent.ToHtmlString(); 

     // do something with the content like removing tags 

     context.ChildContent = new System.Web.HtmlString(lChildContent); 
     } 

     ...