Jump to content

URL with custom text?


cryostar
 Share

Recommended Posts

The only link-related fields I see are Page References and URL. Although none of these two fields support for having a custom text inside the anchor tag, so what I did was to create to fields - a 'link' URL field and a 'link_title' text field. This sound counterintuitive so I would like to know if there are some good solutions out there.

Link to comment
Share on other sites

Usually when you use a page reference or URL field, you do so to model a specific relationship. That way it is often sufficient to have a fixed link text describing that relationship, or to generate it based on the target page. For example, links might always just say “more info” or “related: <?=$page_reference->title?>”, or the domain name extracted from a URL field.

If you want arbitrary link text that is editable by users, your solution seems perfectly fine.

  • Like 1
Link to comment
Share on other sites

19 hours ago, cryostar said:

This sound counterintuitive so I would like to know if there are some good solutions out there.

As @Jan Romero wrote, its more often than not that a link's textual information is generated dynamically from its relationship (e.g. the page/template it's on or that's referenced).

For manual entry of link texts, creating separate fields for the link target and the text like you did is the way to go. You can, however, make your developer life easier when you have multiple occurrences of that combination in different templates by wrapping both in another field type. This can be a Repeater if you have multiple entries, or for a single one you can use either FieldsetPage or FieldsetGroup, depending on whether you want the link entry to live under a unique property of your page. With fieldsetgroup it is $page->urlfield and $page->linktextfield, while in fieldsetpage its $page->myfieldsetpagefield->urlfield and $page->myfieldsetpagefield->linktextfield. The former is more performant, while the latter makes it more clear that both fields are related.

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...