mattmilburn / strapi-plugin-menus

A plugin for Strapi CMS to customize the structure of menus and menu items.
MIT License
112 stars 28 forks source link

Choose the "Entry title" for "Relation" Field Type #127

Closed wdmtech closed 1 year ago

wdmtech commented 1 year ago

Hey there, I have added a couple of relation fields, and curiously the dropdowns for the Pages content-type use id but the ones for Authors use name

Does anyone know if there any way of specifying which field to use, or where the plugin pulls this field from?

image image

Here's how I'm adding the fields:

Schema extension:

// ./src/extensions/menus/strapi-server.js
"use strict";

module.exports = (plugin) => {
  // Get current `MenuItem` attributes.
  const defaultAttrs = plugin.contentTypes["menu-item"].schema.attributes;

  // Define custom attributes for `MenuItem` the same way they would be defined
  // on any other schema.
  const customAttrs = {
    example_bool: {
      type: "boolean",
    },
    example_media: {
      type: "media",
      allowedTypes: ["images"],
      multiple: false,
    },
    pages: {
      type: "relation",
      relation: "oneToMany",
      target: "api::page.page",
    },
    categories: {
      type: "relation",
      relation: "oneToMany",
      target: "api::category.category",
    },
    authors: {
      type: "relation",
      relation: "oneToMany",
      target: "api::author.author",
    },
  };

  // Extend the `MenuItem` content type with custom attributes.
  plugin.contentTypes["menu-item"].schema.attributes = {
    ...defaultAttrs,
    ...customAttrs,
  };

  return plugin;
};

Form layout extension:

// ./config/plugins.js
"use strict";

module.exports = {
  menus: {
    config: {
      maxDepth: 3,
      layouts: {
        menuItem: {
          link: [
            {
              input: {
                label: "Boolean",
                name: "example_bool",
                type: "bool",
              },
              grid: {
                col: 6,
              },
            },
          ],
          media: [
            {
              input: {
                label: "Media",
                name: "example_media",
                type: "media",
              },
            },
          ],
          pages: [
            {
              input: {
                label: "Pages",
                name: "pages",
                type: "relation",
              },
            },
          ],
          categories: [
            {
              input: {
                label: "Categories",
                name: "categories",
                type: "relation",
              },
            },
          ],
          authors: [
            {
              input: {
                label: "Authors",
                name: "authors",
                type: "relation",
              },
            },
          ],
        },
      },
    },
  },
};
wdmtech commented 1 year ago

I changed the label from pages to something else (and back again), and now it is using the name field - I have no idea how, but all works as expected now!