It's good to see the Link field type in D8. It takes a URL and (optionally) link text.

I'm adding a multiple links field to a content type and would like users to be able to provide some styling clues as well: e.g. this link is the primary one, this one needs a download icon. Basically let the user add CSS classes to each of the links in the same way they can add urls, text to each link they enter.

Is there a way to adding a new part to the field type?

(EDIT: I've put the answer into a custom module)

  • Just create new field formatter plugin and extend the \Drupal\link\Plugin\Field\FieldFormatter\LinkFormatter
    – user21641
    Mar 9, 2016 at 11:54
  • Thanks for the hint. The field formatter seems the third thing I'd need to create. First I need somewhere to store the classes data, then I need a widget enabling user to specify classes, then I need a formatter to apply them to the HTML. Mar 9, 2016 at 12:45
  • I think the formatter is a good idea, but it probably doesn't fit @artfulrobot's use case of having users save arbitrary styling clues for link data. You can extend the link class and give it a different annotation and change the schema method (similar to Drupal 7). This creates a new field type plugin. File and Image field type plugins work similarly. This won't do anything about your current data though and you'll need to migrate.
    – mradcliffe
    Mar 9, 2016 at 12:47
  • So there's not a sort of "alter" option where I can alter an existing field (link) to add the extra part? Mar 9, 2016 at 12:52
  • No, there is no field schema alter hook in either Drupal 7 or Drupal 8. Edit: Actually, I guess you could alter the plugin definition, provided your own class, implement schema method, and then all link fields will have that schema from then on. This seems like a lot of behavior to change for something like a custom menu system.
    – mradcliffe
    Mar 9, 2016 at 13:01

1 Answer 1


If you check \Drupal\link\Plugin\Field\FieldType\LinkItem, you see that it supports an array of options.

LinkFormatter then passes those to the URL.

So the only thing that you need is a custom widget that allows you to select/configure classes within the options somehow.

  • I'm not sure if this answers the use case of users saving arbitrary class/style data with the link content when it's created. This would be for a site builder to configure all link field content for a specific link field to have that style/class data.
    – mradcliffe
    Mar 9, 2016 at 16:09
  • Not sure I understand your point. the link field can store any classes. It's just a question of how you expose it in the UI, you could make a textfield so anything is allowed or you could make a select/checkboxes to only allow specific classes. It was explicitly decided to offer the storage but not provide any advanced widgets and leave that to contrib/custom modules.
    – Berdir
    Mar 9, 2016 at 16:32
  • Check out my Segue - classy links module (sandbox) that does as you suggest :-) Mar 15, 2016 at 9:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.