cms icon indicating copy to clipboard operation
cms copied to clipboard

Custom Fields for the Link fieldtype

Open Michael-Paragonn opened this issue 1 year ago • 6 comments

With Craft as dependent as it is on custom fields, it seems a bit incongruous to have the advanced fields of the Link fieldtype be a limited assortment of "hard-coded" options. I mean, what you do have absolutely does make sense, it just begs the question of why there isn't a "custom field" type of option.

IRL use cases for custom fields on links:

  • Link style: Many times, a client will want to have the option to show a link in a button-style, rather than a "text link" style. It would simplify things if us developers could provide a dropdown field to select that on the link itself. image
  • Progressive enhancement: A link which goes to a "utility" page of some sort (login/register/search/subscribe/etc) by default, but if JS is enabled, the functionality of those pages gets loaded into a <dialog> instead.
  • Marketing: Typically it's more user-friendly to allow content authors to add marketing URL params in a visual manner instead of a technical one. Having them type out "?ref=campaign_name" in the URL suffix field is a technical approach; allowing them to select (via a Relationship field) the associated Campaign entry is a visual approach.

Taken to the "extreme", perhaps links could benefit from being Entry Types themselves?

Originally posted by @Michael-Paragonn in https://github.com/craftcms/cms/issues/16268#issuecomment-2559004026

Michael-Paragonn avatar Dec 30 '24 14:12 Michael-Paragonn

Hello and sorry to jump in: When the need arises for the more advanced options you describe, why not just use a Matrix field with a custom "Link" Entry Type? I get why it would be more convenient to have extra utilities in the native link field, but there is a balance at play between a) adding complexity to native components and b) providing the flexibility and tools to make your own advanced components. The later option keeps the framework more unopinionated and lightweight. Just my two cents 🙂

thupsi avatar Jan 07 '25 10:01 thupsi

@thupsi Matrix for a single link seems like overkill.

In general adding custom field layouts starts to push this field into the Hyper territory.

Do you have a custom field layout for each link type eg: Entry, Asset, URL?

leevigraham avatar Jan 22 '25 04:01 leevigraham

The native link field is getting better and better! I agree a custom field option would be great. This is how we "solve" it now: Image

A custom field based on a Dropdown with labels and values would already help a lot.

To clean up the UI it would even be nice if the buttons would have min/max settings (like assets/entries), so the setup with matrix field would not be needed and the interface would require less space.

sanderpotjer avatar Jan 23 '25 15:01 sanderpotjer

Agree with @thupsi here. We handle this via a matrix field with a custom entry type.

ryanleichty avatar Aug 13 '25 20:08 ryanleichty

Is there an easy way to add a custom advanced field with a plugin/module? For example, something that looks and functions just like the Target option, but is called Show Icon. If anyone has a boilerplate start on this I'd love to see it.

simonkuran avatar Aug 15 '25 14:08 simonkuran

@simonkuran Craft doesn’t currently trigger any events that would make that possible.

brandonkelly avatar Aug 15 '25 20:08 brandonkelly