The example of hook_link_alter is not working because route_name variable is not passed to this hook. I suppose route name should be obtained from url variable instead.

function hook_link_alter(&$variables) {

  // Add a warning to the end of route links to the admin section.
  if (isset($variables['route_name']) && strpos($variables['route_name'], 'admin') !== FALSE) {
    $variables['text'] = t('@text (Warning!)', [
      '@text' => $variables['text'],
    ]);
  }
}

Issue fork drupal-3204353

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Chi created an issue. See original summary.

pragati_kanade made their first commit to this issue’s fork.

pragati_kanade’s picture

Assigned: Unassigned » pragati_kanade
pragati_kanade’s picture

Assigned: pragati_kanade » Unassigned
Status: Active » Needs review

longwave’s picture

Status: Needs review » Needs work

Thanks for working on this! The code improvement is good but we don't need the additional comment, we can just use a single-line @var comment to denote the variable type.

ravi.shankar made their first commit to this issue’s fork.

ravi.shankar’s picture

Status: Needs work » Needs review

Made changes as per comment #6.

longwave’s picture

Status: Needs review » Reviewed & tested by the community

Thanks!

  • catch committed 8cef0f3 on 9.2.x
    Issue #3204353 by pragati_kanade, ravi.shankar, Chi, longwave:...

  • catch committed 47f8584 on 9.1.x
    Issue #3204353 by pragati_kanade, ravi.shankar, Chi, longwave:...
catch’s picture

Version: 9.2.x-dev » 9.1.x-dev
Status: Reviewed & tested by the community » Fixed

Committed/pushed to 9.2.x and cherry-picked to 8.9.x, thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

wombatbuddy’s picture

I'm not sure that this is the right place to the issue I have discovered, but it looks related. The documentation page for Drupal 10 still has the wrong example: https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Utility%2...
The file "core/lib/Drupal/Core/Utility/link.api.php" referenced by the documentation does not exist.
Also, the link to non-existent file "core/lib/Drupal/Core/Utility/link.api.php" is present on the page with hooks list: https://api.drupal.org/api/drupal/core%21core.api.php/group/hooks/10

Chi’s picture

That happened because #2808577: Move hook_link_alter documentation to core/lib/Drupal/Core/Utility/link.api.php copied old hook code to link.api.php.

@wombatbuddy can you create an issue for this?