Data fields appear to be populating ok by using the suggested patch below, however, media fields are not populating.
Tried the following with no luck -
* http://localhost:1337/api/pages?populate=*
* {{protocol}}://{{host}}:{{port}}/api/pages?populate[Content][populate]=images
Example below is from - https://forum.strapi.io/t/strapi-v4-populate-media-and-dynamiczones-from-components/12670/9
Also tried this plugin (no luck) - https://www.npmjs.com/package/strapi-plugin-populate-deep
Page example below with dynamic zone -
/helpers/populate.js (this works for all data except images) -
const { createCoreController } = require("@strapi/strapi/lib/factories");
function populateAttribute({ components }) {
if (components) {
const populate = components.reduce((currentValue, current) => {
return { ...currentValue, [current.split(".").pop()]: { populate: "*" } };
}, {});
return { populate };
}
return { populate: "*" };
}
const getPopulateFromSchema = function (schema) {
return Object.keys(schema.attributes).reduce((currentValue, current) => {
const attribute = schema.attributes[current];
if (!["dynamiczone", "component"].includes(attribute.type)) {
return currentValue;
}
return {
...currentValue,
[current]: populateAttribute(attribute),
};
}, {});
};
function createPopulatedController(uid, schema) {
return createCoreController(uid, () => {
console.log(schema.collectionName, JSON.stringify(getPopulateFromSchema(schema)));
return {
async find(ctx) {
ctx.query = {
...ctx.query,
populate: getPopulateFromSchema(schema),
// populate: '*',
};
return await super.find(ctx);
},
async findOne(ctx) {
ctx.query = {
...ctx.query,
populate: getPopulateFromSchema(schema),
// populate: '*',
};
return await super.findOne(ctx);
},
};
});
}
module.exports = createPopulatedController;
/src/api/page/controllers/pages.js -
"use strict";
const collectionType = 'page'
const schema = require(`../content-types/${collectionType}/schema.json`);
const createPopulatedController = require("../../../helpers/populate");
module.exports = createPopulatedController(`api::${collectionType}.${collectionType}`, schema);
Response below images don't come through -
{
"data": [
{
"id": 1,
"attributes": {
"title": "testing home page",
"slug": "/",
"publish_at": null,
"createdAt": "2022-04-12T12:08:32.002Z",
"updatedAt": "2022-04-12T15:07:11.990Z",
"publishedAt": "2022-04-12T12:42:55.682Z",
"locale": "en",
"seoComponent": null,
"block": [
{
"id": 1,
"__component": "image.image-copy-full",
"heading": "Delivering something amazing",
"subHeading": "test sadasdf",
"ctaButton": "test",
"miscValues": {
"testing": "object field"
},
"actionUrl": null,
"isInternal": true,
"isVisible": true
},
{
"id": 1,
"__component": "image.image-copy-chip",
"heading": "A platform",
"subHeading": "Allowing full integration",
"ctaButton": null,
"miscValues": null,
"actionUrl": null,
"isInternal": true,
"isVisible": false
},
{
"id": 1,
"__component": "image.image-copy",
"heading": "To transform our world",
"subHeading": "In order to reach that scale",
"ctaButton": null,
"miscValues": null,
"actionUrl": null,
"isInternal": true
}
]
}
}
],
"meta": {
"pagination": {
"page": 1,
"pageSize": 25,
"pageCount": 1,
"total": 1
}
}
}
Schema sample for "ImageCopyFull" on screenshot -
{
"collectionName": "components_image_image_copy_fulls",
"info": {
"displayName": "ImageCopyFull",
"icon": "file-image",
"description": ""
},
"options": {},
"attributes": {
"heading": {
"type": "string"
},
"subHeading": {
"type": "string"
},
"ctaButton": {
"type": "string"
},
"miscValues": {
"type": "json"
},
"actionUrl": {
"type": "string"
},
"isInternal": {
"type": "boolean",
"default": true
},
"image": {
"type": "media",
"multiple": false,
"required": true,
"allowedTypes": [
"images",
"videos",
"audios",
"files"
]
},
"isVisible": {
"type": "boolean",
"default": false
}
}
}