Specifying Attribute Type Mappings

There are cases in which the default mapping is not suitable for the intended purpose of an attribute. The child_order attribute, for example, which is of the referencelist type, serves to sort navigations and is mapped to the linklist type. As a result, sorting a navigation creates links to each of its navigation points. Since it's rather cumbersome to delete these links, a mapping that does without consistency checking is preferable.

This is exactly what custom attribute types have been designed for. They let you change the mapping of attribute types.

Currently, only two alternative attribute mappings are available: referencelist attributes can be represented as text instead of linklist, and reference attributes can be represented as string instead of linklist attributes. This prevents the creation of links in the CMS, and no consistency checks are performed.

There are three ways to specify a different mapping.

1. Mapping across object classes

For mapping attributes by their name to a different type, the Fiona7.custom_attribute_types is available. It doesn't matter to which object classes these attributs belong; only their name counts.

A good candidate for this is the above-mentioned attribute for sorting a navigation, child_order:

Fiona7.custom_attribute_types = {
  child_order: :text
}

When mapping an attribute type by the name of the attribute, both this name and the alternative type need to be a Ruby symbol.

2. Object-class-specific mapping

Mappings specified by means of the Fiona7.custom_attribute_types configuration setting may also be defined for individual object classes. An example:

Fiona7.custom_attribute_types = {
  'MyPage' => {
    child_order: :text
  }
}

Object-class-specific attribute type mappings are only accepted in standalone mode.
The attribute name as well as the alternative type need to be a Ruby symbol. The object class name needs to be a string.

3. Dynamic mapping

For cases in which a static definition is not flexible enough, a dynamic approach is available: You can set the value of  Fiona7.custom_attribute_types to a lambda or a Proc with two parameters, the object class name and the attribute name. In the example below, the alternative mapping for child_order is restricted to object classes whose name ends with Page (or page):

Fiona7.custom_attribute_types = lambda do |obj_class, attribute|
  if obj_class =~ /Page$/i
    if attribute == :child_order
      :text
    end
  end
end

In legacy mode, an attribute needs to have the same (alternative) type across all object classes it has been assigned to.