Measurable Success «10 Checkpoints for Future-Proof Enterprise CMS» White Paper
Measurable Success - White Paper

provideObjClass(name, contentClassOrOptions)

Creates a page or resource type for the CMS, or registers an already existing class.

If a class is given, this function registers an unnamed CMS object class under the given name. Otherwise, Scrivito.provideObjClass generates an object class using the given schema.

Object classes serve as boilerplates for CMS objects. Their main purpose is to define the attributes the instances require, for example a string attribute named “title”, or a binary attribute for binary data like an image or a PDF document.

Scrivito.provideObjClass('StandardPage', {
  attributes: {
    body: 'widgetlist',
    title: 'string',
  },
});

Params

  • name (String) – The name of the CMS object class.
  • contentClassOrOptions (Class, Object) – Options for generating an object class (see below) or an already generated class that Scrivito should register.

Options

  • attributes (Object) – The attribute definition of the CMS object class. Either only the type, or an array consisting of the type, followed by options. Available options are:
    • values (Array) – List of values that can be assigned (applies to the enum and multienum types).
    • only (String or Array) –
      • With widgetlist attributes, optionally the name of a widget class (String) or a list of such names (Array). If specified, only widgets of the given class(es) can be placed into the widgetlist attribute.
      • With reference and referencelist attributes, optionally the name of an object class (String) or a list of such names (Array). If specified, only CMS objects of the given class(es) can be placed into the attribute.
  • extractTextAttributes (Array of strings) – The names and order of the attributes to be used by Scrivito.extractText. Default: []. The following attribute types are supported:
    • string
    • html
    • widgetlist
    • 'blob:text' (the text metadata item of a binary Obj’s blob attribute)
  • extend (ObjClass) – The CMS object class from which this one should inherit its attributes. The specified class must inherit from Obj. By default, the base class Obj is used.

Returns

ObjClass

Remarks

User-defined page models inherit all methods from Obj.

See also

provideWidgetClass – The definition of pages and widgets is fairly identical. It only differs with respect to the base type: Obj vs. Widget.

Examples

Create a SamplePage class, showing off with attribute types:

Scrivito.provideObjClass('SamplePage', {
  attributes: {
    body: 'widgetlist',
    callToAction: 'link',
    colorTheme: ['enum', { values: ['black', 'white', 'grey'] }],
    galleryImages: 'referencelist',
    headerImage: 'reference',
    layoutColumns: 'integer',
    metaRobots: ['multienum', { values: ['noindex', 'nofollow'] }],
    publishedAt: 'date',
    eventStart: 'datetime',
    relatedLinks: 'linklist',
    tables: ['widgetlist', { only: 'TableWidget' }],
    title: 'string',
  },
});

Create an OpaqueImage resource, exemplifying the remaining attribute types:

Scrivito.provideObjClass('OpaqueImage', {
  attributes: {
    blob: 'binary',
    opacityAsBgImage: 'float',
    tags: 'stringlist',
  },
});

Create a Homepage class with a title, a topBody and a bottomBody. topBody and bottomBody are used by Scrivito.extractText:

Scrivito.provideObjClass('Homepage', {
  attributes: {
    title: 'string',
    topBody: 'widgetlist',
    bottomBody: 'widgetlist,
  },
  extractTextAttributes: ['topBody', 'bottomBody'],
});

Create a Download class whose text metadata item is used by Scrivito.extractText:

Scrivito.provideObjClass('Download', {
  attributes: {
    blob: 'binary',
  },
  extractTextAttributes: ['blob:text'],
});

Create an abstract BaseClass and its NewsPage subclass:

const BaseClass = Scrivito.createObjClass({
  attributes: {
    body: 'widgetlist',
  },
});

class NewsPage extends BaseClass {
  myMethod() {
    return 'cool';
  }
}

Scrivito.provideObjClass('NewsPage', NewsPage);