Problem/Motivation

While paths can be altered, link attributes can't.

Proposed resolution

Add hook_link_alter, similar to D8.

Remaining tasks

User interface changes

API changes

Data model changes

CommentFileSizeAuthor
#4 interdiff.txt1.35 KBchx
#4 2562731_4.patch3.37 KBchx
#2 2562731_1.patch3.14 KBchx

Comments

chx created an issue. See original summary.

chx’s picture

StatusFileSize
new3.14 KB
fabianx’s picture

+++ b/modules/system/system.api.php
@@ -4331,6 +4331,53 @@ function hook_url_outbound_alter(&$path, &$options, $original_path) {
+function hook_link_alter($text, &$path, &$options) {

Missing '&' before $text.

chx’s picture

StatusFileSize
new3.37 KB
new1.35 KB

Also added a note about how hook_url_outbound_alter is better for $path .

chx’s picture

Fixing credit.

fabianx’s picture

Status: Needs review » Reviewed & tested by the community

RTBC, I don't think we need tests for this little change.

David_Rothstein’s picture

Status: Reviewed & tested by the community » Needs review
Issue tags: +Needs subsystem maintainer review

My understanding is that this kind of use case is one of the reasons l() was made themable... Via a theme preprocess function or similar, you should be able to alter anything you want to here.

Of course that's an expensive thing to do performance-wise, but the expense comes only for the sites that need it, whereas a drupal_alter() call isn't totally free and impacts the majority of sites that won't ever use it. Plus it adds some extra complexity to what is already a complex area of the code...

I know there are also concerns with l() getting called too early in the bootstrap and triggering the theme system too early, but that should be possible to fix in #2086335: Bootstrap issue with theme initialization causes hook_custom_theme() never to be invoked.

Overall I think this change needs more review; I'm not totally sure that it's justified.

fabianx’s picture

We maybe could make hook_link_alter() optionally enabled via a $settings parameter?

Theme Link via preprocess is not the right thing here as it bundles the fast link system to the (in comparison) very slow theme system.

Status: Needs review » Closed (outdated)

Automatically closed because Drupal 7 security and bugfix support has ended as of 5 January 2025. If the issue verifiably applies to later versions, please reopen with details and update the version.