provideWidgetClass(name, contentClassOrOptions)

Creates a widget class or registers an already existing class.

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

Widget classes serve as boilerplates for CMS widgets. Their main purpose is to define the attributes the instances require, for example an html attribute named “content” and a string attribute named “summary” .

Copy
Scrivito.provideWidgetClass('NewsTextWidget', {
  attributes: {
    content: 'html',
    summary: 'string',
  },

  // onlyInside: 'ArticlePage',
});

Params

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

Options

  • attributes (Object) – The attribute definition of the CMS widget 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.
  • extend (WidgetClass) - The CMS widget class from which this one should inherit its attributes. The specified class must inherit from Widget. By default, the base class Widget is used.
  • onlyInside (String or Array) – Optionally, a single page or widget class name (string) or several such names (array) specifying the class(es) in whose instances widgets of this class must be contained. So, instances of this widget class cannot be placed on pages or into widgets of any other classes than the ones specified here.

Examples

Create an advanced image widget class with support for some fancy features:

Copy
Scrivito.provideWidgetClass('AdvancedImageWidget', {
  attributes: {
    image: 'reference',
    caption: 'string',
    zoomTo: ['enum', { values: ['none', 'left', 'center', 'right'] } ],
  },
});

Create an abstract BaseClass and a subclass named AdvancedImageWidget:

Copy
const BaseClass = Scrivito.createWidgetClass({
  attributes: {
    image: 'reference',
  },
});

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

Scrivito.provideWidgetClass('AdvancedImageWidget', AdvancedImageWidget);