How can I create content-types on init of 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