How can I add record data to the URL prefix in the file viewer?

This seems like a common scenario but I wasn’t able to find info in the docs.

How can I add to the url prefix data from the record being displayed? So I need to substitute in the URL prefix the field id from the record.

image

Thanks!

2 Likes

Hi @jonl :wave:

The value of your field is actually appended after the prefix of the URL.

So assuming your field value is 1234 and the prefix is https://my-prefix/folder/ the URL of your file viewer will be https://my-prefix/folder/1234.

:point_right: If you want to add additional data (such as item ID + some other value separated with a slash), you have to handle it in your database (i.e: save the whole string with all the info needed) or use a smart field

In other words, Forest Admin UI takes the field value as-is, and append it at the end of the URL. Still you can choose what your backend returns to the UI when it ask for item values.

2 Likes

Unfortunately that is not an option. I would have to redo a lot of logic in my app.

This seems quite an omission if it’s not possible.

Hello @jonl :wave:

The workaround @rap2h gave you is quite easy to implement.

In your collection you would just need to create a smart field such as the following:

collection('myCollection', {
  actions: [],
  fields: [{
    field: 'image_link',
    type: 'String',
    get: (record) => {
      return record.originalLinkAttribut + record.id;
    },
  }],
  segments: [],
});

Then configure this field in your UI the same way you did for originalLinkAttribut and you will be ready to go :+1:

You could then hide the originalLinkAttribut field to not pollute your layout.

With this, the url will be computed correctly and your images should be accessible.

Tell me if this helps out, or if you have any question :smiley:

Steve.

3 Likes

I must admit that I did a quick read of his second paragraph and missed completely the “smart field” workaround. I just read make the change in the DB and was disappointed. Should have read more and better.

Thanks both of you guys. A smart field is indeed an option that we can consider.

Edit: Just to confirm that the solution proposed works perfectly. I had to add some additional logic that I didn’t mention, but everything is peachy. Thanks again @rap2h and @Steve_Bunlon

2 Likes

We are glad the solution suits your need :tada::confetti_ball:

Thanks for the update and for posting on the community :pray:

Steve.

2 Likes