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);
        }
    }
}

Umbraco 7 Note 1

Umbraco についてです。メモとして描いていたので内容は部分的です。

Umbraco で登場する用語

  • Dashboards
    • http://hoge/umbraco でアクセスできる管理画面
  • Umbraco API
    • Version 6 から登場した Umbarco のデータを操作できる API
  • Back Office
    • Umbraco の構成要素

Back Office

C# で例えると、Document Type : クラス、Content : クラスから作成したインスタンス、Properies : クラスがもつ Property、Data Type : Property の型、Property Editors : Property の型の構造(string, int, Custom Class の中身の実装)。Umbraco でサイトを作成するには、Document Type を定義し、Properties を定義し、Content を作成してページを作る。

  • Document Type
    • ページの構造。
    • 例: Blog という Document Type は、 Title, BodyText, PublishDate, という Properties を持つ。
  • Content
    • ページ。
    • 例: Document Type の "Blog" から 2011/11/10 の記事 を作成する。(= Content を 1 つ作成した)
    • 例: Document Type の "Blog" から 2011/11/30 の記事 を作成する。(= Content を 1 つ作成した)
  • Properties
  • Data Type
  • Property Editors
  • Media Type
  • Media
  • Member Type
  • Member
  • Node
  • User
  • Macros
  • Macro Parameter Editor
  • Templates/Layouts/Masterpages
  • Packages

参照

未分類

Document Type

Document Type は継承することができる。この Document Type の継承は、"Property" や "Tab" を継承したい(同じ構造を持たせたい) 、という目的の時に使用する。とあるページがとあるページの下に配置されるからといって、Document Type を継承しなければいけない、ということではない。 (http://hoge/blog の下に http://hoge/blog/entry というページを配置する場合、必ずしも entry の Document Type は blog の Document Type を継承しなければいけない、ということではない。)

Document Type Compositions

Umbraco 7.2 から追加された機能。Document Type を複数継承することができるようになった。

今までは、継承する Document Type は一つしか選択できず、また継承元を変更することもできなかった。Document Type Compositions により複数の Document Type を継承元として選択できるようになり、変更も可能になった。

例えば、複数の Document Type に "Meta Keyword" という Property を持たせたい場合は、"SEO" という Document Type を作成して "Meta Keyword" という Property を持たせる。そして、例えばページという Document Type にて Document Type Compositions で "SEO" にチェックを入れる。そうすると、ページの Document Type に "Meta Keyword" という Property を持たせることができる。そして、ページは、他の Document Type も継承することができるので、気軽に Document Type を小さくまとめて定義できる。

なお、Dashboards の Document Type では、今まで通りのツリー構造で表示される。

Umbraco 7.2 Compositions

Alt Template

実際に選択している Template とは別のテンプレートを適用して表示することができる機能。URL に ?alttemplate=hoge ( hoge に Template の Alias) を付けて表示することができる。

注意として、意図しないページを表示させたり、余計なエラーを発生することになる。

この機能を禁止する場合は、 config/umbracoSettings.config の disableAlternativeTemplates に false を設定する。

<web.routing
    trySkipIisCustomErrors="false"
    internalRedirectPreservesTemplate="false" disableAlternativeTemplates="true">
</web.routing>
Custom Error Page

Custom Error ページの Content を用意する config/umbracoSettings.config にて、Content Id を設定

<errors>
  <error404>1732</error404>
</errors>

Web.config の system.webserver に

<httpErrors existingResponse="PassThrough"/>

ASP.NET の通常のエラー設定も必要に応じて。

<customErrors mode="RemoteOnly" defaultRedirect="~/Error.aspx"/>