More on Content Types

  Written by The Jahia Team
   Estimated reading time:


In this topic, you will learn more about content types definitions. As a Jahia developer, you'll be able to use several options in the content properties that you define, to improve the experience of end users in Content Editor:

  • Defining a helper for your property
  • Setting custom validations and related custom messages
  • Adding constraints for the dates
  • Setting a property as mandatory
  • Adding a default value for your property
  • Initializing choicelist (dropdown fields) with the right values

 Before diving in these options, you'll need to understand the selectors that are available for your properties. 

Selectors

Depending on the property type, a corresponding input field will be provided. It is possible to override the default input field by adding a specifier after the type definition.

Usage / syntax

- field-name (type)
- field-name (type,selector)
- field-name (type,selector[options])

where

  • type is one of : string, long, double, decimal, path, uri, boolean, date, binary, weakreference
  • selector is one of : text, richtext, textarea, choicelist, datetimepicker, datepicker, picker, category, checkbox
  • options are relative to the selector, you can chain them using a coma as separator. Example : [option1, option2]

Default selectors for data types

  • for string, long, double, decimal, path, uri : will display an input text field on a single line
  • for boolean : will display a checkbox
  • for date : will display a date picker
  • for weakreference : will display a selector

Custom selectors

You can change the default display of a screen

  • text : will display a single line form
  • richtext : will display ckeditor
  • textarea : will display a multiline area
  • choicelist : will display a drop down list with values define by the default value set or options. The choiceLists initializers defined by default are : resourceBundle, country, templates, templatesNode, users, nodetypes, subnodetypes, nodes, menus, script, flag, sortableFieldnames, moduleImage, linkerProps, workflow, workflowTypes, sort and componenttypes.
    - firstField (string, choicelist[sortableFieldnames]) < 'true', 'false'
    - play (string, choicelist[resourceBundle]) < 'true', 'false'
    - templateForLink (string, choicelist[templates,resourceBundle])
    - nodeType (string, choicelist[subnodetypes = 'jnt:page,jnt:contentList,jnt:folder',resourceBundle])
  • datetimepicker : will display a single line field and a calendar to select date and time
  • datepicker : will display a single line field and a calendar to select date
  • picker : will display a picker, depending of the option. The picker types defined by default are : file, image, folder, contentfolder, portlet, page, editorial, editoriallink, category, site, user and usergroup
    - source (weakreference, picker[type='file'])
    - image (weakreference, picker[type='image'])
    - j:node (weakreference, picker[type='contentfolder'])
  • category : will display a category picker
  • checkbox : will display a checkbox

Available pickers

filepicker (weakreference, picker [ type = 'file' ])
imagepicker (weakreference, picker [ type = 'image' ])
folderpicker (weakreference, picker [ type = 'folder' ])
contentfolderpicker (weakreference, picker [ type = 'contentfolder' ])
editorialpicker (weakreference, picker [ type = 'editorial' ])
editoriallinkpicker (weakreference, picker [ type = 'editoriallink' ])
categorypicker (weakreference, picker [ type = 'category' ])
sitepicker (weakreference, picker [ type = 'site' ])
userpicker (weakreference, picker [ type = 'user' ])

Helpers

You may want to add description or helper text to your field, to give some additional information to the user, for exemple here is a Job offer content, that contains a "skills" property. We expect the contributor to fill this multiple field with the required skills for the job.

The definition look like this:

[qant:jobOffer] > jnt:content, qamix:qaContent, jmix:editorialContent
 - skills (string, text) i18n multiple

And the generated form look like this:

Content-Editor-helper-2.png

It's possible to add a custom internatiomalized helper for this field using resource bundle:

qant_jobOffer.skills = Required skils
qant_jobOffer.skills.ui.tooltip = List the required skills for this job offer

And this will result in displaying the tooltip message for this field:

Content-Editor-helper-3.png

Regular expressions validation and custom error messages

If you need to control and validate that a text field for a given property have a specific format you can use regexp constraint.

[qant:jobOffer] > jnt:content, qamix:qaContent, jmix:editorialContent
 - skills (string, text) i18n multiple
 - contactMail (string, text) mandatory < '^$|[A-Za-z0-9._%+-]+@(?:[A-Za-z0-9-]+\\.)+[A-Za-z]{2,}'

In the previous definition we created new property "contactMail" with a regexp constraint.

By default the form will show this error message if the text field is invalid:

regexp-valdiation-error.png

But you can customize this message using resource bundle:

qant_jobOffer.contactMail = Contact mail
qant_jobOffer.contactMail.constraint.error.message = Please provide a valid email

This will display your internationalized message as an warning:

regexp-validation-custom.png

And the warning message will automatically disapear as soon as the text field value will match the regexp.

Date property constraints

It's possible to bound the date value using date boundary constraints in your definitions.

Samples are better than explainations, you can see in this definitions all type of date boundary constraints:

[qant:dateConstraint] > jnt:content, qamix:qaContent, jmix:editorialContent
   - dateConstraintLowerBoudaries (date, DatePicker) < "(2019-06-04T00:00:00.000,)"
   - dateConstraintUpperBoudaries (date, DatePicker) < "(,2021-06-20T00:00:00.000)"
   - dateConstraintBothBoudaries (date, DatePicker) < "(2019-06-04T00:00:00.000,2021-06-20T00:00:00.000)"
   - dateConstraintLowerBoudariesInc (date, DatePicker) < '[2019-06-04T00:00:00.000,]'
   - dateConstraintUpperBoudariesInc (date, DatePicker) < '[,2021-06-20T00:00:00.000]'
   - dateConstraintBothBoudariesInc (date, DatePicker) < '[2019-06-04T00:00:00.000,2021-06-20T00:00:00.000]'
   - datetimeConstraintLowerBoudaries (date, DateTimePicker) < '(2019-06-04T05:00:00.000,)'
   - datetimeConstraintUpperBoudaries (date, DateTimePicker) < '(,2021-06-20T05:10:00.000)'
   - datetimeConstraintBothBoudaries (date, DateTimePicker) < '(2019-06-04T05:10:00.000,2021-06-20T05:10:00.000)'
   - datetimeConstraintLowerBoudariesInc (date, DateTimePicker) < '[2019-06-04T05:00:00.000,]'
   - datetimeConstraintUpperBoudariesInc (date, DateTimePicker) < '[,2021-06-20T05:10:00.000]'
   - datetimeConstraintBothBoudariesInc (date, DateTimePicker) < '[2019-06-04T05:10:00.000,2021-06-20T05:10:00.000]'
  • You can delimit your date value using a minimal boundary (always the first one) or maximal boundary (always the second one) or both 
  • The boundary always need to be separate by a comma, even if you only use one of them.
  • You can specify if the boundary is included using [brackets] or if the boundary is excluded by using (parentheses)
  • You can use this constraints on both DatePicker and DateTimePicker

If you decide to use this boundaries, the date picker will only allow the user to select a valid date:

date-picker-1.png

If the user enter a not allowed date, then a validation warning will be displayed automatically:

date-picker-2.png

Mandatory properties

It's possible to set some properties mandatory, sometime it make sense to force your contributor to fill mandatory properties.

To do this you can add the keyword "mandatory" in your definition.

[qant:allFieldsRequired] > jnt:content, qamix:qaContent, jmix:editorialContent
   - sharedSmallText (string) mandatory
   - smallText (string) i18n mandatory
   - sharedTextarea (string, textarea) mandatory
   - textarea (string, textarea) i18n mandatory
   - sharedChoicelist (string, choicelist[resourceBundle]) = 'choice1' mandatory < 'choice1', 'choice2', 'choice3'
   - choicelist (string, choicelist[resourceBundle]) = 'choice1' i18n mandatory < 'choice1', 'choice2', 'choice3'
   - sharedLong (long) mandatory

Every kind of property can be mandatory, even multiple valued properties. They will be displayed like this in the editor:

mandatory-prop-1.png

And the contributor will not be able to save until he provide a value for this mandatory properties:

mandatory-prop-2.png

Default values

It's possible to provide default values to your properties, if you do so it will prefill the editor with your configured default values.

Almost every type of property can have default values, even multiple values, the only exception is the reference properties.

note: default values will only be used to prefill the editor when you are creating a new content, but not when you are editing existing contents.

Default values for single valued properties

[qant:AllFieldsDefault] > jnt:content, qamix:qaContent, jmix:editorialContent, jmix:defaultPropMixin
    - sharedSmallText (string) = 'value 1'
    - sharedTextarea (string, textarea) = 'value 1'
    - sharedChoicelist (string, choicelist[resourceBundle]) = 'choice1' < 'choice1', 'choice2', 'choice3'
    - sharedLong (long) = 1
    - sharedDouble (double) = 1.1
    - sharedBoolean (boolean) = true
    - sharedBigtext (string, richtext) = 'value 1'
    - sharedDate (date) = '2019-06-04T00:00:00.000'
    - sharedDecimal (decimal) = 1234567890.123456789

Will display:

default-values-1.png

Default values for multiple valued properties

[qant:allFieldsMultipleDefault] > jnt:content, qamix:qaContent, jmix:editorialContent
   - sharedChoicelist (string, choicelist[resourceBundle]) = 'choice1' multiple < 'choice1', 'choice2', 'choice3'
   - dynamicChoicelist (string, tag[autocomplete=10,separator=',']) = 'tag1', 'tag2' facetable nofulltext multiple
   - sharedSmallText (string) = 'test1=1', 'test2=2' multiple < '[a-zA-z1-9]*=[a-zA-z1-9]*'
   - sharedTextarea (string, textarea) = 'value1', 'value2' multiple
   - sharedLong (long) = 1, 2 multiple
   - sharedDouble (double) = 1.1, 2.2 multiple
   - sharedBoolean (boolean) = true, false multiple
   - sharedBigtext (string, richtext) = 'value 1', 'value 2' multiple
   - sharedDate (date) = '2019-06-04T00:00:00.000', '2029-06-04T00:00:00.000' multiple
   - sharedDecimal (decimal) = 1234567890.123456789, 1134567890.113456789 multiple

Will display:

default-values-2.png

Restrict content type creation using permissions and roles

It's possible to only allow some conte type to be created by your users using permissions and roles.

Inside the Jahia Administration in the role administration page, you can select or unselect the node types sections you want to allow for a given role.

This permissions are available on "Current site" and "Templates and Components":

component-permissions.png

Every permissions listed here correspond to a mixin and section used to display the content type in the content type tree selector.

for exemple:

[qant:jobOffer] > jnt:content, jmix:basicContent, jmix:editorialContent
 - skills (string, text) i18n multiple
 - contactMail (string, text) mandatory < '^$|[A-Za-z0-9._%+-]+@(?:[A-Za-z0-9-]+\\.)+[A-Za-z]{2,}'

Is displayed under basic content section, because of the mixin inheritance on "jmix:basicContent":

tree-selector-content-type.png

If you disable the permission "Component jmix basic content" for role "editor".

Every user using the role "editor" on the current site will not be able to create any content of the "jmix:basicContent" section, including our jobOffer content type.

Choicelists

It's possible to provide data set to choicelist in multiple ways. 

You can provide the available values directly in the definition:

[qant:choicelist] > jnt:content, qamix:qaContent, jmix:editorialContent
 - resourceBundle (string, choicelist[resourceBundle]) = 'choice1' < 'choice1', 'choice2', 'choice3'

Then you can provide a translation for each values using the resourceBundle option, and provide the internationalized name using resource bundle files:

qant_choicelist.resourceBundle = Resource bundle
qant_choicelist.resourceBundle.choice1 = Choice one
qant_choicelist.resourceBundle.choice2 = Choice two
qant_choicelist.resourceBundle.choice3 = Choice three

This will display:

choicelist-1.png

Choicelist initializers

You can use choicelist initalizers to provide dynamic data set, Jahia provides some of them ready to be reused.

The user choicelist

[qant:choicelist] > jnt:content, qamix:qaContent, jmix:editorialContent
 - users (string, choicelist[users])

Will display the list of users available on the server:

choicelist-2.png

The country choicelist

[qant:choicelist] > jnt:content, qamix:qaContent, jmix:editorialContent
 - country (string, choicelist[country])

Will display a list of countries name:

choicelist-3.png

The node type choicelist

[qant:choicelist] > jnt:content, qamix:qaContent, jmix:editorialContent
    - j:nodeTypes (string,choicelist[nodetypes=nt:base])

Will display a list of all available nodetypes that inherits from the nodetypes option:

choicelist-4.png

The node choicelist

[qant:choicelist] > jnt:content, qamix:qaContent, jmix:editorialContent
 - category (weakreference, choicelist[nodes='/sites/systemsite/categories/;jnt:category'])

This one is a little bit complex, but very useful, you can provide a path and a node type and it will list the available nodes that correspond, you will be able to store the result in weakreference directly.

In the current exemple, it's a way to list some categories and store the choosen one in a property named "category":

choicelist-5.png

Note: this can be useful, because you can setup the contents you want to display in the list, by choosing the path and the node type.

But in case you want to categorize you content you just have to enable the fieldset already available in the editor:

 

category-1.png

Then select the categories you want to add to your content:

category-2.png

More About choicelists

Choicelist initializers can be extended, and you can create your own ones, if you want to know more about choicelist initializers, please read: 

Choicelist initializers documentation