Drupal 8: How to Return a Markup incl. a URL link

Submitted by admin on Sun, 03/04/2018 - 01:17

Drupal 8 provides a useful helper funciton $entity->toLink(..). You must return it by converting to a string by using ->toString() at the end.
public function foo(Request $request = NULL) {

$id = $request->get('id'); // from query string
$entity = YourEntity::load($id);

$message = $this->t('Check at @url.', [
'@url' => $entity->toLink('<TEXT_TO_LINK_HERE>', 'edit-form')->toString(),
]);

return [
'#type' => 'markup',
'#markup' => $message,
];
}

Tags