Drupal 8 hook_link_alter() example

  1. function custom_module_link_alter(&$variables) {
  2.  
  3. $current_path = \Drupal::request()->getRequestUri();
  4. $current_user = \Drupal::currentUser();
  5. $roles = $current_user->getRoles();
  6.  
  7. if (is_object($variables['url'])) {
  8. $url = $variables['url'];
  9. if ($url->isRouted()) {
  10. $route_name = $url->getRouteName();
  11. if ($route_name != 'view.view_name.page_1') {
  12. return;
  13. }
  14. if (in_array('the_role', $roles) ) {
  15. $variables['text'] = t('@text', ['@text' => 'new text']);
  16. }
  17. }
  18. }
  19. }