#Displaying virtual titles in the relationship field

1 messages · Page 1 of 1 (latest)

loud fjord
#

Does anyone know how I can show computed virtual titles in relationship dropdowns?

I have a vehicles collection where the title is generated in an afterRead hook (built from specs like make, model, year, etc).

// Virtual title field
{
  name: 'title',
  type: 'text',
  // ... other config
}

// Collection hooks
hooks: {
  afterRead: [
    async ({ doc }) => {
      if (doc?.specifications) {
        doc.title = formatTitle(doc, 'en') // Computes title from specifications
      }
      return doc
    }
  ]
}
// Admin config
admin: {
  useAsTitle: 'title'
}

It works fine in:

  • API responses
  • Admin list views
  • Individual docs

but in relationship dropdowns I just see Untitled – ID: xxx. Looks like those queries don't run afterRead, so the dropdown only gets the raw (empty) DB value.

Is there a way to make relationship dropdowns use the computed title? Or another approach for useAsTitle with virtual fields?

Example: instead of Untitled – ID: 123, I’d like it to show something like:
BMW X5, 2023, Automatic, Diesel

I have also tried inserting the hooks inside the title field itself. I've also tried to make a custom relationship component but there doesn't seem to be a way to control the dropdown list or how the selected item behaves - the same goes for the hasMany relationship field.

Thanks! 🙏

frozen zealotBOT
dry otter
#

I don't think you can. What I have done when I have needed this is that I created title as a regular text field instead. And created a hook to save the correct value. Then it can be used everywhere as a regular field.