miso_soup3 Blog

主に ASP.NET 関連について書いています。

Umbraco 7 公開日の設定

Umbraco の Document Type には、作成日・編集日 は予め用意されているが、公開日は用意されていない。

そこで、公開日を設定したい場合は、Document Event を拡張し、コンテンツが公開された時のイベントを拡張し、 ”あるドキュメントが公開されたらその時の日付を Property にセットする”という実装をかく。

以下はその例のコード。 (このコードには、MarkdownでかかれたテキストをHtmlに変換する処理も混ざっている。)

プロジェクト配下に「Umbraco.Core.ApplicationEventHandler」を継承したクラスを配置する。

参照:

application-startup - Events - Reference - Documentation - our.umbraco.org

Document Events https://our.umbraco.org/documentation/reference/events/Document-Events (ちょっと古い)

using HogeProject.Umbraco.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Umbraco.Core;
using Umbraco.Core.Events;
using Umbraco.Core.Models;
using Umbraco.Core.Publishing;
using Umbraco.Core.Services;

namespace HogeProject.Umbraco.Extensions
{
    /// <summary>
    /// Umbraco の アプリケーション イベントをカスタマイズします
    /// </summary>
    /// <remarks>
    /// https://our.umbraco.org/documentation/Reference/Events/application-startup
    /// 
    /// Document Events https://our.umbraco.org/documentation/reference/events/Document-Events (ちょっと古い)
    /// </remarks>
    public class HogeProjectApplicationEventHandler : ApplicationEventHandler
    {
        protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            ContentService.Publishing += ContentService_Publishing;
            ContentService.Saving += ContentService_Saving;
        }

        /// <summary>
        /// Content Saving
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void ContentService_Saving(IContentService sender, SaveEventArgs<IContent> e)
        {
            foreach(IContent node in e.SavedEntities)
            {
                if (node.ContentType.Alias == "Article" || node.ContentType.Alias == "BlogEntry")
                {
                    //markdown で書かれた文章を変換しプロパティに格納します。
                    SetMarkdownParsed(node);
                }
            }
        }

        /// <summary>
        /// Content が Publish されようとするときに呼び出されます。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        void ContentService_Publishing(IPublishingStrategy sender, PublishEventArgs<IContent> e)
        {
            foreach (IContent node in e.PublishedEntities)
            {
                if (node.ContentType.Alias == "Article" || node.ContentType.Alias == "Subject")
                {
                    // 最初の公開日をプロパティに設定します。
                    SetFirstPublishedDate(node, "sinceSet");
                }
                else if (node.ContentType.Alias == "BlogEntry")
                {
                    // 最初の公開日をプロパティに設定します。
                    SetFirstPublishedDate(node, "firstPublishedDate");
                }
            }
        }

        /// <summary>
        /// プロパティに最初の公開日を設定します
        /// </summary>
        /// <param name="content"></param>
        private void SetFirstPublishedDate(IContent content, string propertyAlias)
        {
            //指定した値が null の場合のみ設定します

            if (content.HasProperty(propertyAlias) == false)
                return;

            //既に設定してある場合は設定しない
            if (content.GetValue(propertyAlias) != null)
                return;

            //初期データの場合は設定しない
            if (content.HasProperty("isInitialData") && content.GetValue<bool>("isInitialData"))
                return;

            content.SetValue(propertyAlias, DateTime.Now);
        }

        /// <summary>
        /// Markdown でかかれた本文を Html に変換し、プロパティに設定します。
        /// </summary>
        /// <param name="content"></param>
        private void SetMarkdownParsed(IContent content)
        {
            if (!content.HasProperty("bodyText") || !content.HasProperty("bodyTextParsed"))
                return;

            string bodyTextMarkdown = content.GetValue<string>("bodyText");
            string bodyTextHtml = MarkdownHelper.Transform(bodyTextMarkdown);

            content.SetValue("bodyTextParsed", bodyTextHtml);
        }
    }
}