Allow tokens for url of the Link field

Created on 19 August 2017, almost 8 years ago
Updated 30 January 2023, over 2 years ago

Problem/Motivation

The link text of the link field does allow to use tokens, but the url does not.

A use case example is using the link for a sharing function where the link is

http://twitter.com/share?url=[current-page:url:absolute]&text=[current-page:title]

and the link text "Twitter".

It does not appear logical why I can use tokens for the link text and not for the title. Therefore I marked this as a bug.

Steps to reproduce

  1. Enable token module
  2. Enable link module
  3. Create content type
  4. Add link field to content type form display. Allow unlimited values so we can test lots of things.
  5. Go to /node/add/ to create a new piece of content and to test tokens in the url input of a link field.
  6. Enter title
  7. Enter link title in your newly created link field
  8. Enter url with a token in it.
    • http://twitter.com/share?url=[current-page:url:absolute]&text=[current-page:title]
    • /node/[node:id]
  9. Save the content and see how this doesn't replace the token

Proposed resolution

Optionally allow tokens to be used in the url field of link fields

Remaining tasks

This currently only works on form submissions, tested with a migration and it seems more work is needed to make this work in a migration, my assumption is that it probably won't work in other non-form related entity creations either. example JSON API
links are rendered correctly on the front end, but they are not tied to the context in the database, and are instead tied to the node itself.
The contrib module: https://www.drupal.org/project/field_group_link โ†’ does not render link properly which leads to other questions about how these types of links should be rendered.
Additionally, this does not account for entity references via token.

User interface changes

Maybe allow tokens on the link field creation form.

API changes

Add new method for generating links with tokens.
Add new scheme:// type for tokens token://

Data model changes

Adds token context array to url options array in database so that contextual tokens can validate on form submission.
a:1:{s:7:"context";a:1:{s:4:"node";s:3:"193";}}

Release notes snippet

โœจ Feature request
Status

Needs work

Version

10.1 โœจ

Component
Linkย  โ†’

Last updated 2 days ago

Created by

๐Ÿ‡ฉ๐Ÿ‡ชGermany demonde

Live updates comments and jobs are added and updated live.
  • Needs subsystem maintainer review

    It is used to alert the maintainer(s) of a particular core subsystem that an issue significantly impacts their subsystem, and their signoff is needed (see the governance policy draft for more information). Also, if you use this tag, make sure the issue component is set to the correct subsystem. If an issue significantly impacts more than one subsystem, use needs framework manager review instead.

Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • The Needs Review Queue Bot โ†’ tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

    Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.

    Consult the Drupal Contributor Guide โ†’ to find step-by-step guides for working with issues.

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia abhishek.kumar

    Can this be reviewed by core maintainers and suggest their view on this ?

  • ๐Ÿ‡ง๐Ÿ‡ชBelgium BramDriesen Belgium ๐Ÿ‡ง๐Ÿ‡ช

    Version should always target the latest version and be back ported when needed.

  • ๐Ÿ‡บ๐Ÿ‡ฆUkraine alt.dev

    alt.dev โ†’ made their first commit to this issueโ€™s fork.

  • ๐Ÿ‡บ๐Ÿ‡ฆUkraine alt.dev

    We faced an issue with this patch.

    If the page title has an apostrophe symbol, the \Drupal::service('token')->replace() method will run the Html::escape() method which will convert the apostrophe to the ' HTML symbol. After that, the \Drupal::request()->query->get() function won't be able to extract the whole param and will trim everything after the # symbol.

    I made changes so that the patch will use the "replacePlain()" method instead.

  • ๐Ÿ‡บ๐Ÿ‡ฆUkraine alt.dev

    Adding the patch with the current state of the branch. Works with 10.4.3.

  • First commit to issue fork.
  • fromme โ†’ changed the visibility of the branch 2903336-allow-tokens-for to hidden.

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia junaidpv Kannur, Kerala

    Re-rolled #65 for 10.4.5

  • Pipeline finished with Failed
    8 days ago
    Total: 684s
    #507840
  • Pipeline finished with Failed
    8 days ago
    Total: 618s
    #508560
  • Pipeline finished with Failed
    8 days ago
    Total: 675s
    #508750
  • Pipeline finished with Success
    7 days ago
    Total: 792s
    #509482
  • Pipeline finished with Failed
    6 days ago
    Total: 841s
    #510347
  • Pipeline finished with Success
    6 days ago
    Total: 761s
    #510445
  • Pipeline finished with Success
    3 days ago
    Total: 626s
    #512481
  • Pipeline finished with Success
    2 days ago
    Total: 803s
    #512536
Production build 0.71.5 2024