Field type "Computed"
Matthew Butt
To add to this - custom fields should ideally be calculated at runtime, not stored in the database. They should otherwise behave like normal fields, so they're valid values for
populate
and can be picked up by plugins like strapi-typed-client
. They should also be applicable as extensions, so we should be able to, for example, calculate an href
field from a plugin::navigation.navigation-item
(from the strapi-plugin-navigation
plugin).Steve Chen
Before we get this, you can use life cycles.
Add a lifecycles.js in your class folder with code like this:
module.exports = {
beforeCreate(event) {
grade = event.params.data.grade;
level = event.params.data.level;
event.params.data.class_name =
G${grade} ${level}
;},
beforeUpdate(event) {
grade = event.params.data.grade;
level = event.params.data.level;
event.params.data.class_name =
G${grade} ${level}
;},
};
Florent Catiau-Tristant
Steve Chen To complete your answer, this is what I did in the meantime the feature gets prioritized:
- Create a new field in my entity as usual. Make it required, and possiblyprivateif you don't need it to be returned by the api.
- Add the lifecycle hooks to automatically update its value based on other entity attributes.
- On the Strapi interface, update the "Configuration View" and make that field not editable, so that nobody can manually updates this attribute's value from the UI.
Works like a charm for my use case :)