mattmilburn / strapi-plugin-menus

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

Allow to override the default `menu-item` form layout #154

Open Dominic-Preap opened 6 months ago

Dominic-Preap commented 6 months ago

Hi, it would be nice if we can override the Menu Item layout because sometime user want some fields at the top or change (placeholder, description) of the original field such as target or url.

Currently it will add duplicate fields like in the code below. I think we can do filter unique field on function getFieldsLayout.

https://github.com/mattmilburn/strapi-plugin-menus/blob/70693ee8a9e6345997223e7614784f03e8f2cc2f/admin/src/utils/get-fields-layout.js#L89

image

// ./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 = {
    zhTitle: {
      type: 'string',
    },
    isHide: {
      type: 'boolean',
    },
  };

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

  return plugin;
};
// ./config/plugins.ts
export default ({ env }) => ({
  menus: {
    config: {
      maxDepth: 3,
      layouts: {
        menuItem: {
          link: [
            {
              input: {
                label: 'Hide Menu',
                name: 'isHide',
                type: 'bool'
              },
              grid: { col: 6 }
            },
            {
              input: {
                label: 'Title (Chinese)',
                name: 'zhTitle',
                type: 'text',
                placeholder: 'Write title here...'
              },
              grid: { col: 6 }
            },
            {
              input: {
                label: 'How does it open?',
                name: 'target', // <== I want to override the default target field
                type: 'select',
                options: [
                  { label: 'New Window', value: '_blank' },
                  { label: 'Current Window', value: '_top' }
                ]
              },
              grid: { col: 6 }
            }
          ]
        }
      }
    }
  }
});