开发者

N2: Set default values for ContentItems

开发者 https://www.devze.com 2023-01-22 16:42 出处:网络
When using N2 CMS: If I want to set some default values when a new Conten开发者_如何学PythontItem is created (e.g. setting the CreatedByUser value for a new Page so I can record who originally create

When using N2 CMS:

If I want to set some default values when a new Conten开发者_如何学PythontItem is created (e.g. setting the CreatedByUser value for a new Page so I can record who originally created it) where is the best place to put that code?

I figure the constructor of the ContentItem isn't ideal because that will get called when existing objects are loaded.


If you're using the Get/SetDetail syntax then you can do something like this in the property getter:

public virtual string TopImage
{
    get { return (string)(GetDetail("TopImage") ?? string.Empty); }
    set { SetDetail("TopImage", value); }
}

That's a bit ugly, so there's also an overload for Get/Set detail that lets you specify the default:

public virtual string TopImage
{
    get { return GetDetail("TopImage", String.Empty /* Default */); }
    set { SetDetail("TopImage", value, String.Empty /* Default */); }
}

If you want to save a value when something is saved then try overriding the AddTo method on the ContentItem. This is called every time the object is saved, so be careful if you only want to call it the first time something is saved (ID == 0 when an Item is "new")

0

精彩评论

暂无评论...
验证码 换一张
取 消