Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: MOTION-157

...

Create your own fields when the default fields do not meet your requirements. Magnolia fields are implemented as Vaadin components. Start by looking through the Vaadin sampler. When you find a matching field, implement it as a simple Vaadin Field in Magnolia. If you need a more complex field that is a composition of more than one simple field, look at the composite field or implement a Vaadin CustomField.

personaSwitcher is an example of a custom field. Editors can quickly switch between personas while previewing the page.

Table of Contents

Required classes

Each field needs the following classes:

Definition class

A definition class defines the field. It typically reads properties from the field definition. The definition class must implement the 

Javadoc resource link
classNameinfo.magnolia.ui.form.field.definition.FieldDefinition
renderTypeasynchronous
interface.

Factory class

A factory class creates and initializes Vaadin fields based on the field definition.

For example,

Javadoc resource link
classNameinfo.magnolia.ui.form.field.factory.TextFieldFactory
renderTypeasynchronous
creates a text field. It contains logic to react to the definition properties. If the rows property equals 1 the factory creates a single-line input element. If rows is more than 1 the factory creates a multiline text area. Factory classes extend 
Javadoc resource link
classNameinfo.magnolia.ui.form.field.factory.AbstractFieldFactory
renderTypeasynchronous
which can handle all common field properties.

Field class

A field class is only needed if you cannot find a single Vaadin field that meets your requirements. Simple fields such as Text do not have a field class - the factory class simply creates the matching Vaadin field directly. However, more complex fields need a field class.

For example, Link is a compound of a text box and a button. Vaadin doesn't provide this combination out of the box, so we need a

Javadoc resource link
classNameinfo.magnolia.ui.form.field.LinkField
renderTypeasynchronous
class that extends Vaadin CustomField. The custom field class provides a layout with a text field on the left and a button on the right. It also registers a preview component that displays a thumbnail of the link target if defined.

The Link field link field is an example that provides all three classes:

  • info.magnolia.ui.form.field.definition.LinkFieldDefinition
  • info.magnolia.ui.form.field.factory.LinkFieldFactory
  • info.magnolia.ui.form.field.LinkField

Registering a field

Any module can register a new field in the fieldTypes node. Register your field in your own module.

If you add the field to the UI Framework module it becomes globally available. Here is an example how the UI Framework module registers the Text the text field.

You can configure field types in a YAML definition file inside a light module folder.

...

info.magnolia.ui.form.field.factory.TextFieldFactory
Localtab
activetrue
titleFile
Code Block
languagejs
title<my-module>/fieldTypes/<field name>.yaml
definitionClass: info.magnolia.ui.form.field.definition.TextFieldDefinition
factoryClass: 
info.magnolia.ui.form.field.factory.TextFieldFactory
Localtab
titleTree
Advanced Tables - Table Plus
heading0
enableHeadingAttributesfalse
enableSortingfalse
classm5-configuration-tree
enableHighlightingfalse
Node nameValue

Mgnl f
modules

Mgnl f
ui-framework

 

Mgnl f
fieldTypes

Mgnl n
text

Mgnl p
definitionClass           

info.magnolia.ui.form.field.definition.TextFieldDefinition

Mgnl p
factoryClass

Using a field in a form

Once the field is registered, add it to a dialog or form by referencing the field definition class in the class property or by referencing the field alias name in the fieldType property. See Field definition for more.

If you create a complex field and need to store values in the repository in a specific way, see Transforming field values.