`link` fields in REST, JSON:API and GraphQL cannot be rendered by client

Created on 9 July 2019, almost 5 years ago
Updated 30 January 2023, over 1 year ago

Problem/Motivation

Link fields and menu links are output in JSON:API in the following format:

"field_link": [
  {
    "uri": "internal:/",
    "title": "Home",
    "options": [
    ]
  },
  {
    "uri": "entity:node/5",
    "title": "News Center",
    "options": [
    ]
  }
]

This isn't very useful for anything consuming the API because it needs to process these links. In most cases the data can't be fetched because it's unknown what node type is being referenced, and so which endpoint to query is unknown. If this was known, then it would require a lot of unnecessary requests to get the links from various bundle endpoints.

Proposed resolution

Add a computed full_url property at \Drupal\link\Plugin\Field\FieldType\LinkItem::propertyDefinitions()

"field_link": [
  {
    "uri": "internal:/",
    "full_url": '/'
    "title": "Home",
    "options": [
    ]
  },
  {
    "uri": "entity:node/5",
    "full_url": '/node/5'
    "title": "News Center",
    "options": [
    ]
  }
]

Remaining tasks

Review.
RTBC

User interface changes

None

API changes

An additional property full_url is added to the link item.

Data model changes

Adds a new computed field so no change in the data model.

Release notes snippet

A new property full_url has been added to the link field to access the generated URL.

Edit.: The new field was going to be called just url but it caused some twig override issues (see #22,#26,#27 so full_url was used instead as suggested in #67)

✨ Feature request
Status

Needs work

Version

10.1 ✨

Component
LinkΒ  β†’

Last updated 6 days ago

Created by

πŸ‡¬πŸ‡§United Kingdom justafish London, UK

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

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.

Production build 0.69.0 2024