-
-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#407 Add collection filtering by field and value #409
#407 Add collection filtering by field and value #409
Conversation
I'm not sure how valuable this will be since the cms can't currently load multiple entry types from the same folder - we'd have to dig a bit more to get the implementation right. As it is, it wouldn't resolve #407. What would be the advantage of this approach over simply having separate folders per language? That's not an ideal approach at all, I realize, just trying to understand your use case. Thoughts? |
That issue is mostly about generators that lump multiple entry types into one directory, so resolving it would require being able to edit those different entry types from that single directory. This won't do that. Or, using your case as an example, you wouldn't be able to edit any of the other languages, just one. Maybe I'm missing something, it's quite possible :) |
Nope, in that sense you are totally correct, which is why this by itself won't solve the problem of i18n on Hugo or in general but it will do what has been requested on #407:
Does that. I used a new
Using 1collection per blogPostType value you can then fix:
Each collection may have its own fields and all should be ok as we should be filtering out files that use different fields. Hope I'm seeing this right, seems like what was originally asked but we could use some feedback from @jholmes033169 :) |
Awesome work. While I agree that it doesn't quite solve the i18 issue as I understand it, it DOES address this requirements I outlined. I will be testing with it today and over the weekend. |
As pointed out by @erquhart though, we may have some issues with the multiple collections on the same dir. But I'll give it a look on Monday the latest |
To recap our convo in Gitter for posterity: once you filter on an entry type, the CMS can no longer interact with the excluded entries from that directory. This is why the fix in question won't address #407. That said, this problem is worth solving, and it may still be a relatively quick fix. @lmcorreia let me know if you need to discuss anything when you come back to this. |
Hmm... I was assuming that targeting multiple collection at the same folder, each filtering on something different... was straight forward. Was that a bad assumption? If so... that seems like an architectural weakness... and since you guys seem pretty sharp, I'm them assuming it wasn't arbitrary, but rather a deliberate choice. I don't want to start weakening the architecture, but this feature seems so... basic, I guess. So... I guess my question then is... why can the CMS no longer interact with the files in a folder that was excluded by another collection? |
And to clarify... I'm also a developer... I'm not criticizing (this is amazing work)... just seeking to understand. |
@jholmes033169 this is a community project - opportunities for improvement abound, but I think we're all equal to the task if we work together. When I say it won't work, I mean I tried, and got some awesome console errors and a CMS that wouldn't load. I'm not digging into this particular issue at the moment, so I didn't look beyond that. It sounds like @lmcorreia is taking another pass soon, but by all means, pull down the branch and take a whack at it yourself! There's help if you have questions. |
Noted... just making sure... some communities are different. I'll also take a look at it over the weekend. |
I cannot figure out for the life of me why this is causing a problem for other collections. There are some design assumptions I am not familiar with... or need to become more familiar with. |
@jholmes033169 I'll take a look soon. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this! Just a few small changes needed.
example/config.yml
Outdated
@@ -6,13 +6,44 @@ media_folder: "assets/uploads" | |||
|
|||
collections: # A list of collections the CMS should be able to edit | |||
- name: "posts" # Used in routes, ie.: /admin/collections/:slug/edit | |||
label: "Post" # Used in the UI, ie.: "New Post" | |||
label: "(EN) Post" # Used in the UI, ie.: "New Post" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick: drop the "(EN) " prefix - if this update is too in-your-face it could cause confusion. We want it to be clear that this is optional.
example/config.yml
Outdated
- {label: "SEO Description", name: "description", widget: "text"} | ||
card: {type: "image", image: "image", text: "title"} | ||
|
||
- name: "faq" # Used in routes, ie.: /admin/collections/:slug/edit |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like an accidental paste (duplicate faq collection).
src/backends/backend.js
Outdated
@@ -96,6 +97,13 @@ class Backend { | |||
{ | |||
entries: entries.map(this.entryWithFormat(collection)), | |||
} | |||
)) | |||
.then(loadedCollection => ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're trying to get better about source docs - can you add a super brief comment explaining this new link in the promise chain?
@erquhart Review comments addressed, if you need any further documentation, let me know! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
ea8745d
to
e7d71c8
Compare
Squashed and rebased. |
…#407_filter_collection
src/backends/backend.js
Outdated
.then(loadedCollection => ( | ||
{ | ||
entries: loadedCollection.entries.filter( | ||
entry => (!collectionFilter || entry.data[collectionFilter.get('field')] === collectionFilter.get('value')) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is going to loop over the full collection even when there is no filter, can we not do that?
I would expect doing something like collectionFilter ? loadedCollection.entries.filter(...) : loadedCollection.entries
.
It would also be great it was a separated function if this becomes hard to read, something like:
.then(loadedCollection => {entries: filterEntries(loadedCollection, collectionFilter)})
Copy, @calavera - I'll give it a quick refactoring soon. |
@lmcorreia Is there anything I can do to help with this? (I'm offering because there a a LOT of people waiting on this one.) Willing to help any way I can. |
@jholmes033169 I'm sure he wouldn't mind someone else pushing a commit for the refactor requested by @calavera. |
A little advice here... I'm trying to get this fork so I can refactor it... but I can't find it. I've never tried to fork a fork... some I'm not sure what "right" way to do this is. |
@jholmes033169 sorry, forgot that PR edits are limited to maintainers. We'll make sure this gets in soon. |
@jholmes033169 if you're still up for refactoring this, just open a new PR - just do your work in a separate commit so that @lmcorreia's commits are maintained as is. You probably already know this, but you can quickly grab his branch to your forked repo locally like so:
|
@jholmes033169 @erquhart Sorry for the absence, picking this up now |
LGTM, thanks @lmcorreia ! |
- Summary
Closes #407
- Test plan
A new field was added to the sample repo and filtering is done on it via the changes made to the sample config.yml. Probably could use some more hands-on testing to make sure everything is ok but the code is very straightforward and the only new post that should be excluded is excluded as specified in the filter. Changing filtering values also yields the expected results.
- Description for the changelog
Add collection entries filtering by
field: value
through thefilter
property in the collections.- A picture of a cute animal (not mandatory but encouraged)