Get locale from beforeUpdate lifecycle hook

Hi!.

I finally found a workaround. I can get the id of the entity I`m editing, and query its locale:

module.exports = {
  async beforeCreate(event) {
    const locale = event.params.data.locale;
    await renameSlugIfNeeded(event, locale);
  },
  async beforeUpdate(event) {
    const id = event.params.where.id;
    const locale = await getBeforeUpdateLocale(id);

    if (locale !== null) {
      await renameSlugIfNeeded(event, locale);
    }
  },
};

async function getBeforeUpdateLocale(id) {
  const page = await strapi.service('api::page.page').findOne(id);

  if (page === null) {
    return null;
  }

  return page.locale;
}
3 Likes