Lifecycle hooks for Components
Ernest
Hi everyone! I think it would be very useful to have lifecycle hooks for Components, just as it is today for Collection Types.
A
Anotnin Arsenault
What i did was to create a middleware that is called before update(controller) and there i am fetching the detailled data and then adding it to the ctx.state.
Marko Žmak - Pepe
Anotnin Arsenault: Can you chare an example?
A
Anotnin Arsenault
Marko Žmak - Pepe: Sure
Marko Žmak - Pepe
Anotnin Arsenault: Thank you. Any idea how to efficently use it for all content types?
A
Anotnin Arsenault
Marko Žmak - Pepe: In the lifecycle you got access to strapi so simply get the oldData with: const oldData = strapi.requestContext.get().state.oldData
Marco Magnanini
Anotnin Arsenault: It doesn't work when the update is made from the admin panel, since it doesn't use the same API routes. I also tried customizing the service and the controller but I still have the problem with the admin panel. The only way to completely customize the update logic is through lifecycle hooks and they don't work with components :(
Francesc Perez
Do this, and work a little bit :)
Sem Limi
Agree, I really really need my previous component data to be visible in the beforeUpdate lifecycle before it gets updated. Strapi is almost useless for the feature my company needs (viewing change log and version control of data updates) without this feature since 90% of our data is component based. Would anyone know an alternative solution to how I could get pre-updated component data before it updates? And then compare the old vs. updated component data?
Nitara
Sem Limi: I saw an idea to fetch the data from old record, but after I tried that, I get the same data for components - since components are saved before lifecycle events are even fired (as far as I understand it).
Boegie19
Nitara: If you use a before hook it should be fired before the components are saved if that is not the cease you could add a middleware that fires before you even call the lifecycle
Nitara
Boegie19: Yes, I am using beforeCreate/beforeUpdate and data there contains just IDs of records in components - but I can fetch that with a bit of additional code. The issue is that fetched old record and data sent to lifecycle method is the same (for components). I will check out this middleware method - did not use those until now in Strapi, but sounds like a feasible idea, with probably some other gotchas, but well, that is programming :D Thank you!
Fahid Mohammad
Any luck guys? this is a much needed solution.
Ebrahim Alkandary
yes it would be great, I've been stuck to find a solution for this matter
Sven Bretschneider
Or at least make component data visible in before... lifecycle hooks of content types
James Ricketts
Agreed! This would be extremely useful.