How can I create content-types on init of a custom plugin?

System Information
  • Strapi Version: 4
  • Operating System: mac
  • Database: sqlite
  • Node Version:
  • NPM Version:
  • Yarn Version:

I suggest you use the strapi cli for all of your needs to create content-types/plugins/routes etc…

Simply run:

npm run strapi generate

//or

yarn strapi generate

All the best! :grinning:

1 Like

Thanks for the answer. I’d like some content types to be created automatically by the plugin and not through the CLI. Trying to figure it out through the docs…

Do you how to create content-types through a custom plugin?

Hey @user4

If I am understanding correctly, the entry file documentation and specially the content type object that you export is what you are looking for. Here is how you define a sample content types for a custom plugin ( I believe the same code on the doc has a bug but feel free to try) :

// path: ./strapi-server.js

module.exports = () => ({
    contentTypes: {
        'content-type-a': { // should re-use the singularName of the content-type
            info: {
                tableName: 'content-type',
                singularName: 'content-type-a', // kebab-case mandatory
                pluralName: 'content-type-as', // kebab-case mandatory
                displayName: 'Content Type A',
                description: 'A regular content type',
                kind: 'collectionType'
            },
            options: {
                draftAndPublish: true,
            },
            pluginOptions: {
                'content-manager': {
                    visible: false
                },
                'content-type-builder': {
                    visible: false
                }
            },
            attributes: {
                name: {
                    type: 'string',
                    min: 1,
                    max: 50,
                    configurable: false
                },
            }
        },
        'content-type-b': { // should re-use the singularName of the content-type
            info: {
                tableName: 'content-type',
                singularName: 'content-type-a', // kebab-case mandatory
                pluralName: 'content-type-as', // kebab-case mandatory
                displayName: 'Content Type A',
                description: 'A regular content type',
                kind: 'collectionType'
            },
            options: {
                draftAndPublish: true,
            },
            pluginOptions: {
                'content-manager': {
                    visible: false
                },
                'content-type-builder': {
                    visible: false
                }
            },
            attributes: {
                name: {
                    type: 'string',
                    min: 1,
                    max: 50,
                    configurable: false
                },
            }
        }
    }
});

I also don’t know if you are looking to rather dynamically create content types at run time, which I am not sure if its possible.

1 Like

Thanks for the help!
It turns that a content type in this contentTypes object, needs to have all the data you pasted, inside an object ‘schema’. For example

'content-type-a': { // should re-use the singularName of the content-type
          schema: {
            info: {
                tableName: 'content-type',
                singularName: 'content-type-a', // kebab-case mandatory
                pluralName: 'content-type-as', // kebab-case mandatory
                displayName: 'Content Type A',
                description: 'A regular content type',
                kind: 'collectionType'
            },
            options: {
                draftAndPublish: true,
            },
            pluginOptions: {
                'content-manager': {
                    visible: false
                },
                'content-type-builder': {
                    visible: false
                }
            },
            attributes: {
                name: {
                    type: 'string',
                    min: 1,
                    max: 50,
                    configurable: false
                },
            }
        }
      }