Displaying relationship titles instead of IDs in collection list view

default discord avatar
ssyberg
5 months ago
41

I swear I saw this at some point, if there some standard way to replace the id with the title for relationship columns in the list view?

  • default discord avatar
    illedran
    5 months ago

    For me, it works out of the box if I use the

    admin.useAsTitle

    option when defining collections

  • discord user avatar
    alessiogr
    Payload Team
    5 months ago

    @ssyberg do you have a minimal reproduction collection you can share?


    Would like to look into this!



    (assuming it works in the document view itself and that you didn't forget the

    admin.useAsTitle

    option)

  • default discord avatar
    ssyberg
    5 months ago

    Yup

    useAsTitle

    is working fine, in face when I click in to that record it surfaces the title just fine:



        fields: [
            {
                type: 'row',
                fields: [
                    {
                        name: 'center',
                        type: 'select',
                        required: 'true',
                        options: CENTERS
                    },
                    {
                        name: 'references',
                        type: "relationship",
                        relationTo: ["posts", "papers", "reports", "in-the-media",],
                        maxDepth: 1,
                    },
                ]
            },
    image.png
  • discord user avatar
    alessiogr
    Payload Team
    5 months ago

    and it doesn't work in the list view?

  • default discord avatar
    ssyberg
    5 months ago

    Right! Sorry I should have clarified,

    useAsTitle

    is working fine on the related model itself

    image.png
  • discord user avatar
    alessiogr
    Payload Team
    5 months ago

    lemmme see if I can replicate it

  • default discord avatar
    ssyberg
    5 months ago

    This might be relevant, the

    useAsTitle

    is being set in a plugin



    (I've written a plugin to enable "base collections")

  • discord user avatar
    alessiogr
    Payload Team
    5 months ago

    Maybe! At least the code snippet you shared works perfectly fine in the list view - just tested that.



    Would help a lot if you can figure out a minimal reproduction and publish that one

  • default discord avatar
    ssyberg
    5 months ago

    Fascinating

  • discord user avatar
    alessiogr
    Payload Team
    5 months ago

    I could swear I've encountered a similar problem once where it didn't show the name in the list view - not quite sure anymore. But it really might just happen for some more complex edge cases

  • default discord avatar
    ssyberg
    5 months ago

    I'm gonna bypass the base collection and set useAsTitle directly

  • discord user avatar
    alessiogr
    Payload Team
    5 months ago

    what exactly is a base collection?

  • default discord avatar
    ssyberg
    5 months ago

    Essentially I've added inheritance to payload



    Allows you to set default fields/configs either on all collections or specify specific collections to inherit from



    Here's my base collection that ALL collections extend (except those specified in a an exclude option in the plugin):


    import Title from "@fields/Title";
    import Slug from "@fields/Slug";
    
    const Base = {
      versions: {
        drafts: true,
      },
      access: {
        read: () => true,
      },
      fields: [Title, Slug],
    };
    
    export default Base;


    The plugin is written in such a way that the actual collections can also overrride these values



    Interesting, looking at the network response and the title is not making its way back to the list view

    image.png
  • discord user avatar
    alessiogr
    Payload Team
    5 months ago

    oh interesting - sounds quite useful! And so, the useAsTitle is specified in one of the collections from which it inherits it, and not in the "real" collection?

  • default discord avatar
    ssyberg
    5 months ago

    Correct



    Though I moved it back in to the real collection and it had no effect



    Interestingly

    http://localhost:3000/cms/api/posts/

    returns the title



    But add this where clause and it disappears



    http://localhost:3000/cms/api/posts?depth=0&where%5Bid%5D%5Bin%5D%5B0%5D=64273bfb93fa4750d7ff70cb&locale=en&limit=250


    Oh maybe it's locale related?



    That's it



    http://localhost:3000/cms/api/posts?locale=en

    and

    title

    disappears



    Maybe try your local example again with locales enabled?

  • discord user avatar
    alessiogr
    Payload Team
    5 months ago

    Is field-level localization enabled on the title field

  • default discord avatar
    ssyberg
    5 months ago

    Yes



    Wait a minute



    I don't have an

    en

    locale



    where is that coming from haha

  • discord user avatar
    alessiogr
    Payload Team
    5 months ago

    lolol reproduced it! Most definitely a bug

  • discord user avatar
    alessiogr
    Payload Team
    5 months ago

    hah seems so! The moment I add a translation for en, it works



    doesnt even seem to be the default locale



    just does it for "en"

  • default discord avatar
    ssyberg
    5 months ago

    ha

  • discord user avatar
    alessiogr
    Payload Team
    5 months ago

    If you want to I can open a payload issue for you, since I just made a repro. Or, would you like to do it?

  • default discord avatar
    ssyberg
    5 months ago

    well seems like it'll be an easy fix!



    My kids about to wake up from a nap so I probably wont get to it till tomorrow, so feel free!



    Thanks for the help!

  • discord user avatar
    alessiogr
    Payload Team
    5 months ago

    Sure thing, you're welcome! Might take a look tomorrow myself to try and figure out a fix

  • discord user avatar
    alessiogr
    Payload Team
    5 months ago

    @ssyberg Indeed - another issue was that the relationship Cells weren't refreshed in the first place when you change the locale



    https://github.com/payloadcms/payload/pull/2438

    this PR should fix it ^^

  • default discord avatar
    ssyberg
    5 months ago

    Oh nice find!

  • discord user avatar
    dribbens
    Payload Team
    5 months ago

    I am just getting caught up on this thread. I don't know if this is a bug though.


    The change you made is originally how I was treating the locale to fetch in the

    relationshipsProvider

    . Then this issue was opened and we intentially made the provider use the admin UI i18n setting.


    https://github.com/payloadcms/payload/pull/1648


    I'm not sure the best way forward TBH.

  • discord user avatar
    alessiogr
    Payload Team
    5 months ago

    huh why would you prefer using i18n there instead of locale? Locale would reflect more what this relationship

    actually

    points to

  • default discord avatar
    ssyberg
    5 months ago

    Agreed, I think it needs to reflect the chosen locale

    image.png
  • discord user avatar
    alessiogr
    Payload Team
    5 months ago

    I think i18n is best to use only for things completely unrelated to the data behind it

  • default discord avatar
    ssyberg
    5 months ago

    Yea I think I fundamentally disagree with this:


    Relationship labels with titles should take into account the admin user's i18n language setting.


    As I've already stated, it assumes parity between i18n locales and localization locales and this parity is not otherwise documented that I'm aware of



    Also if the relations

    useAsTitle

    field is localized, it really seems like it should return the currently selected

    locale

    regardless of the admin users language settings

  • discord user avatar
    alessiogr
    Payload Team
    5 months ago

    putting this link here so I can actually open it on my phone

    https://discord.com/channels/967097582721572934/1049367843495481464


    that discord conversation is leaving me confused



    it doesn't quite explain the change from locale to i18n. The author was always talking about an issue with localization and not i18n



    looking at the PR it seems that originally, both the locale and i18n were completely disregarded weren't they?

  • default discord avatar
    tinouti
    5 months ago

    Hey @ssyberg , out of curiosity, is there a trick I don't know about to get nice clean locale names like you have here (maybe even localized), or is that just the actual locales you defined in your config, ie:


    localization: {
        locales: [
          'Arabic',
          'Cyrillic',
          'Hindi',
          'Chinese',
        ],
        ...
  • default discord avatar
    ssyberg
    5 months ago

    @tinouti that's just default Payload behavior AFAIK!

  • default discord avatar
    tinouti
    5 months ago

    So I take it it was the latter (ie: your config file looks like what I put in that code snippet)? No secret tip on how to get nice labels for locales? ๐Ÿ˜ข

  • default discord avatar
    ssyberg
    5 months ago

    I don't think so...

  • discord user avatar
    dribbens
    Payload Team
    5 months ago

    It would be a trivial enhancement to make locales have the same format as

    options

    do with select or radio fields. We just have to update everywhere in the code that uses locales to get the values out and then instead of

    locales: ['en', 'es']

    you'd have

    locales: [{ value: 'en', label: 'English'}, { value: 'es', label: 'espaรฑol' }]

    . Then the UI can use the nice label.



    And make it a nonbreaking change of course.


    If anyone is interested in opening a PR for this, we'd be happy to take it.

  • default discord avatar
    tinouti
    5 months ago

    If @alessiogr the PR vigilante doesn't get to it first, I might give it a try this weekend ๐Ÿ˜„ ๐Ÿ‘

  • discord user avatar
    alessiogr
    Payload Team
    5 months ago

    Feel free hah, don't have enough time for yet another project sadly ๐Ÿ˜ฆ

  • default discord avatar
    tinouti
    4 months ago

    @dribbens would you have any pointers/tips on where/what I should be looking for to get started on this? I've tried poking around in the source, but I don't have a solid grasp on how Payload works under the hood yet so it's been a bit overwhelming. I'd probably start with wherever the config's getting parsed / Payload's getting initialized? ๐Ÿค”



    I was hoping that @jmikrut's "How Payload works" video (

    https://discord.com/channels/967097582721572934/1102950643259424828/1102990277502840852

    ๐Ÿ‘€) was gonna come out and would give me the necessary knowledge and confidence to get started, but given how busy you all are, I realize now that this might not come anytime soon. ๐Ÿ˜‚



    Oh shit, looks like someone might have already done it ๐Ÿ˜ฎ


    https://github.com/payloadcms/payload/commit/c86a9677ec4a03a256e55b5d68a4fab79dfe1523
Open the post
Continue the discussion in Discord
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.