Skip to content
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

feat(core): Allow customising the collection list based on multiple fields #2140

Merged
merged 18 commits into from
Mar 29, 2019

Conversation

devinea
Copy link
Contributor

@devinea devinea commented Mar 5, 2019

Summary

This allows customising the collection list display based on multiple fields rather than the title or identifier_field.
Adding the following to the collection definition in the config.yml controls the feature.

    list_fields: ['version','title']
    list_fields_seperator: ' - '

full collection

  - name: 'faq' # Used in routes, ie.: /admin/collections/:slug/edit
    label: 'FAQ' # Used in the UI
    folder: '_faqs'
    list_fields: ['version','title']
    list_fields_seperator: ' - '
    create: true # Allow users to create new documents in this collection
    fields: # The fields each document in this collection have
      - { label: 'Question', name: 'title', widget: 'string', tagname: 'h1' }
      - { label: 'Answer', name: 'body', widget: 'markdown' }
      - { label: 'Version', name: 'version', widget: 'string', default : '1.01' }

Test plan

Here is a screenshot of the result.
screenshot 2019-03-05 at 01 01 46

@netlify
Copy link

netlify bot commented Mar 5, 2019

Preview proposed changes to the CMS demo site in the link below:

Built with commit af645e9

https://deploy-preview-2140--cms-demo.netlify.com

@netlify
Copy link

netlify bot commented Mar 7, 2019

Preview proposed changes to netlifycms.org in the link below:

Built with commit 077a408

https://deploy-preview-2140--netlify-cms-www.netlify.com

@tomrutgers
Copy link
Contributor

tomrutgers commented Mar 8, 2019

@devinea I like where this is going! List view customization is a long standing feature request, so thanks for picking this up!

I have to say though, I find the concept of having three different options to displaying a text in the list view (title, identifier_field and list_fields) a bit hard to grasp. If we can somehow manage to integrate identifier_field and list_fields into a single property with some kinda smart logic in the background that figures out if it's a single field or an array of fields, it would be a whole lot easier to sell. Might be overthinking this though. Thoughts @erquhart?

@talves
Copy link
Collaborator

talves commented Mar 8, 2019

I like this feature. Great start!

I agree, we should just check for an array and use it if it exists. and then have identifier_fields_separator as an optional field that defaults to -.

@erquhart
Copy link
Contributor

erquhart commented Mar 11, 2019

Thanks for this @devinea! See #2019 for previous discussion on how to address the underlying issue here. The current practice for constructing custom values in the configuration is to use template strings, as we do for the slug config, so we should probably go that route for this as well: https://www.netlifycms.org/docs/configuration-options/#slug

This would allow us to use a familiar approach and just one config field to set it up. We could call the config key summary.

Your use case would then look like:

  - name: 'faq' # Used in routes, ie.: /admin/collections/:slug/edit
    label: 'FAQ' # Used in the UI
    folder: '_faqs'
    summary: '{{version}} - {{title}}'
    create: true # Allow users to create new documents in this collection
    fields: # The fields each document in this collection have
      - { label: 'Question', name: 'title', widget: 'string', tagname: 'h1' }
      - { label: 'Answer', name: 'body', widget: 'markdown' }
      - { label: 'Version', name: 'version', widget: 'string', default : '1.01' }

Thoughts?

@MerlindlH
Copy link

Hey everyone,
thanks for your feedback :)
@devinea and I refactored the code according to @erquhart 's suggestions. In detail, we added a summary key, replacing list_fields and list_fields_separator and made use of the existing logic for compiling slug templates to replace the templates in this field. This also makes use of the existing logic to find the respective identifier and use it when compiling the template.

Copy link
Contributor

@erquhart erquhart left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for putting this together @devinea and @MerlindlH!

function slugFormatter(collection, entryData, slugConfig) {
const template = collection.get('slug') || '{{slug}}';

export function getIdentifier(entryData, collection) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just a wrapper around selectIdentifier, use that instead of exporting this.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed, thanks :)


const summary = collection.get('summary');
if (summary) {
title = compileSlug(summary, new Date(), getIdentifier(entryData, collection), entryData);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The date replacements should be based off of fields in the entry, similar to what we do when creating a deploy preview URL: https://github.com/netlify/netlify-cms/blob/3cac3ef7796e89f8a3462b2dc06457fb4086cb29/packages/netlify-cms-core/src/backend.js#L242

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A problem could arise here if we do not have a date field in the entry.
We would like to use the compileSlug function and "ignore" all date references in summary (which compileSlug currently doesn't allow, so we'd have to refactor it) or we could catch the raised exception and retry compileSlug after removing all date template string from summary

@@ -128,7 +128,7 @@ export const selectInferedField = (collection, fieldName) => {
const fields = collection.get('fields');
let field;

// If colllection has no fields or fieldName is not defined within inferables list, return null
// If collection has no fields or fieldName is not defined within inferables list, return null
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I must have looked at this in annoyance a thousand times but never bothered to fix lol 👍👍

packages/netlify-cms-core/src/backend.js Outdated Show resolved Hide resolved
packages/netlify-cms-core/src/backend.js Outdated Show resolved Hide resolved
@erquhart
Copy link
Contributor

Pushed up a refactor, please test it in your setup and let me know if you run into any issues. It's rebased so you'll likely need to run yarn install first.

@MerlindlH
Copy link

Thanks for refactoring the code and extracting the logic :)
image
Fixed a small issue where the string wasn't replaced by the right content but the string itself
I changed the date handling a bit so that the summary still uses the date if there is one in the fields

export function extractTemplateVars(template) {
const regexp = RegExp(templateVariablePattern, 'g');
return extractVars(template, regexp);
const contentRegexp = RegExp(templateContentPattern, 'g');
return template.match(regexp).map(elem => elem.match(contentRegexp)[0]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Brilliant!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, but I forgot to handle the case where there are no matches and the first match-call returns null instead of []

@MerlindlH
Copy link

MerlindlH commented Mar 28, 2019

Apparently the search doesn't work anymore, I'm currently trying to figure out which change did crash it (update: fixed)

@erquhart
Copy link
Contributor

Fantastic edits, thank you. Writing code when sleep deprived is never a good idea, not sure why I do it 🙄

Added a refactor and addressed the failing CI build.

@erquhart
Copy link
Contributor

erquhart commented Mar 28, 2019

@MerlindlH everything looking good to your testing? We can merge and release in the next beta if so.

@MerlindlH
Copy link

MerlindlH commented Mar 29, 2019

I just checked it locally, fixed a small issue with searching a date and I think It's good to go (if the remote tests also go green, locally tests seemed fine)
Thanks for your feedback and support :)

@erquhart
Copy link
Contributor

Sounds good - is one more commit coming for the date search fix you mentioned?

Sent with GitHawk

@MerlindlH
Copy link

No, no more commits from my side (at least regarding the date search, that is fixed :) )
Let me know If there is anything else that has to be corrected, besides that it's good to go from my side :)

@erquhart erquhart merged commit 573ad88 into decaporg:master Mar 29, 2019
@erquhart
Copy link
Contributor

@MerlindlH @devinea this is currently published in the beta channel, you can use it at netlify-cms@beta.

@devinea devinea deleted the entry_card branch April 1, 2019 19:11
@devinea
Copy link
Contributor Author

devinea commented Apr 1, 2019

Awesome @erquhart @MerlindlH thanks

@adamtaylor13
Copy link

Is this in the docs as of now, or should this thread be the general reference for now? Just curious because I don't see it, but I'm not sure if I'm overlooking it somewhere.

@tomrutgers
Copy link
Contributor

@adamtaylor13 You can find this in the docs here: https://www.netlifycms.org/docs/configuration-options/#summary

@adamtaylor13
Copy link

@tomrutgers DOH. I missed that somehow. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants