Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
Front-end developers and themers who want full control over the markup use custom link markup in a parent (e.g. node) template:
<a href="{{ content.field_link.url }}" class="link-internal">{{ 'Related'|t }}</a>
Only the file field has a URL only formatter, but Image module and Link module don't. Workarounds like below violate caching because no caching metadata bubbles up:
URL: {{ content.field_link.0['#url'] }}
Proposed resolution
Provide a field formatter that only returns the URL of a link.
Remaining tasks
tbd
User interface changes
tbd
Comment | File | Size | Author |
---|---|---|---|
#23 | url_as_plain_text.JPG | 36.63 KB | Krzysztof Domański |
#21 | 2806199-21-add-link-url-field-formatter.patch | 4.09 KB | mlncn |
#6 | 2806199-6.patch | 1.48 KB | joelpittet |
#6 | interdiff.txt | 2.29 KB | joelpittet |
#4 | link-url-formatter-2806199-4.patch | 1.98 KB | Sutharsan |
Comments
Comment #2
Sutharsan CreditAttribution: Sutharsan commentedComment #3
evanmwillhite CreditAttribution: evanmwillhite commentedDitto what I said here about this approach in component-based theming. Even outside of that though, this is a really common use-case with needed support.
Comment #4
Sutharsan CreditAttribution: Sutharsan commentedThe patch contains the proposed Link URL formatter.
Comment #5
dawehnerSuper nice!
no longer needed
nitpick: wrong docs
I guess we could simplify this by always extracting the cacheability metadata. It would just be empty for the external case
Comment #6
joelpittetTrying to address #5.
Comment #7
dawehnerUnnecessary change
Comment #9
aj2r CreditAttribution: aj2r at La Drupalera by Emergya commentedI've noticed that:
Anyway changed to old 'array()' instead of '[]'.
Comment #10
Mac_Weber CreditAttribution: Mac_Weber as a volunteer commented@dawehner I think we are only using the short array syntax, not? https://www.drupal.org/docs/develop/standards/coding-standards#array
In this case patch #6 looks better.
Comment #11
Pavan B S CreditAttribution: Pavan B S at Valuebound commentedMade changes as suggested in comment #10
Comment #12
dawehnerRight, but my comment is 5 months old :)
Comment #13
Mac_Weber CreditAttribution: Mac_Weber as a volunteer commented@Pavan B S it seems your patch files were created against the wrong branch. Review this: Making a Drupal patch with Git and Creating an interdiff
I marked #6 for retest.
Comment #14
Mac_Weber CreditAttribution: Mac_Weber as a volunteer commentedPatch #6 looks good. I marked it for retest, just in case it needs a re-roll again.
Comment #15
Mac_Weber CreditAttribution: Mac_Weber as a volunteer commentedComment #16
alexpottLooks like #6 is the "correct" patch to review here. As the issue tags say "needs tests" - well it needs tests.
Comment #21
mlncn CreditAttribution: mlncn at Agaric for Drutopia, Find It Cambridge, Cambridge, Massachusetts Family Policy Council commentedHere's a patch with a test added for the URL field formatter!
2806199-21-add-link-url-field-formatter.patch
Here's a commit showing what putting it to use looks like in a template, with a no-cruft field template and generous use of Twig's ability to remove whitespace.
Comment #23
Krzysztof DomańskiWe don't need an additional field formatter. We can set "URL only" and "Show URL as plain text" for the default link formatter. It returns the URL of a link.
Comment #24
Krzysztof Domański