use Drupal\Core\Link; $link = Link::fromTextAndUrl('My Blog Post', \Drupal\Core\Url::fromRoute('entity.node.canonical', ['node' => 123])); $link->toString();
use Drupal\Core\Link; $user = \Drupal\user\Entity\User::load(1); $link = Link::fromTextAndUrl($user->getDisplayName(), $user->toUrl()); $link->toString();In this example, we generate a link for a user with ID 1. We use the `load` method to load the user entity and then call its `getDisplayName` method to get the user's name. We use the `toUrl` method to create a Drupal URL for the user and then create the link with `fromTextAndUrl`. Finally, we call the `toString` method to output the link HTML. This library is part of the Drupal core package and can be found in the `core/lib/Drupal/Core/Link.php` file.