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.

Copy
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.
  • extend (ObjClass) – The CMS object class from which this one should inherit its attributes. The class given 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:

Copy
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',
    relatedLinks: 'linklist',
    tables: ['widgetlist', { only: 'TableWidget' }],
    title: 'string',
  },
});

Create an “OpaqueImage” resource, exemplifying the remaining attribute types:

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

Create an abstract “BaseClass” and a “NewsPage” subclass:

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

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

Scrivito.provideObjClass('NewsPage', NewsPage);