V4 Create Content Type (Model) through Plugin

I’m still figuring out this new v4 way. But one think I would like to know is, how can I auto-create a content type from a generate plugin.

Example, I generate a plugin call “cheer”. Now I want that plugin to create a model call “cheer” so that I can control the model, route, controllers all within the plugin folders itself.

Here’s how my folder structure is :-

plugins/cheer/server/index.js

'use strict';

const register = require('./register');
const bootstrap = require('./bootstrap');
const destroy = require('./destroy');
const config = require('./config');
const controllers = require('./controllers');
const routes = require('./routes');
const services = require('./services');
const contentTypes = require('./content-types');

module.exports = {
  register,
  bootstrap,
  destroy,
  config,
  controllers,
  routes,
  services,
  contentTypes,
  policies: {},
  middlewares: {},
};

plugins/cheer/server/content-types/index.js

const contentTypeCheer = require('./cheer');

module.exports = {
  'cheer': contentTypeCheer, // should re-use the singularName of the content-type
};

plugins/cheer/server/content-types/cheer/index.js

module.exports = {
  info: {
    tableName: 'content-type',
    singularName: 'cheer', // kebab-case mandatory
    pluralName: 'cheers', // kebab-case mandatory
    displayName: 'Cheer',
    description: 'Content type for cheer plugin.',
    kind: 'collectionType'
  },
  options: {
    draftAndPublish: false,
  },
  pluginOptions: {
    'content-manager': {
      visible: true
    },
    'content-type-builder': {
      visible: true
    }
  },
  attributes: {
    "Type": {
      "type": "string",
      "required": true,
      "unique": true
    },
    "Icon": {
      "allowedTypes": [
        "images"
      ],
      "type": "media",
      "multiple": false,
      "required": true
    }
  }
};

I followed this structure based on this documentation Server API - Strapi Developer Docs , but when I try to run “strapi develop”, I got error “TypeError: Cannot set property ‘plugin’ of undefined”.

What did I do wrong?

2 Likes

Just encountered the same problem. The docs have code typos in it also…
I can’t understand the docs, I think I’m not the only one,
Strapi - a definitive object types guidance, regarding the entire app, is needed and would be very helpful!

Solution

Export your content-type object with the key schema containing the data.

// [pluginName]/server/content-types/[contentTypeName].js

module.exports = {
  schema: {
    info: {
      tableName: 'content-type',
      singularName: 'message', // kebab-case mandatory
      pluralName: 'messages', // kebab-case mandatory
      displayName: 'הודעות',
      description: 'This is the test messages content type from the custom plugin',
      kind: 'collectionType'
    },
    options: {
      draftAndPublish: true,
    },
    pluginOptions: {
      'content-manager': {
        visible: true
      },
      'content-type-builder': {
        visible: true
      }
    },
    attributes: {
      content: {
        type: 'string',
        min: 1,
        max: 50,
        configurable: false
      },
      from: {
        type: 'email',
        required: true,
        configurable: false
      },
      title: {
        type: "string",
        required:true,
      }
    }
  }
}

Make sure you import the content type by the name of the singularName of it.

// [pluginName]/server/content-types/index.js

const message = require("./messages")
const bankAccount  = require("./bankAccounts")

module.exports = {
  message,
  'bank-account':bankAccount,
}