Creating custom umbraco datatypes revisited Again 1
The upcoming umbraco Juno release (umbraco 4.6) will be introducing data editor settings these make it a lot easier to create custom datatypes that have settings (more details on the umbraco.org blog)
On this blog I’ve got a couple of posts that outline the changes in the creation of custom datatypes, looks like we’ve gone from 250 lines of code to 70 lines of code.
In these posts I’m always creating the same datatype: a textarea that has a limit on the number of characters, you can set the limit on the datatype settings
History
18/11/2008 - Creating custom umbraco datatypes
old school, 3 classes +- 250 lines of code
29/03/2009 - Creating custom umbraco datatypes revisited
introduction of abstract data editor ( 2 classes +- 200 lines of code)
Coming in umbraco Juno - Introduction of DataEditorSettings (single class +- 70 lines of code)
Using DataEditorSettings
Dynamic creation of the prevalue editor (settings editor), by simply marking a property with the DataEditorSetting property.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using umbraco.cms.businesslogic.datatype;
namespace DataEditorSettings.CustomDataEditorUsingDataEditorSettings
{
//using data editor settings
public class DataType : umbraco.cms.businesslogic.datatype.AbstractDataEditor
{
private CharlimitControl control = new CharlimitControl();
[DataEditorSetting("Limit",
description = "Maximum number of characters",
defaultValue = "250")]
public string Limit { get; set; }
// Set ID, needs to be unique
public override Guid Id
{
get
{
return new Guid("1BA9853C-8772-43A8-937B-E865B21DFDDA");
}
}
//Set name, (is what appears in data editor dropdown)
public override string DataTypeName
{
get
{
return "CharLimit (using data editor settings)";
}
}
public DataType()
{
//set rendercontrol
base.RenderControl = control;
//init event
control.Init += new EventHandler(control_Init);
//save event
base.DataEditorControl.OnSave +=
new umbraco.cms.businesslogic.datatype.AbstractDataEditorControl.SaveEventHandler(
DataEditorControl_OnSave);
}
void control_Init(object sender, EventArgs e)
{
control.Text = base.Data.Value != null ? base.Data.Value.ToString() : "";
control.Limit = string.IsNullOrEmpty(Limit) ? "250" : Limit;
}
void DataEditorControl_OnSave(EventArgs e)
{
base.Data.Value = control.Text;
}
}
}
Download the sample project to see the impact (you’ll have to be running a build of Juno from at least 19/11 if you want to test it , get it here http://nightly.umbraco.org/umbraco%204.6/4.6%20Alpha/ )