diff --git a/docs/.vuepress/config/sidebar-developer.js b/docs/.vuepress/config/sidebar-developer.js index a69ce917fb..db1ca21d4c 100644 --- a/docs/.vuepress/config/sidebar-developer.js +++ b/docs/.vuepress/config/sidebar-developer.js @@ -497,7 +497,6 @@ const developer = [ // ['/developer-docs/latest/guides/is-owner', 'Create is owner policy'], // ['/developer-docs/latest/guides/custom-admin', 'Custom admin'], // ['/developer-docs/latest/guides/custom-data-response', 'Custom data response'], - ['/developer-docs/latest/guides/draft', 'Draft system'], // ['/developer-docs/latest/guides/error-catching', 'Error catching'], // ['/developer-docs/latest/guides/external-data', 'Fetching external data'], ['/developer-docs/latest/guides/jwt-validation', 'JWT validation'], diff --git a/docs/developer-docs/latest/guides/draft.md b/docs/developer-docs/latest/guides/draft.md deleted file mode 100644 index 4775930771..0000000000 --- a/docs/developer-docs/latest/guides/draft.md +++ /dev/null @@ -1,149 +0,0 @@ ---- -title: Draft System - Strapi Developer Docs -description: Learn in this guide how to create a draft system that will allow you to manage draft, published, and archive status inside your Strapi project. -canonicalUrl: https://docs.strapi.io/developer-docs/latest/guides/draft.html ---- - -# Draft system - -:::caution -The native [**Draft & Publish feature**](/user-docs/latest/content-manager/saving-and-publishing-content.md#saving-publishing-content) has been released in Strapi v3.2. We suggest you to use the native feature instead of this guide. - -This guide is still useful if you want to see the concept of "force filtering" in action. -::: - -This guide will explain how to create a draft system that will allow you to manage draft, published, and archive status. - -## Introduction - -What we want here is to fetch only data that has a `published` status. - -But we don't want to use [parameters](/developer-docs/latest/developer-resources/database-apis-reference/rest/api-parameters.md) (eg. /articles?status=published) because you can easily fake the params. - -To be able to do that, you have first to understand some concepts. - -When you create a content-type, it generates an API with the following list of [endpoints](/developer-docs/latest/developer-resources/database-apis-reference/rest-api.md#endpoints). - -Each of these endpoint triggers a controller action. Here is the list of [controller actions](/developer-docs/latest/development/backend-customization/controllers.md) that exist by default when a content-type is created. - -If you check the controller file of your generated API `./api/{content-type}/controller/{Content-Type}.js`, you will see an empty file. It is because all the default logic is managed by Strapi. But you can override these actions with your own code. - -And that is what we will do to filter to `published` status by default. - -## Example - -In our example we will use an Article content-type. By default, when you fetch articles you will get all articles. -Let's consider you don't want to expose articles that are in `draft` or `archive` status. - -To enforce this rule we will customize the action that fetches all articles to just fetch `published` articles. - -To follow the example you will have to create a content-type `articles` and add the following field definitions: - -- `string` attribute named `title` -- `text` attribute named `content` -- `enumeration` attribute named `status` with `draft`, `published`, `archive` - -```js -"attributes": { - "title": { - "type": "string" - }, - "content": { - "type": "text" - }, - "status": { - "type": "enumeration", - "enum": [ - "archive", - "draft", - "published" - ] - } - } -``` - -Then add some data with different `status`. - -## Override controller action - -To customize the function that fetches all our articles we will have to override the `find` function. - -First, to see the difference, let's request `GET /articles`. You will see all the data you created. -Now let's start the customization. - -**Path —** `./api/article/controller/Article.js` - -```js -module.exports = { - async find() { - return 'strapi'; - }, -}; -``` - -After saving the new function, let's restart the `GET /articles` request. We will see `strapi` as response. - -## Get the data back - -We now know the function we have to update, but we just want to customize the returned article values. - -In the [controller documentation](/developer-docs/latest/development/backend-customization/controllers.md#extending-core-controllers) you will find the default implementation of every action. It will help you overwrite the fetch logic. - -**Path —** `./api/article/controller/Article.js` - -```js -const { sanitizeEntity } = require('strapi-utils'); - -module.exports = { - async find(ctx) { - let entities; - 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 })); - }, -}; -``` - -And now the data is back on `GET /articles` - -## Apply our changes - -Here we want to force it to fetch articles that have status equal to `published`. - -The way to do that is to set `ctx.query.status` to `published`. -It will force the filter of the query. - -**Path —** `./api/article/controller/Article.js` - -```js -const { sanitizeEntity } = require('strapi-utils'); - -module.exports = { - async find(ctx) { - let entities; - - ctx.query = { - ...ctx.query, - status: 'published', - }; - - 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 })); - }, -}; -``` - -And tada! Draft and archived articles disappeared. - -::: tip -This guide can be applied to any other controller action. -:::