Search code examples
strapi

Is there any draft system for Single Types in strapi?


I wanted to save draft in strapi for single types without publishing it. Right now if i save the changes it will be published directly. I tried using a boolean - published, and show the data on frontend app when published is true. This way if the published bool is false whole single page data will not be shown. I want it so that previous data is still there but not the new unpublished data.

Is there any way to make this achievable?


Solution

  • Since you've mentioned it, why not add an is_draft boolean type to your strapi model and use that as a shadow crud filter when querying the rest api or graphql endpoint?

    If you want to prevent strapi from returning draft records, you can also override the controller find function.

    
    // api/article/controllers/article.js
    
    const { sanitizeEntity } = require('strapi-utils');
    
    module.exports = {
    
      /**
       * Retrieve records.
       *
       * @return {Array}
       */
    
      async find(ctx) {
        let entities;
        ctx.query.is_draft = typeof ctx.query.is_draft === 'boolean' ? ctx.query.is_draft || false; // return published by default, or the explicitly specified is_draft filter.
        if (ctx.query._q) {
          entities = await strapi.services.article.search(ctx.query);
        } else {
          entities = await strapi.services.article.find(ctx.query);
        }
    
        return entities.map(entity => sanitizeEntity(entity, { model: strapi.models.article }));
      },
    };