#How to properly update navigation data in globals in PayloadCMS?

3 messages · Page 1 of 1 (latest)

uneven phoenix
#

Hi everyone,

I’m working on a project using PayloadCMS and want to update global data (navigation) after saving or modifying a document in the Pages collection. I’m using the CollectionAfterChangeHook, but I’m having some issues with implementing the global data update.

Here’s my current code:

import { CollectionAfterChangeHook } from "payload";
import { Page } from "@/payload-types";

export const updateNavigation: CollectionAfterChangeHook<Page> = async ({
  doc,
  req: { payload },
}) => {
  const navigation = await payload.findGlobal({
    slug: "navigation",
  });

  await payload.updateGlobal({
    slug: "navigation",
    data: {
      ...navigation,
      updatedAt: new Date().toISOString(),
    },
  });

  return doc;
};

My questions:

  1. Is the code above correct for updating globals in PayloadCMS?
  2. How can I properly use payload.findGlobal and payload.updateGlobal to update the navigation data?
  3. What’s the best way to pass data from the modified Page (e.g., adding a link to the new page in navigation)?
spark totemBOT