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

Created on 9 July 2019, over 5 years ago
Updated 4 September 2024, 4 months 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

11.0 🔥

Component
Link 

Last updated 3 days ago

Created by

🇬🇧United Kingdom justafish London, UK

Live updates comments and jobs are added and updated live.
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.

Production build 0.71.5 2024