Getting a Link object from a "link" field type

Created on 20 June 2016, over 8 years ago
Updated 1 July 2024, 4 months ago

Problem/Motivation

Currently if we want to get a Link object from a LinkItem object (i.e. field item of the "link" type), we have to get the Url and Title properties of the LinkItem separately and create instantiate a Link object ourselves:

$title = $link_item->get('title');
$url = $link_item->getUrl();
$link = \Drupal::Link->fromTextAndUrl($title, $url);

Steps to reproduce

Try to get a Link object from a LinkItem object

Proposed resolution

Make it possible to get the Link object with a single method call:

$link = $link_item->toLink()

Remaining tasks

None

User interface changes

None

API changes

A new toLink() method is added to Drupal\link\LinkItemInterface and Drupal\link\Plugin\Field\FieldType\LinkItem. It returns a Drupal\Core\Link object.

Data model changes

None

Release notes snippet

It is now possible to get a Link object from the LinkItem object.

Changed record drafted here: https://www.drupal.org/node/3458241 β†’

✨ Feature request
Status

Needs work

Version

11.0 πŸ”₯

Component
LinkΒ  β†’

Last updated 8 days ago

Created by

πŸ‡ͺπŸ‡ͺEstonia thlor

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