diff --git a/example/config.yml b/example/config.yml index fd9d855a0bd1..7761c6351a3f 100644 --- a/example/config.yml +++ b/example/config.yml @@ -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: @@ -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: diff --git a/example/index.html b/example/index.html index e7a96279d81b..0115ac8bdac1 100644 --- a/example/index.html +++ b/example/index.html @@ -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: { @@ -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" } } diff --git a/src/backends/backend.js b/src/backends/backend.js index 1eb6de41a291..2e0350baa741 100644 --- a/src/backends/backend.js +++ b/src/backends/backend.js @@ -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( @@ -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')) + ), + } )); }