Slug management : awtis & mizik to slug

This commit is contained in:
2020-12-13 13:38:33 +01:00
parent 9e785e78e4
commit 62361abd53
+23 -6
View File
@@ -1,8 +1,25 @@
'use strict';
'use strict'
/**
* Read the documentation (https://strapi.io/documentation/v3.x/concepts/models.html#lifecycle-hooks)
* to customize this model
*/
const slugify = require('slugify')
module.exports = {};
const jwennAwtisEpiId = async data => {
const awtis = await strapi.query('awtis').find({_id: data})
return awtis.map(a => a.alias).join('-')
}
module.exports = {
lifecycles: {
beforeCreate: async data => {
if (data.titre) {
const awtis = await jwennAwtisEpiId(data.awtis)
data.slug = slugify(`${awtis}-${data.titre}`, {lower: true, remove: /[*+~.()'"!:@]/g})
}
},
beforeUpdate: async (params, data) => {
if (data.titre) {
const awtis = await jwennAwtisEpiId(data.awtis)
data.slug = slugify(`${awtis}-${data.titre}`, {lower: true, remove: /[*+~.()'"!:@]/g})
}
}
}
}