Custom Fields

Håkan Edling edited this page May 25, 2018 · 4 revisions

The IField interface

In order to create a custom field your class has to implement the IField interface from the Piranha.Extend namespace.

GetTitle

string GetTitle()

Gets the list item title if this field is used in a collection region.

Init

void Init(IApi api)

Initializes the field for client use. This is called when the field is loaded and is used for loading additional resources. As an example, the ImageField loads the Id of the referenced image from the database, then when Init() is called it loads the referenced media object.

Create the field

Let's take a look at how a simple string field could be implemented.

using Piranha;
using Piranha.Extend;

[FieldType(Name = "Custom String")]
public class CustomStringField : IField
{
    public string Value { get; set; }

    public string GetTitle() {
        return Value;
    }

    public void Init(IApi api) {
        // Nothing special for this field
    }
}

Register the field

All available fields has to be registered in the singleton Piranha.App after the app has been initialized.

Piranha.App.Init(api);

// Register custom fields
Piranha.App.Fields.Register<CustomStringField>();

Create the manager view

The manager interface is based on EditorTemplates for each field. All views for fields should be placed in:

~/Areas/Manager/Views/Shared/EditorTemplates

with the same name as the field class, in this example it should be named CustomStringField.cshtml.

@model CustomStringField
@Html.TextBoxFor(m => m.Value, new { @class = "form-control" })

A note on value serialization

As default, all fields are serialized as JSON to the database, however you can choose to serialize your data in a different way, or maybe even store it in a separate table in the database. The easiest way to do this is the register a custom serializer for your field. You can read more about this in Serializers.

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.