Draft system
WARNING
The native Draft & Publish feature has been released in version 3.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.
What we want here is to fetch only data that has a status.
But we don’t want to use parameters (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 .
Each of these endpoint triggers a controller action. Here is the list of controller actions that exist by default when a content type is created.
And that is what we will do to filter to published
status by default.
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:
- attribute named
content
enumeration
attribute namedstatus
withdraft
,published
,archive
Then add some data with different status
.
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 . You will see all the data you created. Now let’s start the customization.
Path — ./api/article/controller/Article.js
We now know the function we have to update, but we just want to customize the returned article values.
In the you will find the default implementation of every action. It will help you overwrite the fetch logic.
Path — ./api/article/controller/Article.js
And now the data is back on GET /articles
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 —
And tada! Draft and archived articles disappeared.
This guide can be applied to any other controller action.