Skip to content

Custom Field Attributes and FieldTypes

Arran France edited this page Jul 22, 2015 · 4 revisions

As mentioned in the Block Attributes section of the Blocks documentation, you can create your own FieldTypes for use in your own Blocks, Jobs, or anything that inherits from Rock.Attribute.IHasAttributes - provided you heed the following instructions.

  1. Create a FieldType class that inherits from Rock.Field.FieldType and implements the Rock.Field.IFieldType interface
  2. Create a FieldAttribute class that inherits from Rock.Attribute.FieldAttribute
  3. Register your custom FieldType in Rock

So if you had a Hologram field type class that was going to represent the <hologram> tag in HTML 9...

    public class Hologram : FieldType
    {
        //...
    }

Custom FieldType

You'll need to implement the IFieldType interface - handling things such as returning a string value that represents your object.

    public override string FormatValue( Control parentControl, string value,
        Dictionary<string, ConfigurationValue> configurationValues, bool condensed )
    {
        // The given "value" is what was stored in the repository.
        // Now construct the string that represents the <hologram> tag
        string[] values = value.Split( '|' );
        return string.Format( "<hologram id='{0}_hologram'><source type='hologram/mp9' src='{1}'/>
            </hologram>", values[0], values[1] );
    }

Custom FieldAttribute

The Rock Framework provides methods in the Rock.Attribute.Helper class to create, read, and update the attributes.

    public class HologramFieldAttribute : FieldAttribute
    {
        public HologramFieldAttribute( int order, string name, string description, bool required )
            : base( order, name, required, string.Empty, null, string.Empty, description )
        {
        }
    }

TODO: document this better (my brain hurts).

Register Your FieldType

You will need to add your custom field type to the FieldType entity collection or table either via SQL or some code. In particular, make sure you set the Name, Assembly, and Class.

    -- this is for illustration purposes only
    INSERT INTO [FieldType] ([IsSystem], [Name], [Description], [Assembly], [Class], [Guid])
    VALUES (0, N'Hologram', N'A Hologram field', N'com.mychurch.RockUtilsAssembly',
        N'com.mychurch.Rock.Field.Types.Hologram', 'abcdefff-fff-ffff-ffff-abcdefffffff')
Clone this wiki locally