#Create Internal Link to Custom Route

12 messages · Page 1 of 1 (latest)

livid surge
#

I have create a custom collection called services and a page to display all items in that collection as a list. Now I am stuck at how to add a link to that page, in my services route as an internal link. I very used the posts collection and route as inspiration, roughly, but the links feature of rich text only lets me enable my collection and add links to individual items from that collection.

The best I can do for now is to use a custom url to link to the services page, but the host name is a problem. I could probably use a hook somewhere to fix the host name, but I'd rather use the internal link function if at all possible.

lime arrowBOT
frail perch
#

Hey @livid surge if I'm understanding this correctly the answer is a pretty simple one actually!

The internal link picker only works with documents in a collection — it can't point to a custom route directly.

You can just create a Page with the slug services in your Pages collection. Your /services route can then just redirect to or render from that page document, and it becomes selectable as a normal internal link in the rich text editor

livid surge
# frail perch Hey <@1068465205589319730> if I'm understanding this correctly the answer is a p...

Thanks. A redirect did actually occur to me, but then I got stuck on how to do the redirection or rendering, because, limited by my lack of Payload experience, I only know how to run custom code from a custom route.

Let's say I add a page to my Pages collection, Servicesproxy or such, with slug svc-slug, what can I do in that page's content to redirect? Or should I be looking at a hook to check for that slug and inject a redirect component, or maybe my service list component?

frail perch
#

Let me ask you this—on your frontend right now, if you go to domainname.com/services does your collection list come up as you mentioned?

livid surge
#

Yes

#

Right now I just have a hard-coded Link in the header pointed there, but I want to be able to add such a link in content as well

frail perch
#

Ok—you shouldn't even need a redirect then, just add a blank Page in the CMS with a slug of services and it should A. appear in the internal link dropdown to link to B. just work

livid surge
#

Oh, OK, like a "pass through" or dummy slug just to build the link. I'll check it out quick.

frail perch
#

basically yes

#

your explcit route is going to take precedence over the routes for Page docs, so really it's just giving your link component a way to reference where you want it to go

livid surge
#

Wonderful, thanks. I think this is going to come in quite handy. I've used a few custom routes to skirt learning Payload too in depth for just the basics I need right now and this will be great for that.