Add attributes on the formatter level

Created on 12 November 2024, 7 months ago

Problem/Motivation

Link Attributes adds attributes on the field widget level, so each link in an entity can have additional attributes, classes, etc. That's super helpful!

But additionally, there's also often the need to set attributes (starting with simple classes) on the formatter level! So they will be same for the link field display and won't change per entity. These attributes can not be changed in entities, as they are set when rendering the field.

Link attributes already has a lot of related functionality and core is missing this feature, so that currently you have to override the link in theme or twig, which is okay, but not what you want from Drupal in many cases.

So we'd like to help implementing this (maybe in a submodule)? link_attributes_formatter?

https://www.drupal.org/project/link_class β†’ does similar things, but seems not very well maintainted and might conflict with link_attributes. Would be much nicer to have all this in link_attributes with known UX. And even by name it fits into "Link Attributes" :)

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

✨ Feature request
Status

Active

Version

2.0

Component

Code

Created by

πŸ‡©πŸ‡ͺGermany Anybody Porta Westfalica

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @Anybody
  • πŸ‡©πŸ‡ͺGermany Anybody Porta Westfalica
  • πŸ‡©πŸ‡ͺGermany Anybody Porta Westfalica

    @larowlan if you're fine with the proposal, we'll start implementing this as submodule via MR.

  • Yes, please make this feature happen. Adding attributes to link fields is horrible on frontend.

  • πŸ‡¦πŸ‡ΊAustralia larowlan πŸ‡¦πŸ‡ΊπŸ.au GMT+10

    Element class formatter does this too I think

    I'm happy with a sub-module, but we'd have to be mindful about how we merge values from the formatter with user supplied values.

    Perhaps we need options 'overrride user options' or 'user options override defaults'

  • πŸ‡©πŸ‡ͺGermany Anybody Porta Westfalica

    Thanks for the feedback @larowlan that's wonderful news!

    Yes I think one of the key benefits is to have that control in one module. For the beginning, I'd say we should simply merge and unify them, not overriding anything. For the future we should then see, if there are cases where overriding into one or the other direction makes sense as an option? (YAGNI)

    It's planned to start here soon! :)

  • Pipeline finished with Success
    7 months ago
    Total: 175s
    #338316
  • πŸ‡©πŸ‡ͺGermany Anybody Porta Westfalica
  • Assigned to lrwebks
  • Status changed to Needs work 4 months ago
  • πŸ‡©πŸ‡ͺGermany lrwebks Porta Westfalica
  • Pipeline finished with Success
    3 months ago
    Total: 177s
    #452078
  • Pipeline finished with Success
    3 months ago
    Total: 174s
    #452098
  • πŸ‡©πŸ‡ͺGermany lrwebks Porta Westfalica

    The formatter is in place and works as expected (as far as I'm aware). If a value is set in the formatter, it overrides any value from the field widget, but keeps the widget value if it is not set in the formatter. I also added a note regarding that in the formatter settings, so that there will be no confusion as to why a setting provided via field widget is not taking effect in the actual view (that confusion would definitely happen to me a lot :|).

    So, it is off to β€œNeeds Review” now.

  • πŸ‡©πŸ‡ͺGermany Grevil
  • Assigned to Grevil
  • Status changed to Needs review about 1 month ago
  • πŸ‡©πŸ‡ͺGermany Grevil

    Great work! Code wise this looks great :)

    Two things, I think this would be really nice as a small (but helpful) addition:

    1. Show the widget setting as ghost text on the related formatter setting input field (if empty).
    2. Show whether a setting is overriden in the summary.

    But I am unsure, whether this is easily implemented.

  • πŸ‡©πŸ‡ͺGermany Grevil

    @larowlan, what do you think about this? Should we use the current approach, or should we enhance this as stated above?

  • πŸ‡¦πŸ‡ΊAustralia larowlan πŸ‡¦πŸ‡ΊπŸ.au GMT+10

    Hey @grevil I'm focusing on 11.2.0 issues this week but will make a note to come back here next week after the alpha window

  • πŸ‡©πŸ‡ͺGermany Grevil

    Ok, thanks @larowlan!

  • πŸ‡¦πŸ‡ΊAustralia larowlan πŸ‡¦πŸ‡ΊπŸ.au GMT+10

    I'm still not convinced this module is the right place for this, there are already several solutions for this (such as element class formatter).

    I _could_ see the benefit in having it in this module _if_ the formatter made use of the attribute plugins - but at present it is a hard-coded list.

    So in order to be useful, I think it should be dynamic (based on the plugins) rather than a fixed list. But even then I'm still not sure this is something we should be doing - as I believe there are other modules that already do this well/better.

  • πŸ‡©πŸ‡ͺGermany Grevil

    Thanks for the feedback @larowlan! We'll discuss this internally and come back to you!

Production build 0.71.5 2024