Category class

Return to Studio and create a category class. It will contain articles on specific topics.

Create a name attribute, String type. It will store the category name. Check Required and Unique boxes. Select Search tab and check Common searchable and Select searchable boxes.

Next, you need to link articles and categories. One article can be placed in several categories. Go to article class and create a categories attribute, Reference type. Check Multiple box.

Go to category class and create a publicArticles attribute, Back reference type. Select article class in the Reference class field. Then select categories attribute in the Reference attribute field. Check Multiple and Read only boxes. You cannot add or remove articles using this attribute, only managing categories from article itself.

Now a category displays all articles that have this category. To filter public articles, you must create a view of the article class. A view extends and overrides class parameters. For example, when displaying objects in a list, the list view can display only some class attributes. When creating an object, the create view can add validators. When editing an object, the edit view may prohibit modification of some attributes and more.

Open article class and go to Views tab. Create a publicList view. Specify condition {"status": "published"} for selecting articles in the Filter field. Only articles with published status will be displayed in this view.

Save the view. Then go to Attributes tab and add attributes that should be in the view.

Go to category class and open publicArticles attribute. Click Relation tab. Select publicList view in the List view field,.

Go to category class and create a publicArticleCounter attribute, Calculated type. Set calculation algorithm in the Expression field:

["$query", "count", "publicList.article", null, {"categories": ".$key"}]

See node_modules\evado-meta-base\calc\Calc for more details.

Calculated attribute is not stored in database, but is calculated on every request. This affects performance and imposes certain restrictions on searches and selections.

Export metadata and go to Office. Create categories and articles. Add a category to an article. Change status of article and check how the list of public articles in category has changed.