iT邦幫忙

2017 iT 邦幫忙鐵人賽
DAY 12
0
自我挑戰組

30天打造自己的 Umbraco CMS 系列 第 12

Day 12 - Umbraco - Events (1) ContentService Events (更)

今天先突然跳過來講 Umbraco Events,因為這幾天都在研究他

Umbraco Events

Umbraco 採用 .NET 事件讓開發者可以在整個流程中可以介入,
今天要談的 ContentService Events 算是蠻常被用到的,
對Content的 Save,Publish,UnPublish,Delete,Move,Copy,Trash,RollBack等,
都可以藉由 hook event的方式來做適當的判斷、處理、攔截等等。

每個事件都有分 before,after,看事件名稱為 inged 就可略知一二,
希望某件事情不要被執行,就會在 Action + ing 的 event 裡面做cancel,
希望某件事情做完後,要接著做其他額外的商業邏輯或是資料處理就會在 Action + ed 裡處理。

不過可藉由輸入參數的方式達到不進入這些事件。(後面會講到)

ContentService https://our.umbraco.org/documentation/Reference/Events/ContentService-Events

ContentService

在 Content Section 裡面不管增刪修都會有對應的事件可以介入,以下來大略說明幾個比較常用的


Saving,Saved

(IContentService sender, SaveEventArgs<IContent> e)
e.SavedEntities: Gets the collection of IContent objects being saved.

IContent 藉由 Umbraco 後台進行 新增、儲存,
又或者透過 Umbraco C# API 呼叫 ContentService.Save()
都會觸發此 event。

e.SavedEntities 可以取得觸發 Saving,Saved event 的 IContent List,
可以用 foreach 去讀取每個 Content 並對其做判斷如 敏感字詞檢測等。

如果我們發現 Content中包含一些我們不希望存在或是有疑慮的東西,
那可以在 Saving event 中 設定 e.Cancel = true,來阻止 Content 被寫入資料庫。

這裡要注意的是 Saved event 不能使用 e.Cancel = true 會報錯

Saved event 中,我們可以寫些如存檔後自動去建立對應的資料夾放附件、在客製資料表建一筆資料等等。

在 Umbraco 事件裡面,儲存( Save )跟發布 ( Publish/UnPublish )是要分開看的,

  • 儲存主要是針對 Content 內容進行存檔
  • 發布則是決定前台是否可以看到,正常來講只有 Publish 的 Content 才能在前台被看到。

Publishing,Published

(IPublishingStrategy sender, PublishEventArgs<Umbraco.Core.Models.IContent> e)
e.PublishedEntities Gets the collection of IContent objects being published.

如果我們要擷取 Content 發布前做一些檢查如 無效連結檢測,內容檢測等等,
就可以 hook 這兩個 event 來處理。

Publishing 階段

Content 還沒被真的被改成發布狀態,所以要攔截並取消發布就是在這做。

Published 階段

Content 已經被改為發布狀態,並且會將內容寫入XML供前台使用。
又或者在發布 Content後要自動寄信通知訂閱者的話,也可以註冊在此 event中。

儲存並發布的按鈕會怎麼觸發這幾個事件呢?

觸發順序為 Saving => Publishing => Saved => Published 之後會用到


Copying,Copied

IContentService sender, CopyEventArgs<IContent> e
e.Copy:Gets the IContent object being copied.
e.Original:Gets the original IContent object.
e.ParentId:Gets the Id of the parent of the IContent being copied.

在兩個不同的 Document type那邊設定 Permissions 設定有相同的Child時,
就能利用 Umbraco 提供的 Copy 以及下面的 Move 功能進行複製及移動的操作。

這裡要注意的是 Copy 分成兩種,

  • 只複製自己
  • 複製自已並包含底下所有子節點

依據不同的情況善用此功能的話,可以省下很多時間,
如果在複製的過程中有特殊需求,可能是哪幾個東西一定不能複製,
又或者哪些東西要從其他地方抓進來的話,就可以利用這幾個 events 來達到效果。


Moving,Moved

(IContentService sender, MoveEventArgs<IContent> e)
e.Entity: Gets the moved IContent object.
e.ParentId: Gets the Id of the parent of the IContent moved.

跟上面的 Copy 很類似, 一樣是要兩個 Document type 設定有相同的 Child時才能進行移動。


Trashing,Trashed

(IContentService sender, MoveEventArgs<IContent> e)
e.Entity: Gets the trashed IContent object.
e.ParentId : Gets the Id of the RecycleBin.

Umbraco 的刪除分成兩種,跟 Windows 系統類似,

  • 將資料丟到資源回收桶 (Delete)
  • 將資料從資源回收桶刪除 (Trash)

被丟到資源回收桶的東西能夠被復原到原本的位置,相對的他也會佔系統的容量。
而從資源回收桶刪除時,就會呼叫到 Trashing,Trashed events


Deleting,Deleted

(IContentService sender, DeleteEventArgs<IContent> e)
e.DeletedEntities : Gets the collection of IContent objects being deleted.

在 Content Section 中,我們對 Content 進行 Delete動作時,
只是將該 Content 以及底下的子節點丟入資源回收桶中,並不是實際刪除掉。
所以可以進行復原的動作,有些東西或許想 Delete 就要達到真正刪除效果時,
就 hook Deleted 後,呼叫 ContentService 真正刪掉他。


SendingToPublish,SentToPublish

(IContentService sender, SendToPublishEventArgs<IContent> e)
e.Entity : Gets the IContent object being sent to publish.

這個事件跟 Publish 其實做的事情一樣,但是觸發的情況不同,
因為不是每個使用者都有 Publish 的權限。
所以當使用者沒給權限時但又必須要將 Content 發布時,
可以透過 Umbraco C# API 呼叫 SendToPublish 來達到此效果


UnPublishing,UnPublished

(IPublishingStrategy sender, PublishEventArgs<Umbraco.Core.Models.IContent> e)
e.PublishedEntities Gets the collection of IContent objects being published.

這兩個是官方文件沒寫的隱藏版事件,
在我們將 Content 取消發布時會被觸發,
這裡有趣的是,取消發布的情況下,並不會觸發 Saving,Saved event
所以也代表你在畫面上操作的所有變更,如果沒有先儲存就直接按 'UnPublish' 的話。
恭喜你,所有異動都沒有被存入,只有 Content 從 Publish 改變成 UnPublish 狀態


今日結論

我們今天講到 Umbraco Events 中最常被使用到的 ContentService Events。
當然整個 Umbraco 事件中還有很多很多事件如

  • MediaService Events
  • ContentTypeService Events
  • DataTypeService Events

之後會再慢慢介紹到的~

這幾天在研究怎麼強制不讓 Content 被 UnPublish,發現很像有點小麻煩啊...


上一篇
Day 11 - Umbraco - Data Type (1)
下一篇
Day 13 - Umbraco - Media
系列文
30天打造自己的 Umbraco CMS 18
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言