Skip to content

Create new menu global after creating a page #1019

Answered by Braggedtooth
Braggedtooth asked this question in Q&A
Discussion options

You must be logged in to vote

Solution hook for adding page when created and deleting when page is deleted.

import payload from "payload";
import { FieldHookArgs } from "payload/dist/fields/config/types";
import { MainMenu, Page } from "../../payload-types";

/**
 *  fetches the main menu from the database
 */
const fetchMenu = async (): Promise<MainMenu> =>
  await payload.findGlobal({ slug: "mainMenu" });

/**
 *  fetch the menu, add the new page to it, and then update the menu
 * @param args - {
 * @returns The page that was created.
 */
export const addPageToMenu = async (args: {
  doc: Page;
  operation: FieldHookArgs["operation"];
}) => {
  const menu = await fetchMenu();
  if (args.operation === "create") {
    c…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by Braggedtooth
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant