Кастомные Content Types в Strapi
Content Types в Strapi бывают трёх видов: Collection Type (список записей), Single Type (одна запись — настройки сайта, главная страница), Component (переиспользуемая группа полей). Все они описываются JSON-схемой в src/api/ или src/components/.
Collection Type
// src/api/product/content-types/product/schema.json
{
"kind": "collectionType",
"collectionName": "products",
"info": {
"singularName": "product",
"pluralName": "products",
"displayName": "Товар"
},
"options": { "draftAndPublish": true },
"pluginOptions": { "i18n": { "localized": true } },
"attributes": {
"name": {
"type": "string",
"required": true,
"pluginOptions": { "i18n": { "localized": true } }
},
"slug": { "type": "uid", "targetField": "name" },
"description": {
"type": "richtext",
"pluginOptions": { "i18n": { "localized": true } }
},
"price": { "type": "decimal", "required": true, "min": 0 },
"stock": { "type": "integer", "default": 0, "min": 0 },
"images": { "type": "media", "multiple": true, "allowedTypes": ["images"] },
"category": {
"type": "relation",
"relation": "manyToOne",
"target": "api::category.category",
"inversedBy": "products"
},
"specs": {
"type": "component",
"repeatable": true,
"component": "product.spec"
}
}
}
Single Type
// src/api/homepage/content-types/homepage/schema.json
{
"kind": "singleType",
"collectionName": "homepages",
"info": {
"singularName": "homepage",
"pluralName": "homepages",
"displayName": "Главная страница"
},
"attributes": {
"hero": { "type": "component", "component": "sections.hero", "repeatable": false },
"sections": { "type": "dynamiczone", "components": ["sections.text", "sections.gallery", "sections.cta"] },
"seo": { "type": "component", "component": "shared.seo", "repeatable": false }
}
}
Component
// src/components/shared/seo.json
{
"collectionName": "components_shared_seos",
"info": { "displayName": "SEO", "icon": "search" },
"attributes": {
"metaTitle": { "type": "string", "maxLength": 60 },
"metaDescription": { "type": "text", "maxLength": 160 },
"ogImage": { "type": "media", "multiple": false, "allowedTypes": ["images"] },
"noIndex": { "type": "boolean", "default": false }
}
}
API-запросы к content types
# Collection Type
GET /api/products?populate=images,category,specs&sort=createdAt:desc
# Single Type
GET /api/homepage?populate=hero,sections,seo
# Dynamic Zone — нужно populate каждого компонента
GET /api/homepage?populate[sections][populate]=*
Программное создание записей
// В контроллере или сервисе Strapi
await strapi.entityService.create('api::product.product', {
data: {
name: 'Новый товар',
slug: 'novy-tovar',
price: 1500,
publishedAt: new Date(),
},
})
Сроки
Создание 3–5 content types с компонентами через Content-Type Builder и JSON — 1–2 дня.







