Strapi V4 populate Media and Dynamiczones from Components

For those still struggling with this, we use this simple customization to the controller find() function for an entity’s dynamic zones and content like below (this entity is a Page we created, so it is the controller for this ./src/api/page/controllers/page.js).
(You will need to augment the populate fields with the names of your own repeatable components or media attributes, and add to it as you add new sub-components to the entity… bit annoying but it has worked so far for our needs…)

async find(ctx) {
    const { query } = ctx;

    const entity = await strapi.entityService.findMany("api::page.page", { // page.page is our entity here
      ...query, // attach any incoming queries already in the request
      populate: {
        Image: true, // get data for media item field called "Image"
        Content: {// a dynamic zone with different components, 
// and those components might have some repeatable sub-component too
// We only seem to need to add the sub-components...
          populate: {
            Image: true,// media field called "Image"
            Buttons: {// repeatable sub-component called "Buttons" used in a dynamic zone component
              populate: {
                page: true,// the Button component has a relation to a page item, so populate that...
              },
            },
            Icons: true,// another repeatable sub-component used in a dynamic zone component
          },
        },
      },
    });
    const sanitizedEntity = await this.sanitizeOutput(entity, ctx);

    return this.transformResponse(sanitizedEntity);
  },
4 Likes