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

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Sutharsan created an issue. See original summary.

Sutharsan’s picture

evanmwillhite’s picture

Ditto 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.

Sutharsan’s picture

Status: Active » Needs review
Issue tags: +Needs tests
Related issues: +#2517030: Add a URL formatter for the image field
FileSize
1.98 KB

The patch contains the proposed Link URL formatter.

dawehner’s picture

Super nice!

  1. +++ b/core/modules/link/src/Plugin/Field/FieldFormatter/LinkUrlFormatter.php
    @@ -0,0 +1,68 @@
    +/**
    + * @file
    + * Contains \Drupal\image\Plugin\Field\FieldFormatter\ImageUrlFormatter.
    + */
    

    no longer needed

  2. +++ b/core/modules/link/src/Plugin/Field/FieldFormatter/LinkUrlFormatter.php
    @@ -0,0 +1,68 @@
    +/**
    + * Plugin implementation of the 'image_url' formatter.
    

    nitpick: wrong docs

  3. +++ b/core/modules/link/src/Plugin/Field/FieldFormatter/LinkUrlFormatter.php
    @@ -0,0 +1,68 @@
    +      // Generate external URL without cacheable metadata or internal URL and
    +      // collect cacheable metadata.
    +      if ($link_url->isExternal()) {
    +        $url = $link_url->toString(FALSE);
    +      }
    +      else {
    +        $generated_url = $link_url->toString(TRUE);
    +        $url = $generated_url->getGeneratedUrl();
    +        $cacheable_metadata = CacheableMetadata::createFromObject($generated_url);
    +      }
    

    I guess we could simplify this by always extracting the cacheability metadata. It would just be empty for the external case

joelpittet’s picture

dawehner’s picture

Status: Needs review » Needs work
+++ b/core/modules/link/src/Plugin/Field/FieldFormatter/LinkUrlFormatter.php
@@ -33,34 +23,26 @@ class LinkUrlFormatter extends LinkFormatter {
    */
   public function settingsSummary() {
-    return array();
+    return [];
   }
...
-    $elements = array();
+    $elements = [];
     $cacheable_metadata = NULL;

Unnecessary change

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

aj2r’s picture

Status: Needs work » Needs review
Issue tags: +DevDaysSeville
FileSize
753 bytes
1.49 KB

I've noticed that:

PHP >= 5.4 the short array syntax seems to be the de-facto standard. Only use array(), if you want your script to run on PHP <5.4.

Anyway changed to old 'array()' instead of '[]'.

Mac_Weber’s picture

@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.

Pavan B S’s picture

Made changes as suggested in comment #10

dawehner’s picture

@dawehner I think we are only using the short array syntax, not?

Right, but my comment is 5 months old :)

Mac_Weber’s picture

@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.

Mac_Weber’s picture

Status: Needs review » Reviewed & tested by the community

Patch #6 looks good. I marked it for retest, just in case it needs a re-roll again.

Mac_Weber’s picture

alexpott’s picture

Status: Reviewed & tested by the community » Needs work

Looks like #6 is the "correct" patch to review here. As the issue tags say "needs tests" - well it needs tests.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

mlncn’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests
FileSize
4.09 KB

Here'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.

Status: Needs review » Needs work

The last submitted patch, 21: 2806199-21-add-link-url-field-formatter.patch, failed testing. View results

Krzysztof Domański’s picture

Issue tags: -DevDaysSeville
FileSize
36.63 KB

We 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.

Format settings for link field

Krzysztof Domański’s picture

Status: Needs work » Closed (won't fix)