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

Members fund testing for the Drupal project. Drupal Association Learn more

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.