Skip to content

Commit

Permalink
#407 Add collection filtering by field and value
Browse files Browse the repository at this point in the history
  • Loading branch information
Luis Correia authored and calavera committed May 25, 2017
1 parent 6d5ff45 commit 544cb78
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
21 changes: 21 additions & 0 deletions example/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ collections: # A list of collections the CMS should be able to edit
folder: "_posts"
slug: "{{year}}-{{month}}-{{day}}-{{slug}}"
create: true # Allow users to create new documents in this collection
filter:
field: language
value: en
fields: # The fields each document in this collection have
- {label: "Title", name: "title", widget: "string", tagname: "h1"}
- {label: "Publish Date", name: "date", widget: "datetime", format: "YYYY-MM-DD hh:mma"}
- {label: "Language", name: "language", widget: "string"}
- {label: "Cover Image", name: "image", widget: "image", required: false, tagname: ""}
- {label: "Body", name: "body", widget: "markdown"}
meta:
Expand All @@ -26,6 +30,23 @@ collections: # A list of collections the CMS should be able to edit
- {label: "Question", name: "title", widget: "string", tagname: "h1"}
- {label: "Answer", name: "body", widget: "markdown"}

- name: "posts-pt" # Used in routes, ie.: /admin/collections/:slug/edit
label: "(PT) Post" # Used in the UI, ie.: "New Post"
folder: "_posts"
slug: "{{year}}-{{month}}-{{day}}-{{slug}}"
create: true # Allow users to create new documents in this collection
filter:
field: language
value: pt
fields: # The fields each document in this collection have
- {label: "Title", name: "title", widget: "string", tagname: "h1"}
- {label: "Publish Date", name: "date", widget: "datetime", format: "YYYY-MM-DD hh:mma"}
- {label: "Language", name: "language", widget: "string"}
- {label: "Cover Image", name: "image", widget: "image", required: false, tagname: ""}
- {label: "Body", name: "body", widget: "markdown"}
meta:
- {label: "SEO Description", name: "description", widget: "text"}

- name: "settings"
label: "Settings"
editor:
Expand Down
13 changes: 8 additions & 5 deletions example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,19 @@
window.repoFiles = {
_posts: {
"2015-02-14-this-is-a-post.md": {
content: "---\ntitle: This is a YAML front matter post\nimage: /nf-logo.png\ndate: 2015-02-14T00:00:00.000Z\n---\n\n# I Am a Title in Markdown\n\nHello, world\n\n* One Thing\n* Another Thing\n* A Third Thing\n"
content: "---\nlanguage: en\ntitle: This is a YAML front matter post\nimage: /nf-logo.png\ndate: 2015-02-14T00:00:00.000Z\n---\n\n# I Am a Title in Markdown\n\nHello, world\n\n* One Thing\n* Another Thing\n* A Third Thing\n"
},
"2015-02-15-this-is-a-json-frontmatter-post.md": {
content: "{\n\"title\": \"This is a JSON front matter post\",\n\"image\": \"/nf-logo.png\",\n\"date\": \"2015-02-15T00:00:00.000Z\"\n}\n\n# I Am a Title in Markdown\n\nHello, world\n\n* One Thing\n* Another Thing\n* A Third Thing\n"
content: "{\n\"language\": \"en\",\n\"title\": \"This is a JSON front matter post\",\n\"image\": \"/nf-logo.png\",\n\"date\": \"2015-02-15T00:00:00.000Z\"\n}\n\n# I Am a Title in Markdown\n\nHello, world\n\n* One Thing\n* Another Thing\n* A Third Thing\n"
},
"2015-02-16-this-is-a-toml-frontmatter-post.md": {
content: "+++\ntitle = \"This is a TOML front matter post\"\nimage = \"/nf-logo.png\"\n\"date\" = \"2015-02-16T00:00:00.000Z\"\n+++\n\n# I Am a Title in Markdown\n\nHello, world\n\n* One Thing\n* Another Thing\n* A Third Thing\n"
content: "+++\nlanguage = \"en\"\ntitle = \"This is a TOML front matter post\"\nimage = \"/nf-logo.png\"\n\"date\" = \"2015-02-16T00:00:00.000Z\"\n+++\n\n# I Am a Title in Markdown\n\nHello, world\n\n* One Thing\n* Another Thing\n* A Third Thing\n"
},
"2015-02-14-this-is-a-post-with-a-different-extension.other": {
content: "---\ntitle: This post should not appear because the extension is different\nimage: /nf-logo.png\ndate: 2015-02-14T00:00:00.000Z\n---\n\n# I Am a Title in Markdown\n\nHello, world\n\n* One Thing\n* Another Thing\n* A Third Thing\n"
content: "---\nlanguage: en\ntitle: This post should not appear because the extension is different\nimage: /nf-logo.png\ndate: 2015-02-14T00:00:00.000Z\n---\n\n# I Am a Title in Markdown\n\nHello, world\n\n* One Thing\n* Another Thing\n* A Third Thing\n"
},
"2017-05-15-this-is-a-post-with-a-different-language.pt.md": {
content: "---\nlanguage: pt\ntitle: This post should appear in a separate collection because the language is different\nimage: /nf-logo.png\ndate: 2015-02-14T00:00:00.000Z\n---\n\n# I Am a Title in Markdown\n\nHello, world\n\n* One Thing\n* Another Thing\n* A Third Thing\n"
}
},
_faqs: {
Expand Down Expand Up @@ -57,7 +60,7 @@
var slug = dateString + "-post-number-" + i + ".md";

window.repoFiles._posts[slug] = {
content: "---\ntitle: \"This is post # " + i + "\"\ndate: " + dateString + "T00:99:99.999Z\n---\n\n# The post is number " + i + "\n\nAnd this is yet another identical post body"
content: "---\nlanguage: \"en\"\ntitle: \"This is post # " + i + "\"\ndate: " + dateString + "T00:99:99.999Z\n---\n\n# The post is number " + i + "\n\nAnd this is yet another identical post body"
}
}

Expand Down
9 changes: 9 additions & 0 deletions src/backends/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ class Backend {
listEntries(collection) {
const listMethod = this.implementation[selectListMethod(collection)];
const extension = selectFolderEntryExtension(collection);
const collectionFilter = collection.get('filter');
return listMethod.call(this.implementation, collection, extension)
.then(loadedEntries => (
loadedEntries.map(loadedEntry => createEntry(
Expand All @@ -97,6 +98,14 @@ class Backend {
{
entries: entries.map(this.entryWithFormat(collection)),
}
))
// If this collection has a "filter" property, filter entries accordingly
.then(loadedCollection => (
{
entries: loadedCollection.entries.filter(
entry => (!collectionFilter || entry.data[collectionFilter.get('field')] === collectionFilter.get('value'))
),
}
));
}

Expand Down

0 comments on commit 544cb78

Please sign in to comment.