\Drupal\Core\Url objects should not be responsible for attribute handling from \Drupal:l()

Created on 2 December 2014, almost 11 years ago
Updated 24 July 2025, 3 months ago

Problem/Motivation

HTML attributes are currently set in \Drupal\Core\Url, instead of the \Drupal\Core\Utility\LinkGenerator. \Drupal:l() only accepts 2 parameters $text and Url $url. This means when creating a link, you end up doing something like this:

$external_url = Url::fromUri('http://www.drupal.org');
$external_url->setOption('attributes', array('class' => array('my-class')));
\Drupal::l('Drupal Homepage', $external_url);

If I wanted to print that link twice with a separate set of html attributes, I either have to remove attributes, clone the obj before adding attributes, or create a new URL.

Proposed resolution

It seems me that Url(or UrlGenerator) should be responsible for 'query', 'fragment', etc... Meanwhile LinkGenerator should be responsible for html attributes 'class', 'target', etc..

Remaining tasks

User interface changes

API changes

Data model changes

πŸ“Œ Task
Status

Active

Version

11.0 πŸ”₯

Component

routing system

Created by

πŸ‡ΊπŸ‡ΈUnited States michaellander

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.

Production build 0.71.5 2024