Override strapi-admin 'restaurant' (in my case post) creation, update or what ever controllers

So I searched a lot with a help of @mbule (on the strapi discord) We tried to pass trough the service but look like the strapi-admin does not call the service’s functions too… bit sad…

So @mbule suggest to use Model Lifecycle Hook to change the default field comportement and customize the intput value like an realtime update(that how I understood it in the doc).

So In my case, I wanted to parse the body and auto build the slug field based on the title, in the following code that what I do :

// api/post/models/post.js
const slugify = require("slugify");
// utils
const buildFullUrl = require("../../../utils/buildFullUrl");

module.exports = {
  /**
   * Triggered before user creation.
   */
  lifecycles: {
    async beforeCreate(data) {
      if (data.title) {
        data.slug = slugify(data.title, { lower: true });
      }
      if (data.body) {
        // function which modify the hyperLinks and return HMLT body as string
        const body = await buildFullUrl.index(data);
        data.body = body;
      }
    },
    async beforeUpdate(params, data) {
      if (data.title) {
        data.slug = slugify(data.title, { lower: true });
      }
      if (data.body) {
        // function which modify the hyperLinks and return HMLT body as string
        const body = await buildFullUrl.index(data);
        data.body = body;
      }
    },
  },
}; 

@mbule Hopefully my explication is enough clear ^^

Hopefully this post gonna help some of you !
regards

1 Like